Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 2 Passing Two Dimensional Arrays and One Dimensional Arrays As Parameter to Functions By: Nor Zalina Ismail Faculty of Computer and Mathematical.

Similar presentations


Presentation on theme: "Topic 2 Passing Two Dimensional Arrays and One Dimensional Arrays As Parameter to Functions By: Nor Zalina Ismail Faculty of Computer and Mathematical."— Presentation transcript:

1 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

2 Objective : In this topic you will: Discover how to pass an array as a parameter to a function

3 Arrays as parameter Arrays are passing by using reference parameter but the & symbol is not used

4 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(int i=0;i<size;i++) cout<<setw(5)<<Matrix[i]; }

5 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.

6 Printing an Array(Two Dimensional Array) using Function void printMatrix(int Matrix[][numOfCol],int numOfRows,int NumOfCol); void main() {const int numOfRows=4; const int numOfCol=3; int data[numOfRows][numOfCol]={{3,1,7},{4,8,10}, {12,9,5},{7,1,4}}; printMatrix(data,numOfRows,numOfCol); getch(); }

7 Printing an Array(Two Dimensional Array) using Function(continued) void printMatrix(int Matrix[][numOfCol],int numOfRows,int numOfCol) { for(int r=0;r<numOfRows;r++) {for(int col=0;col<numOfCol;col++) cout<<setw(5)<<Matrix[r][col]; cout<<endl; }

8 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).

9 Processing Two Dimensional Arrays (c-String) void printMatrix(char Matrix[][numOfCol],int numOfRows, int numOfCol); int calcNor(char Matrix[][numOfCol],int numOfRows, int numOfCol); void main() { const int numOfRows=3; const int numOfCol=10; char data[numOfRows][numOfCol]={{“narina”},{“nora”},{“ahmad”}}; int numOfNor; printMatrix(data,numOfRows, numOfCol); numOfNor=calcNor(data,numOfRows, numOfCol); cout<<"\nNumber of nor are:"<<numOfNor; getch(); } void printMatrix(char Matrix[][numOfCol],int numOfRows, int numOfCol) { for(int row=0;row<numOfRows;row++) cout<<Matrix[row]<<" "; } int calcNor(char Matrix[][numOfCol],int numOfRows, int numOfCol) { int countNor=0; for(int row=0;row<numOfRows;row++) if((Matrix[row][0]=='n')&& (Matrix[row][1]=='o')&& (Matrix[row][2]=='r')) countNor++; return countNor; }

10 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: 1.Input all the data into an array. 2.Display all the content of an array. 3.Find and display the sum and average of the entire array. 4.Find and return the largest value in third row. 5.Find an display the sum value for each row. Call a function in main program and display an appropriate output.


Download ppt "Topic 2 Passing Two Dimensional Arrays and One Dimensional Arrays As Parameter to Functions By: Nor Zalina Ismail Faculty of Computer and Mathematical."

Similar presentations


Ads by Google