110 likes | 259 Views
Topic 2 Passing Two Dimensional Arrays and One Dimensional Arrays As Parameter to Functions. By: Nor Zalina Ismail Faculty of Computer and Mathematical Sciences www2.pahang.uitm.edu.my/nor_zalina. Objective :. In this topic you will:
E N D
Topic 2Passing Two Dimensional Arrays and One Dimensional Arrays As Parameter to Functions By: Nor Zalina Ismail Faculty of Computer and Mathematical Sciences www2.pahang.uitm.edu.my/nor_zalina
Objective : • In this topic you will: • Discover how to pass an array as a parameter to a function
Arrays as parameter • Arrays are passing by using reference parameter but the & symbol is not used
Printing an Array(One Dimensional Array) using Function void printMatrix(int Matrix[],int size); void main() { const int size=4; int data[size]={3,1,7,10}; printMatrix(data,size); getch(); } void printMatrix(int Matrix[],int size) { for(inti=0;i<size;i++) cout<<setw(5)<<Matrix[i]; }
Exercise on Processing One Dimensional Array using Function By using a program in previous slide, add a function called findLowest that will receive an array and size and return the lowest number of an array. You should display an output in the main program.
Printing an Array(Two Dimensional Array) using Function(continued) void printMatrix(int Matrix[][numOfCol],intnumOfRows,intnumOfCol) { for(int r=0;r<numOfRows;r++) {for(int col=0;col<numOfCol;col++) cout<<setw(5)<<Matrix[r][col]; cout<<endl; } }
Exercise on Processing Two Dimensional Array using Function Add a function called findAverage that will receive an array,number of rows and number of column and display the average for the second column(in the function).
Processing Two Dimensional Arrays (c-String) void printMatrix(char Matrix[][numOfCol],intnumOfRows, intnumOfCol); intcalcNor(char Matrix[][numOfCol],intnumOfRows, intnumOfCol); void main() { constintnumOfRows=3; constintnumOfCol=10; char data[numOfRows][numOfCol]={{“narina”},{“nora”},{“ahmad”}}; intnumOfNor; printMatrix(data,numOfRows, numOfCol); numOfNor=calcNor(data,numOfRows, numOfCol); cout<<"\nNumber of nor are:"<<numOfNor; getch(); } void printMatrix(char Matrix[][numOfCol],intnumOfRows, intnumOfCol) { for(int row=0;row<numOfRows;row++) cout<<Matrix[row]<<" "; } intcalcNor(char Matrix[][numOfCol],intnumOfRows , intnumOfCol) { intcountNor=0; for(int row=0;row<numOfRows;row++) if((Matrix[row][0]=='n')&& (Matrix[row][1]=='o')&& (Matrix[row][2]=='r')) countNor++; return countNor; }
Exercise on Processing Two Dimensional Arrays Write a program to declare an integer array declared as data[5][6] in main program. From the array, write a function that will: • Input all the data into an array. • Display all the content of an array. • Find and display the sum and average of the entire array. • Find and return the largest value in third row. • Find an display the sum value for each row. Call a function in main program and display an appropriate output.