CMP1401: Introduction To Programming With C: Ece Gelal Soyak Bahc Es Ehir Universitesi

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Multi-Dimensional Arrays Arrays as Function Parameters Summary

CMP1401: Introduction to Programming with C

0 1
14
Ece Gelal Soyak
Bahçeşehir Üniversitesi

P
C M
Week 10: Arrays(cont’d) and Multi-Dimensional Arrays

Dc. 15, 2020

Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 1 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

In this lecture...

0 1
4
More on arrays
Passing an array to a function as argument

Multi-dimensional arrays
P 1
Returning an array from a function (LATER when we learn about pointers)

C M
Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 2 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

What are Multi-Dimensional Arrays?

“Arrays of arrays”
0 1
Examples:
int coordinates[4][3] = {{0, 0, 0},
14
{1, 0, 1},
{1, 0, 5}
P
C M
{4, 7, 9}};

char color[4][10] = {“Blue”, “Red”, “Orange”, “Yellow”};

Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 3 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Two-Dimensional Arrays (2-D arrays)


Arrays that require two subscripts to identify a particular element
Often used to represent tables

1st subscript identifies element’s row; 2nd subscript identifies its


0 1
column

Example: int x[3][4];


14
P
C M
Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 4 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Memory Map of Multi-Dimensional Array


Memory map of matrix x, with 2 rows and 3 columns: int x[2][3];

0 1
14
P
C M
http://www.mattababy.org/∼belmonte/Teaching/CCC/CCC/transparent figure5.gif

Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 5 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Initializing Multi-Dimensional Arrays

Initializing 2-D array:

0 1
4
int a [3][4] = {
{0, 1, 2, 3} , /∗ initializers for row indexed by 0 ∗/

1
{4, 5, 6, 7} , /∗ initializers for row indexed by 1 ∗/
{8, 9, 10, 11} /∗ initializers for row indexed by 2 ∗/
};

Initializing 3-D array:


P
M
int test [2][3][4] = {

C };
{ {3, 4, 2, 3}, {0, −3, 9, 11}, {23, 12, 23, 2} },
{ {13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9} }

Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 6 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Example: 2-D Arrays in Memory

#include <stdio.h>
0 1
int main()
{

14
int arr [3][3]={10, 20, 30, 40, 50, 60, 70, 80, 90};
Output:

0x7ffed428ae80
printf (
printf (
”%p\n”, arr ) ;
”%p\n”, arr[0] ) ;
P 0x7ffed428ae80
0x7ffed428ae8c

M
printf ( ”%p\n”, arr[1] ) ;
10
printf ( ”%d\n”, arr [0][0] ) ;
}

C
Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 7 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Processing a 2-D Array

0 1
14
P
C M
Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 8 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Example: Initializing in Declarations

Print these arrays by rows:

0 1
4
int Array1 [2][3] = { {1, 2, 3} , {4, 5, 6} };
int Array2 [2][3] = { 1, 2, 3, 4, 5 };

1
int Array3 [2][3] = { {1, 2} , {4 } };

for ( int row=0; row<2; row++ )


{
P Rows of Array1:
123
456

}
{

C M
for ( int col=0; col <3; col++ )

printf ( ”%d ”, Array1[row][col ] ) ;

printf ( ” \n”) ;
Rows of Array2:
123
450
Rows of Array3:
120
400

Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 9 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Example - 1

1
#include <stdio.h>

0
int main()
{

4
int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } };
int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 };

1
int i , j ;
printf ( ”Values in array1 by row are:\n” ) ;
for ( i = 0; i < 2; i ++ )
{
for ( j = 0; j < 3; j ++ )
P
printf ( ”%d ”, array1[ i ][ j ] ) ;

{
C M
printf ( ” \n” ) ;

cout << ”\nValues in array2 by row are:” << endl;


for ( i = 0; i < 2; i ++ )

for ( j = 0; j < 3; j ++ )
printf ( ”%d ”, array2[ i ][ j ] ) ;
printf ( ” \n” ) ;
}
return 0;
Ece Gelal Soyak
} · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 10 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Output of Example-1

0 1
14
P
C M
Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 11 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Example-2

1
#include <stdio.h>

0
int main()
{

4
int a [3][2], i , j ;
for ( i =0; i <3; i ++ )

1
{
for ( j =0; j <2 ; j ++ )
Output:

P
{
a[ i ][ j ]= i ;
} 00
}
for ( i =0; i <3; i ++ )
{

} C M
for ( j =0; j <2 ; j ++ )

printf ( ”%d” , a[ i ][ j ] ) ;
11
22

printf ( ” \n” ) ;
}
return 0;
}
Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 12 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Example-3
#include <stdio.h>

1
int main()
{

0
int a [5][5], i , j ;
for ( i =0; i <5; i ++ )

4
{
for ( j =0; j <5; j ++ )

1
{
if ( i ==j )
a[ i ][ j ] = 1;

}
else
a[ i ][ j ] = 0;
P Output:
}
for ( i =0; i <5; i ++)
{

C M
for ( j =0; j <5 ; j ++)
{
printf ( ”%d” , a[ i ][ j ] ) ;
??? (try to guess!)

}
printf ( ” \n” ) ;
}
return 0;
Ece Gelal}Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 13 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Main Differences of Arrays from Other C Data Types

