Professional Documents
Culture Documents
Lecture - Lecture 11 2D Arrays and Functions - 1459387
Lecture - Lecture 11 2D Arrays and Functions - 1459387
(CSCP 1013)
Affefah Qureshi
i n t main() {
i n t marks = 75;
AddGraceMarks(marks) ;
cout<<“You marks i n PF ar e : ”<<mar ks<<end l;
return 0 ;
}
USING REFERENCE VARIABLES WITH FUNCTIONS
c
i n t main() {
i n t marks = 75;
AddGraceMarks(marks) ;
cout<<“You marks i n PF ar e : ”<<mar ks<<end l;
return 0 ;
}
PASSING AN ARRAY TO A FUNCTION
c
• We need to tell the compiler what the type of the
array, and give it a variable name (a reference)
float a[ ]
• We don’t want to specify the size so function can
work with different sized arrays
• Size may be provided as second parameter
• Arrays are automatically passed by reference.
• Do not use & symbol
PASSING AN ARRAY TO A FUNCTION
c
An int array
i n t Displ ay(i n t dat a [ ] , in t N) { The size of the array
i n t sum=0;
cout << “\ n Calcul ating sum…" << endl ;
f o r ( i n t i = 0 ; i < rows; i+ + ) {
f o r ( i n t j = 0 ; j < cols; j + + )
sum+= A r r [ i ] [ j ] ;
return sum;
}
i n t main() {
/ / i n i t i a l i z e 2d array
i n t TDArr[3][4] = { { 3 , 4 , 1 , 3 } , { 9 , 5 , 2 , 1 } , { 7 , 0 , 2 , 1 } } ;
return 0 ; }