You are on page 1of 9

1

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:

data_type Array_Name [Size];


For example,

int x[3] ;

With the help of the following program, the array declaration and initialization is best
understood.

#include <iostream.h>
int main ( )
{

int n[5] = {2,3,5,7,6}; // An integer array of size five


int i;
cout << Element<<\t\tValue;
for( i = 0; i <=4 ; i++ )
cout <<setw(4)<< i << setw(15) << n[i] << endl;
return 0;
}

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.

Enter the numbers to be averaged out


Index
Index
Index
Index
Index
Index
Index
Index
Index

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

PASSING AN ARRAY TO A FUNCTION


Arrays can be passed to functions with simply with their names without any brackets.
Normally the size of the array is also passed to the function. For example, if students is an
integer array and it is passed to function called engineering, the syntax would be:
int students[100];
engineering(students,100);
C++ passes arrays to function by CALL BY REFERENCE. This means the function can modify
the original array. Since the name of the array is the starting address of the array, the
function can access the original contents of the array.
#include <iostream.h>
#include <iomanip.h>
void modifyArray( int [], int );
void modifyElement( int );
int main()
{
const int arraySize = 5;
int a[ arraySize ] = { 0, 1, 2, 3, 4 };
cout << "Effects of passing entire array by reference:"
<< "\n\nThe values of the original array are:\n";
// output original array elements
for ( int i = 0; i < arraySize; i++ )
cout << setw( 3 ) << a[ i ];
cout << endl;

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.

Effects of passing entire array by reference:


The values of the original array are:
0 1 2 3 4
The values of the modified array are:
0 2 4 6 8
Effects of passing array element by value:
a[3] before modifyElement: 6

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;

cout << endl;


return 0;

void bubblesort( int array[] )


{
int pass, hold, j ;
for ( pass = 0 ; pass < size-1 ; pass++ ){
for ( j = 0; j < size-1 ; j++ ){
if ( array[j] > array[j+1] )
{
hold = array[j];

6
array[j] = array[j+1];
array[j+1] = hold;
}

Arrays

}
return;
}
TASK 4: EXECUTE THE ABOVE PROGRAM. WRITE AND EXPLAIN THE OUTPUT.

Data in original order


100
23
32
1
33
67
4
34
55
88
Data after sorting
1
4
23
32
33
34
55
67
88
100
TASK 5: MODIFY THE ABOVE SORTING PROGRAM TO PRINT THE ARRAY IN DESCENDING
ORDER.

#include <iostream.h>
#include <conio.h>
#include<iomanip.h>

void bubblesort(int array[]);


#define size 10
int main()

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

cout << "\t Data after sorting\n";


for ( i = 0 ; i<size ; i++ )
cout << array[i] << endl;

cout << endl;


int z1;
cin>>z1;
return 0;
}

void bubblesort( int array[] )


{
int pass, hold, j ;
for ( pass = 0 ; pass < size-1 ; pass++ ){
for ( j = 0; j < size-1 ; j++ ){
if ( array[j] < array[j+1] )
{
hold = array[j];
array[j] = array[j+1];
array[j+1] = hold;

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

You might also like