0% found this document useful (0 votes)
15 views

06_2D-Arrays_ITEC112_2022-23_Fall

The document provides an introduction to programming concepts related to arrays and functions in C/C++. It explains how to pass arrays to functions, the limitations of returning arrays, and introduces multi-dimensional arrays, specifically two-dimensional arrays. Additionally, it includes examples and exercises to illustrate these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

06_2D-Arrays_ITEC112_2022-23_Fall

The document provides an introduction to programming concepts related to arrays and functions in C/C++. It explains how to pass arrays to functions, the limitations of returning arrays, and introduces multi-dimensional arrays, specifically two-dimensional arrays. Additionally, it includes examples and exercises to illustrate these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

[COMP112, MISY112, ITEC112, CTPR112, ITSE112]

Introduction to Programming
2022 – 2023 Spring Semester

Arrays in Functions
&
2D Arrays
Passing an array to a function
• It is possible to send an array as an argument to a function.
Function name Array name Array size

returnType functionName( dataType arrayName[arraySize] )


{
// code for function
}
Example : Passing an array to a function
// Passing an array to a function
#include <iostream> When we pass an array as an argument
using namespace std; to a function, only the name of the
void showValues( int[] ); array is used.
NO square brackets []
int main(void){
int myArrray[5]= {2,10,45,5,4};

showValues(myArrray);

return 0;
}

void showValues( int a[] ){


for( int i=0; i<5; i++ )
cout<< a[i] << endl;
}
Returning an Array From a Function
• C/C++ does not allow returning an array from a function.
• However, it is possible to return a pointer to an array by
specifying the array's name without an index.
Arrays To and From a Function
Arrays can be passed as arguments to a function

Arrays can NOT be returned


from a function

int[] func(int arr[])


{

return arr;
}
Exercise 1
/* Arrays : Exercise-1

Write a program that asks the user to enter 3 quiz results from the
keyboard
and store them in an array. Having done so, calculate the average of
these
quizzes in a user defined function named: calculateAve.
The function prototype is : float calculateAve( int array[] );

*/
Exercise 1
#include <iostream>
using namespace std;

float calculateAve( int [] );

int main(void){
int mark[3];

for( int i=0; i<3; i++ ){


cout << "Enter Quiz Result #" << i+1 << " : ";
cin >> mark[i];
}

cout << endl << "Average of Quizzes = " << calculateAve(mark);

return 0;
}

float calculateAve( int Qresults[] ){


int sum = 0;
float average = 0;

for( int i=0; i<3; i++ )


sum = sum + Qresults[i];

average = (float)sum/3;

return average;
}
Multi-Dimensional Arrays
• Multi dimensional arrays can be referred as “array of arrays”.
• For a Two - Dimensional (2D) array, we can visualise the elements
inside the array are stored in rows and columns.
• The total number of elements in a 2D array is calculated by
multiplying the row size (first dimension) with the column size
(second dimension).
• Two-dimensional arrays are often called matrices.
Structure of 2D Arrays

dataType arrayName[rowSize][columnSize]

In 2D arrays, each element is referenced with a row and a column number.

Total Number of Elements in 2D Arrays = rowSize x columnSize


Declaration of a 2D Array
A 2D array that contains 3 rows and 4 columns of
int a[3][4]; integer numbers. The array has 12 elements.

First Column Second Column Third Column Fourth Column


(index 0) (index 1) (index 2) (index 3)

First Row (index 0) a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ]


Second Row (index 1) a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]
Third Row (index 2) a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]
Iterating 2D Arrays
• 2D arrays are iterated using a nested loop (nested for loop).
Example 1 :
Initialising the elements of a 2D Array
// Initialising the elements of a 2D array
#include <iostream>
using namespace std;
First Column Second Column Third Column
(index 0) (index 1) (index 2)
int main(void){
int d[3][3];

d[0][0]=5;
d[0][1]=9; First Row (index 0) 5 9 4
d[0][2]=4;

d[1][0]=4;
d[1][1]=2; Second Row (index 1) 4 2 3
d[1][2]=3;

d[2][0]=8;
d[2][1]=6; Third Row (index 2) 8 6 1
d[2][2]=1;

for( int row=0; row<3; row++ ){


for( int col=0; col<3; col++ )
cout << d[row][col] << " ";
cout << endl;
}
return 0;
}
Example 2 :
Initialising the elements of a 2D Array
// Initialising the elements of a 2D array
#include <iostream>
using namespace std;

int main(void){ 5 9 4
int b[3][3]={ {5,9,4}, {4,2,3}, {8,6,1} };
4 2 3
for( int i=0; i<3; i++ ){
for( int j=0; j<3; j++ ) 8 6 1
cout << b[i][j] << " ";
cout << endl;
}

return 0;
}
What happens
When the assigned values do not fill the size of the array?
The rest of the values are accepted as zero.
double rate[3][2] = { {0.075, 0.080} };
Column 0 Column 1

Row 0 0.075 0.080


Row 1 0 0
Row 2 0 0
Example 2 :
Initialising the elements of a 2D Array
// Initialising the elements of a 2D array
#include <iostream>
using namespace std;
5 4 0
int main(void){
int b[3][3]={ {5,4}, {4}, {8,6} };
4 0 0
for( int row=0; row<3; row++ ){
for( int col=0; col<3; col++ )
cout << b[row][col] << " "; 8 6 0
cout << endl;
}

return 0;
}
What is the output of the following program?
[0] [1] [2]
#include <iostream>
using namespace std; C
[0] m
int main(void){
char letter[3][3] = { {'m', 'C'}, [1] t c e
{'t', 'c', 'e'},
{'p', 'n'}, };
[2] p n
cout << letter[0][1] <<
letter[0][0] << letter[2][0] <<
letter[1][2] << endl; Cmpe
return 0;
}
Trace the following program and write the output
#include <iostream> 1 2
using namespace std;

int main(void){ 4 5
int a[10][10], x=0;
for( int i=0; i<3 ;i++ ){
for( int h=0; h<2; h++ ){
a[i][h] = x+1;
x = x+1;
if( a[i][h] == 3 )
break;
cout<< a[i][h] << "\t";
}
cout<<endl;
}

return 0;
}
Exercise 5
• Write a program which will store the following values in a 2D array
and display “1” for an odd number and “0” for an even number.

1 2 3 4 5 1 0 1 0 1
6 7 8 9 10 0 1 0 1 0
11 12 13 14 15 1 0 1 0 1
16 17 18 19 20 0 1 0 1 0
21 22 23 24 25 1 0 1 0 1

You might also like