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

## 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:

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: 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(int i=0;i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4129259/slides/slide_4.jpg", "name": "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

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

Printing an Array(Two Dimensional Array) using Function(continued) void printMatrix(int Matrix[][numOfCol],int numOfRows,int numOfCol) { for(int r=0;r { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4129259/slides/slide_7.jpg", "name": "Printing an Array(Two Dimensional Array) using Function(continued) void printMatrix(int Matrix[][numOfCol],int numOfRows,int numOfCol) { for(int r=0;r

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],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:"< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4129259/slides/slide_9.jpg", "name": "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: <

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