1 / 10

Topic 2 Passing Two Dimensional Arrays and One Dimensional Arrays As Parameter to Functions

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:

xue
Download Presentation

Topic 2 Passing Two Dimensional Arrays and One Dimensional Arrays As Parameter to Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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(inti=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

  7. 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; } }

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

  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: • 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.

More Related