1
1. It is not possible to assign one array to another using an assignment
operator =.
Array variable’s name is nothing but a constant

4 0
Constants cannot be changed → assignments to array variables are illegal.

1
int a [] = {1, 2, 3, 4};
int b [] = {1, 2, 3, 5};

a = b;
a[3] = b [3];
P
// illegal in C and C++
// legal, assigning to array location

“passed by reference”)
M
2. An array passed as a parameter is not copied (array is treated as

C
Array name is a reference to first array location
Array name can be used to access any content by indexing
Array name can be used to change the array contents although array is not
explicitly passed by reference

Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 14 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Passing an Array to a Function (1/2)


#include <stdio.h>
void Change ( int list [], int numElts) // Empty brackets [ ] indicate parameter is an array
{

}
for ( int k=1; k<numElts; k++ ) list [k] += list [k−1];

void Print ( const int list [], int numElts )

0 1
4
{
for ( int k=0; k< numElts; k++) printf ( ”%d \n”, list [k] ) ;

1
}

P
int main()
{
const int SIZE = 5;

M
int numbers[SIZE];
for ( int k=0; k<SIZE; k++ ) numbers[k] = k+1;
printf ( ”Before\n −−−−−−−−\n” );

C
Print (numbers, SIZE); // Size of the array should be separately provided
printf ( ” \n After \n −−−−−−−−\n” );
Change (numbers, SIZE);
Print (numbers, SIZE);
return 0;
}

Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 15 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Passing an Array to a Function (2/2)


Output:

Before
− − − − −−
0 1
Identifier numbers is the location of array’s first
1
2
3
element

14
numbers does not change as a result of being
passed to function “Change”

P
4
5 But contents of numbers do change

M
After
Array name is “passed by value” -- like all
− − − − −−
parameters

C
1
3 But array itself behaves like “passed by
6 reference”
10
15
Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 16 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

const Parameters

The parameter for the function “Print” in previous slide is defined as a


constant array

0 1
4
The values stored in a constant array can be accessed

void Print ( const int list [], int numElts )


{
list [3] = 0;
1
But the entries of a constant array cannot be changed

P
// −−> causes compilation error!

M
}

C
This assignment generates a compilation error :
In function ‘Print’ :
assignment of read-only location ...

Attention! Careful when assigning a value to an arbitrary array index!

Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 17 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Array Size as a Parameter

Size of array parameter is not included in the formal parameter


May be passed as another parameter
0 1
May be stored in a global variable

const int CLASS SIZE = 220;


14
P
int Grades[CLASS SIZE];
int index = 0;

M
while (cin >> score && index<MAX GRADES)
{
Grades[index] = score;

}
index++;

C
Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 18 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Example:
#include <stdio.h>

double getAverage(int arr [], int size)


{
int i ;

0 1
4
double avg, sum = 0;

1
for ( i = 0; i < size; ++i) sum += arr[i ];

P
avg = sum / size;
return avg; Outputs:
}
Average value is: 214.400000
int main ()
{

C M
int balance[5] = {1000, 2, 3, 17, 50};
double avg;
avg = getAverage( balance, 5 ) ;
printf ( ”Average value is: %f ”, avg ) ;

return 0;
}
Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 19 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Call by Value on Array Items (Cheating!)

1
#include <stdio.h>
void Display (char ch)

0
{
printf ( ”%c ”, ch ) ;

4
}

1
int main()
{
char arr [] = { ’ a’ , ’ b’ , ’ c’ , ’ d’ , ’ e’ , ’ f ’ , ’ g’ , ’ h’ , ’ i ’ , ’ j ’ };
for ( int x=0; x<10; x++)
{
P
// Pass each element one by one using subscript

}
}
return 0;

C M
Display ( arr [x] ) ;

Output: a b c d e f g h i j

Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 20 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Passing Arrays to Functions as Parameters

When an array is an argument to a function, only the address of the


first element of the array is passed, not a copy of the entire array.

0 1
14
Empty brackets [ ] are used to indicate that the parameter is an array

Size of the array should be separately provided

P
May be passed as another parameter
May be the value of a global constant variable

C M
Using a const modifier for parameters is good, defensive
programming
Allows the compiler to catch accidental attempts to modify a parameter
Protects the values of the array entries from being modified

Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 21 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Exercise - 1

0
Write a program that gets the midterm, project, and final scores of 10 1
4
students from the user.

P 1
The midterm, project, and final will be weighted by 30%, 30%, and
%40, respectively. Accordingly, compute the final weighted scores of
all students.

C M
Suppose that minimum passing score is the average of final scores.
So, display the final score of each student along with the passing
information.

Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 22 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Exercise - 2

0 1
14
Write a program that gets two matrices of size 3x3 consisting of
double elements.

P
The program will add the matrices, and display the resulting matrix.

C M
Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 23 / 24
Multi-Dimensional Arrays Arrays as Function Parameters Summary

Exercise - 3

0 1
14
Write a program that gets two matrices of size 3x2 and 2x4,
respectively.

P
The program will multiply and display the resulting matrix.

C M
Ece Gelal Soyak · Bahcesehir University CMP1401 · Week 10 – Dec. 15, 2020 24 / 24

You might also like