0% found this document useful (0 votes)
7 views13 pages

10 - C Basics - Arrays (Part 2)

The document explains the concept of multidimensional arrays in C, particularly focusing on two-dimensional arrays and their initialization. It provides examples of how to declare, access, and process these arrays using nested loops, as well as how to pass them to functions. Additionally, it discusses memory size calculation for arrays and includes code snippets for practical implementation.

Uploaded by

rosahshhsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views13 pages

10 - C Basics - Arrays (Part 2)

The document explains the concept of multidimensional arrays in C, particularly focusing on two-dimensional arrays and their initialization. It provides examples of how to declare, access, and process these arrays using nested loops, as well as how to pass them to functions. Additionally, it discusses memory size calculation for arrays and includes code snippets for practical implementation.

Uploaded by

rosahshhsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

C basics

Multi-dimensional arrays
Multidimensional Arrays
Arrays can be multidimensional.
You can think of multidimensional
arrays as "arrays of arrays." 0,0 0,1 0,2

char board[3][3];
board[1][1] = 'o'; x x
board[0][0] = 'x';
1,0 1,1 1,2
board[2][0] = 'o';
board[0][2] = 'x';

Here, we declared and initialized


o
a 2-dimensional array tic-tac-toe 2,0 2,1 2,2
board.
o
Multidimensional Arrays
Accessing Multidimensional Array Elements
This code uses nested for loops to print out all elements of the
tic-tac-toe board array.

// print out all elements

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


{
for (int j = 0; j < 3; j++)
{
Printf(“%d”, board[i][j]);
}
}
2 Dimensional Arrays
A 2-dimentional array a which contains three rows and four
columns can be represented as:
2 Dimensional Arrays

A two dimensional array is the same as several identical arrays


put together. It is a matrix that consists of a certain number of
rows and columns.

For example:
const int NumRows = 3; 0 1 2 3 4 5 6
const int NumCols = 7; 0
int Array[NumRows][NumCols]; 1
2
Where:
Array[2][5] is the 3rd value (row) in 6th column ..
2 Dimensional Arrays

An one-dimensional array is usually processed via a for loop.


Similarly, a two-dimensional array may be processed using two
nested loops.
0 1 2 3 4 5 6
const int NumCols = 7; 0 0 0 0 0 0 0 0
int Array[ NumCols ];
for ( int Col = 0; Col < NumCols; Col++ )
{
Array[ Col ] = 0;
}
2 Dimensional Arrays
A one-dimensional array is usually processed via a for loop.
Similarly, a two-dimensional array may be processed using two
nested loops. 0 1 2 3 4 5 6
0 0 0 0 0 0 0 0
const int NumRows = 3; 1 0 0 0 0 0 0 0
const int NumCols = 7;
int Array[ NumRows ][ NumCols ]; 2 0 0 0 0 0 0 0
for ( int Row = 0; Row < NumRows ; Row++ )
{
for ( int Col = 0; Col < NumCols; Col++ )
{
Array[ Row ][ Col ] = 0;
}
}
2 Dimensional Arrays

A one dimensional array can be initialized while declared as


follows:

int Array[ 7 ]={ 0, 0, 0, 0, 0, 0, 0 };

Also a two dimensional array can be initialized while


declared as follows:

int Array[ 3 ][ 7 ] = { { 0, 0, 0, 0, 0, 0, 0 }, {0, 0, 0, 0, 0, 0, 0 },


{ 0, 0, 0, 0, 0, 0, 0 } };
2 Dimensional Arrays

If you define a two dimensional array as follows:


int grades[ 3 ][ 6 ];
What is the size of this array in the memory?
There is in an operator in C++ that can yield the size of the
array:
Printf(“%d”, sizeof( grades )) ;
The output of this method in this case is:
sizeof( grades ) = 4 * 3 * 6 = 72
where 4 is the size of an integer, 3 is the number of rows,
and 6 is the number of columns.
Passing1-DArraytoafunction
#include <stdio.h>
int getMax(int arr[], int R)
{
int max = 0;
for(int i = 0; i < R; i++)
if(max < arr[ i ])
max = arr[ i ];
return max; This is called a function call
} getMax(array, 2)
int main() { The first parameter is a 1
int array[2]; dimensional array, and the
for(int i = 0; i < 2; i++) second parameter is an integer
scanf(“%d”, &array[ i ]); just like the function defined
Printf(“%d”, getMax(array, 2); above.
}
Passing a2-DArray to afunction
#include <stdio.h> We can also pass C as we pass R, or
const int C = 4; make them both constants too (but
int getMax(int arr[][C], int R) { these constants should be global, i.e.
can be accessed by both the main
int max = 0; and the getMax functions).
for(int i = 0; i < R; i++)
for(int j = 0; j < C; j++)
if(max < arr[i][j])
max = arr[i][j];
This is called a function call
return max;
} getMax(array, 3)
int main(){ The first parameter is a 2
int array[3][C]; for(int i dimensional array, and the
= 0; i < 3; i++) second parameter is an integer
for(int j = 0; j < C; j++) just like the function defined
scanf(“%d”, &array[ i]);
printf(“%d”, getMax(array, 2);
above.
}
Can sum two
matrixes?
Next

You might also like