Topic 2: Passing Two Dimensional Arrays and One Dimensional

advertisement
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<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
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<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],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; }
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.
2.
3.
4.
5.
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.
Download