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

Lesson 8 Arrays_2D

The document explains multiple dimension arrays, focusing on two-dimensional arrays as a way to represent tables and matrices. It provides examples of declaring, initializing, and accessing elements in two-dimensional arrays, as well as their applications in storing data such as student marks. Additionally, it discusses the disadvantages of arrays, including static memory allocation and limitations on data types.

Uploaded by

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

Lesson 8 Arrays_2D

The document explains multiple dimension arrays, focusing on two-dimensional arrays as a way to represent tables and matrices. It provides examples of declaring, initializing, and accessing elements in two-dimensional arrays, as well as their applications in storing data such as student marks. Additionally, it discusses the disadvantages of arrays, including static memory allocation and limitations on data types.

Uploaded by

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

ARRAYS

Multiple Dimension Arrays


Multiple Dimension Arrays

A multiple dimension array is an array that


has two or more dimensions.
Two dimensional arrays are the simplest type
of multiple dimension arrays. They can be
used to represent tables of data, matrices,
and other two dimensional objects.
One of the most obvious example of a two
dimensional array is a table.
How will you store a table or
matrix?

Item1 Item2
Item3

Shop1 310 275 365


Shop2 210 190 325
Shop3 405 235 240
Shop4 260 300 380

To store this data, create 2-D array like


int arr[4][3];
Multi-dimensional ARRAYS, Cont.
 When might we use a multi-dimensional
array?

 Array of names (i.e., array of strings)


 This generalizes to a list of lists
 Other examples,
Checkerboard
Matrices, lists of vectors
Two Dimensional Arrays

We can see how a two dimensional array looks


like a table or matrix.
Thus, we can represent a two dimensional
array as rows and columns.
To declare a two-dimensional array:

(data type) array_name[# of rows] [# of columns];


2-Dimensional Arrays

Can be illustrated as a table:


Each row is a separate array.
For ex.

col 0 col 1 col 2 col 3


row 0 1 2 3 4
row 1 5 6 7 8
row 2 9 10 11 12
Two Dimensional Arrays Declarations

Therefore, a two dimensional array of


integers, named x, with 3 rows and 4 columns
would be declared as:
int x[3][4];

A two dimensional array of characters,


named c, with 119 rows and 2 columns would
be declared as:
char c[119][2];
2-D array initialization
 int table[2][3] = {0,0,0,1,1,1};
 Can also be done as:
int table[2][3] = {{0,0,0},{1,1,1}};
When array is explicitly initialized with all values,
first dimension in its declaration can be skipped
Example, array can be declared as:
int table[][3] = {{0,0,0},{1,1,1}};
But
int table[2][]={0,0,0,1,1,1}; // error

If some values are missing in initializer, they are set


to 0
Two-Dimensional Arrays
int MAXSTU =3;
int MAXSUB =4;
int table[MAXSTU][MAXSUB];
Int row, col;
for (row = 0; row < MAXSTU; row++)
for (col = 0; col < MAXSUB; col++)
scanf (“%d”,&table[row][col])
table
0 1 2 3

table[0] 0 1 2 3 0

table[1] 1 2 3 4 1
table[2]
2 3 4 5 2
Initialization of Multiple Dimension
Arrays
char board[3][3] = {‘X’,‘X’,‘O’,‘O’,‘X’, ‘O’,‘X’,‘O’,‘X’};
Thus, this declaration will set
 board[0][0] to ‘X’,
 board[0][1] to ‘X’,
 board[0][2] to ‘O’,
 board[1][0] to ‘O’,
 board[1][1] to ‘X’,
 board[1][2] to ‘O’,
 board[2][0] to ‘X’,
 board[2][1] to ‘O’,
 board[2][2] to ‘X’.
Multi-dimensional ARRAYS
 int daytab[2][13] = {
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31} };
 Note initialization of arrays within an array.
 How to access an individual element
 daytab[1][4] /* 2 nd row 5th element ie30*/
 NOTE – the following is wrong!
daytab[1,4] /* wrong! */
Storing the marks of all students in all
subjects in a two- dimensional array
( subject no.)
[0] [1] [2] [3]

( student no.) marks[0] 45 65 80 66


marks[1] 65 70 63 72
marks[2] 55 75 59 64

Here the marks are stored in an array marks[students]


[subjects]
i.e. each row contains the marks of all subjects of one
student,each column contains the marks of all the
students for one subject.
Storing the marks of all students in all
subjects in a two- dimensional array
/* Program to get the inputs of marks of all the
students in all their subjects */ [0] [1] [2] [3]
marks[0] 45 65 80 66
# define MAXSTU 3 marks[1] 65 70 63 72
# define MAXSUB 4
marks[2] 55 75 59 64
main( )
{ int marks[MAXSTU][MAXSUB], sum[MAXSTU];
int students,subjects;

/* taking the input of all the marks of all students */


for (students = 0; students < MAXSTU; students++)
for (subjects = 0; subjects < MAXSUB; subjects++)
scanf (“%d”, &marks[students][subjects]); }
Highest mark by any student in any
subject
( subject no.)
[0] [1] [2] [3]

( student no.) marks[0] 45 65 80 66


marks[1] 65 70 63 72
marks[2] 55 75 59 64

Highest 80

Here the marks are stored in an array marks[students]


[subjects]
i.e. each row contains the marks of all subjects of one
student.And each column contains the marks of all the
students for one subject.
Highest mark by any student in any subject
# define MAXSTU 3
# define MAXSUB 4
main( )
{
int marks[MAXSTU][MAXSUB], highest;
int students,subjects;
/* to find the maximum of all the marks scored */
highest = 0;
for (students = 0; students < MAXSTU; students++)
{
for (subjects = 0; subjects < MAXSUB; subjects++ )
{
if (marks[students][subjects]> highest)
highest = marks[students][subjects];
} }
printf (“the highest of all the subject is %d”, highest);
}
Highest marks scored in each individual
subject by any student
# define MAXSTU 120
# define MAXSUB 6
main( )
{ int marks[MAXSTU][MAXSUB], highest[MAXSUB];
int students,subjects;
/* to find the highest marks scored in each subject */
for (subjects = 0; subjects < MAXSUB; subjects++ )
{ highest[subjects] = 0;
for (students = 0; students < MAXSTU; students++)
{ if (marks[students][subjects]> highest[subjects])
highest[subjects] = marks[students][subjects];
printf (“the highest of the subject number %d is %d”,
(subjects+1),highest[subjects]);
}}
DISADVANTAGE OF ARRAYS
•Memory allocation of array is static:

Maximum size (maximum number of elements)


requested by the programmer would be reserved in
the memory irrespective of the usage of the number
of elements by the user.The memory space that is
unused is wasted. LESS RESOURCE UTILIZATION.
For example, int test[30]

•Different data types could not be stored in an array

You might also like