Professional Documents
Culture Documents
Arrays
C O M P U T E R F U N DA M E N TA L S
2009-ME-383
Kamran Ali Ahmed
*Answers are given at specified places. .
ARRAYS
In this laboratory session you will:
1. Learn how to manipulate arrays.
2. Learn how to pass an array to a function.
3. Learn sorting algorithms.
ARRAY DECLARATION
An array is a consecutive group of memory locations that all have the same name and the
same type. To refer to a particular location or element in the array, we specify the name of
the array and the position number of the particular element in the array. The position
number is called the Array Index. The array declaration has the following syntax:
int x[3] ;
With the help of the following program, the array declaration and initialization is best
understood.
#include <iostream.h>
int main ( )
{
2
Arrays
TASK 1: EXECUTE THE ABOVE PROGRAM AND RECORD THE RESULTS AS YOU SEE ON THE
SCREEN.
_____________element___________________________________value_____________________
____________________________________________0
2
_
1
3
______________2_________________________________________5___________________________
_____________________________
______________3_________________________________________7___________________________
____________________________
______________4_________________________________________6___________________________
__________________________
_____________________________________________________________________________________
_____________________________
_____________________________________________________________________________________
_____________________________
#include <iostream.h>
int main ( )
{
int n;
float avg, d, sum = 0;
float list[10];
cout << "\n\t\tEnter the numbers to be averaged out\n\n";
for ( n = 0; n < 10; ++n)
{
cout << Index Equals << n <<\tNumber is;
cin >>list[n];
sum += list[n];
}
avg = sum/10;
cout << The average is << avg;
return 0;
}
TASK 2: TRY TO ENTER AS MANY FLOATING-POINT NUMBERS AS YOU CAN; NOTE THE
OUTPUTS.
Equals0
Equals1
Equals2
Equals3
Equals4
Equals5
Equals6
Equals7
Equals8
Number
Number
Number
Number
Number
Number
Number
Number
Number
is1
is2
is3
is4
is5
is6
is7
is8
is9
3
Index Equals9 Number is10
The average is5.5
Arrays
4
// pass array a to modifyArray by reference
Arrays
modifyArray( a, arraySize );
cout << "The values of the modified array are:\n";
// output modified array elements
for ( int j = 0; j < arraySize; j++ )
cout << setw( 3 ) << a[ j ];
cout << "\n\n\nEffects of passing array element by value:"
<< "\n\na[3] before modifyElement: " << a[ 3 ] << endl;
modifyElement( a[ 3 ] ); // pass array element a[ 3 ] by value
cout << "a[3] after modifyElement: " << a[ 3 ] << endl;
return 0;
}
// in function modifyArray, "b" points to the original array "a" in memory
void modifyArray( int b[], int sizeOfArray )
{
// multiply each array element by 2
for ( int k = 0; k < sizeOfArray; k++ )
b[ k ] *= 2;
}
// in function modifyElement, "e" is a local copy of
// array element a[ 3 ] passed from main
void modifyElement( int e )
{
// multiply parameter by 2
cout << "Value of element in modifyElement: " << ( e *= 2 ) << endl;
}
TASK 3: EXECUTE THE ABOVE CODE. NOTE THE CONTENTS OF THE ARRAY BEFORE AND
AFTER THE EXECUTION.
5
Value of element in modifyElement: 12
a[3] after modifyElement: 6
Arrays
SORTING AN ARRAY
There are many algorithms to sort an array. The most elementary ones are bubble sort and
insertion sort. We shall the working of bubble sort.
#include<iostream.h>
void bubblesort(int array[]);
#define size 10
int main()
{
int array[size] = { 100,23,32,1,33,67,4,34,55,88};
int i;
cout << \tData in original order\n;
for ( i = 0 ; i< size ; i++ )
cout << array[i] << endl;
bubblesort(array); // SORTING
cout << \t Data after sorting\n;
for ( i = 0 ; i<size ; i++ )
cout << array[i] << endl;
6
array[j] = array[j+1];
array[j+1] = hold;
}
Arrays
}
return;
}
TASK 4: EXECUTE THE ABOVE PROGRAM. WRITE AND EXPLAIN THE OUTPUT.
#include <iostream.h>
#include <conio.h>
#include<iomanip.h>
7
Arrays
{
int array[size] = { 100,23,32,1,33,67,4,34,55,88};
int i;
cout << " \tData in original order\n";
for ( i = 0 ; i< size ; i++ )
cout << array[i] << endl;
bubblesort(array); // SORTING
8
}
Arrays
}
}
return;
}
TASK 6: WRITE A PROGRAM WHICH TAKES A STRING (CHARACTER ARRAY) AND PRINTS IT IN
THE REVERSE ORDER
#include <iostream.h>
#include <conio.h>
#include<iomanip.h>
#define size 5
int main()
{ clrscr();
int i;
char array[size] = { 'a','b','c','d','e'};
cout<<"origanal\n"<<array[0]<<array[1]<<array[2]<<array[3]<<array[4]
<<"\n";
for( i=5;i>=0;i--){
cout<<"\nreverse order\n";
cout<<array[i];
}
9
int z;
cin>>z;
return 0;
}
Arrays