Computer+Fundamentals-lab+manual-Arrays+(4)

10
1 Arrays COMPUTER FUNDAMENTALS 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 ( )

description

manual

Transcript of Computer+Fundamentals-lab+manual-Arrays+(4)

Page 1: Computer+Fundamentals-lab+manual-Arrays+(4)

1Arrays

CO M P U T ER F U N DA M EN T A L S

2009-ME-383Kamran 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 fiveint i;cout << “Element”<<”\t\tValue”;for( i = 0; i <=4 ; i++ )

cout <<setw(4)<< i << setw(15) << n[i] << endl;return 0;

}

TASK 1: EXECUTE THE ABOVE PROGRAM AND RECORD THE RESULTS AS YOU SEE ON THE SCREEN.

Page 2: Computer+Fundamentals-lab+manual-Arrays+(4)

2Arrays

_____________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 Equals0 Number is1Index Equals1 Number is2Index Equals2 Number is3Index Equals3 Number is4Index Equals4 Number is5Index Equals5 Number is6Index Equals6 Number is7Index Equals7 Number is8Index Equals8 Number is9Index Equals9 Number is10The average is5.5

Page 3: Computer+Fundamentals-lab+manual-Arrays+(4)

3Arrays

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; // pass array a to modifyArray by reference

modifyArray( a, arraySize );

Page 4: Computer+Fundamentals-lab+manual-Arrays+(4)

4Arrays

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 4The values of the modified array are: 0 2 4 6 8

Effects of passing array element by value:

a[3] before modifyElement: 6Value of element in modifyElement: 12a[3] after modifyElement: 6

Page 5: Computer+Fundamentals-lab+manual-Arrays+(4)

5Arrays

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 10int 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];array[j] = array[j+1];array[j+1] = hold;

}

Page 6: Computer+Fundamentals-lab+manual-Arrays+(4)

6Arrays

} }return;}

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

Data in original order1002332133674345588 Data after sorting1423323334556788100

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()

{

int array[size] = { 100,23,32,1,33,67,4,34,55,88};

Page 7: Computer+Fundamentals-lab+manual-Arrays+(4)

7Arrays

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;

}

}

Page 8: Computer+Fundamentals-lab+manual-Arrays+(4)

8Arrays

}

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];

}

int z;

cin>>z;

Page 9: Computer+Fundamentals-lab+manual-Arrays+(4)

9Arrays

return 0;

}