You are on page 1of 11

CSEE1121: Computer Programming Lab

Lab#07

Multi-Dimensional Arrays
Objective:

 Understand the use of Multi-Dimensional arrays in C


 Understand the declaration of two-dimensional arrays
 Usage of 2D arrays as function arguments

Two-dimensional array are those type of array, which has finite number of rows and finite number of
columns. The declaration form of 2-dimensional array is

Data_type Array_name [row size][column size];

The type may be any valid type supported by C. The rule for giving the array name is same as the
ordinary variable. The row size and column size should be an individual constant.

The following declares a two-dimensional 3 by 3 array of integers and sets the first and last elements to
be 10.

int matrix [3][3];


matrix[0][0] = 10;
matrix[2][2] = 10;

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

/* 2D array declaration*/
int disp[3][5];

/*Counter variables for the loop*/

pg. 1
int i, j;

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


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

printf("Enter value for disp[%d][%d]:", i, j);

scanf("%d", &disp[i][j]);

}
return 0;
}

Initialization of 2D Array
There are many ways to initialize two Dimensional arrays –

int disp[2][4] = {
{10, 11, 12, 13},

{14, 15, 16, 17}

};
OR

int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};
Things which you must consider while initializing 2D array –
You must remember that when we give values during one dimensional array declaration, we don’t need
to mention dimension. But that’s not the case with 2D array; you must specify the second dimension
even if you are giving values during the declaration. Let’s understand this with the help of few examples

/* Valid declaration*/
int abc[2][2] = {1, 2, 3 ,4 }
/* Valid declaration*/
int abc[][2] = {1, 2, 3 ,4 }
/* Invalid declaration – you must specify second dimension*/
int abc[][] = {1, 2, 3 ,4 }
/* Invalid because of the same reason mentioned above*/
int abc[2][] = {1, 2, 3 ,4 }

pg. 2
Store Data into 2D array
...
int abc[5][4];
.....

/*loop for first dimension which is 5 here*/


for(i=0; i<=4; i++)
{

/*loop for second dimension of array which is 4 in this example*/


for(j=0;j<=3;j++)
{

printf("Enter value for abc[%d][%d]:", i, j);

scanf(“%d”, &abc[i][j]);

}
In above example, I have a 2D array – abc of integer type. I have used nested for loops to store data.
Conceptually you can consider above array like this –

However, the actual representation of this array in memory would be something like

pg. 3
Passing 2D array to a function:
C does not really have multi-dimensional arrays, but there are several ways to simulate them. The way
to pass such arrays to a function depends on the way used to simulate the multiple dimensions:

One way is to use an array of arrays. This can only be used if your array bounds are fully determined at
compile time.
#include <stdio.h>

void displayNumbers(int num[2][2]);

int main()

int num[2][2], i, j;

printf("Enter 4 numbers:\n");

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

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

scanf("%d", &num[i][j]);

// passing multi-dimensional array to displayNumbers function

displayNumbers(num);

return 0;

void displayNumbers(int num[2][2])

{
pg. 4
// Instead of the above line,

// void displayNumbers(int num[][2]) is also valid

int i, j;

printf("Displaying:\n");

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

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

printf("%d\n", num[i][j]);

pg. 5
Lab Activities:
Activity 1:
Write a C-Program that gets a 4x4 2D array as input from user and display it on the Screen in Tabular
form shown below:

1834
2678
9171
2242

Activity 2:
Write a C-Program that gets two 3x3 Matrices as input from user and store in 2D arrays. Program will
add the two matrices and display the result on screen.

Activity 3:
Write a user defined function that gets a 3x3 matrix as input, computes and returns its transpose. In
main() ask the user to enter a 3x3 matrix and call the function. Main() will also display both the original
and the transpose matrix on Screen.

Activity 4:
Write a C-Program that will take two 2x2 matrices as input from user, check whether matrix
multiplication is possible; if so, than multiply the two matrices and display the result on screen.

pg. 6
Bonus Activity 1:
Write a C-Program that will take a 3x3 matrix as input from user and check if matrix is lower triangular
or not.

300

580

147

Bonus Activity 2:
Write a C-Program that will take a 3x3 matrix as input from user and check if matrix is identity matrix
or not.

100

010

001

Bonus Activity 3:
Write a C-Program that will define a 2D character array of size 10x50 and ask the user to enter names
of 10 students in the array. Program will also display name of the students on screen.

pg. 7
Design Lab # 7
Lab Report
Student Name:_____________________ Reg. #________________

Answer the following :-

1. Explain objective of the Lab and outline the Lab tasks

pg. 8
2. Show the memory map of a 3x3 character array in tabular form.

3. Write a user defined function that will take a 2D array as input and return its
maximum.

pg. 9
LABORATORY SKILLS RUBRIC (Psychomotor)

Total Marks: 100

Criteria Level 1 Level 2 Level 3 Level 4


Score (S)
(Max Marks) 0% ≤ S < 50% 50% ≤ S< 70% 70% ≤ S< 90% 90%≤ S ≤100%
Procedural Selects Selects and applies Selects and applies Selects and
Awareness inappropriate appropriate skills the appropriate applies
(20) skills and/or and/or strategies strategies and/or appropriate
strategies required by the task skills specific to the strategies and/or
required by the with some errors task without skills specific to
task significant errors the task without
any error
Practical Makes several Makes few critical Makes some non- Applies the
Implementation critical errors in errors in applying critical errors in procedural
(30) applying procedural applying procedural knowledge in
procedural knowledge knowledge perfect ways
knowledge
Safety Requires constant Requires some Follows safety Routinely follows
(10) reminders to reminders to follow procedures with only safety procedures
follow safety safety procedures minimal reminders
procedures
Use of Uses tools, Uses tools, Uses tools, Uses tools,
Tool/Equipment equipment and equipment and equipment and equipment and
(20) materials with materials with materials with materials with a
limited some competence considerable high degree of
competence competence competence
Participation Shows little Demonstrates Demonstrates Actively helps to
to Achieve commitment to commitment to commitment to identify group
Group Goals group goals and group goals, but group goals and goals and works
(10) fails to perform has difficulty carries out assigned effectively to
assigned roles performing roles effectively meet them in all
assigned roles roles assumed
Interpersonal Rarely interacts Interacts with other Interacts with all Interacts
Skills in positively within group members if group members positively with all
Group Work a group, even prompted spontaneously group members
(10) with prompting and encourages
such interaction in
others

Marks Obtained

pg. 10
LABORATORY SKILLS RUBRIC (Cognitive)

Total Marks: 40

Criteria Level 1 Level 2 Level 3 Level 4


Score
(Max. Marks) 0% ≤ S < 50% 50% ≤ S < 70% 70% ≤ S < 90% 90% ≤ S ≤ 100%
Introduction Very little Introduction is brief Introduction is nearly Introduction complete
(5) background with some minor complete, missing some and well-written;
information mistakes minor points provides all necessary
provided or background principles
information is for the experiment
incorrect
Procedure Many stages of the Many stages of the The procedure could be The procedure is well
(5) procedure are not procedure are entered more efficiently designed and all
entered on the lab on the lab report. designed but most stages stages of the
report. of the procedure are procedure are entered
entered on the lab report. on the lab report.
Data Record Data is brief and Data provides some Data is almost complete Data is complete and
(10) missing significant significant information but has some minor relevant. Tables with
pieces of and has few critical mistakes. units are provided.
information. mistakes. Graphs are labeled.
All questions are
answered correctly.
Data Analysis Data are presented Data are presented in Data are presented in Data are presented in
(10) in very unclear ways (charts, tables, ways (charts, tables, ways (charts, tables,
manner. Error graphs) that are not graphs) that can be graphs) that best
analysis is not clear enough. Error understood and facilitate
included. analysis is included. interpreted. Error understanding and
analysis is included. interpretation. Error
analysis is included.
Report Report contains Report is somewhat Report is well organized Report is well
Quality many errors. organized with some and cohesive but organized and
(10) spelling or grammatical contains some cohesive and contains
errors. grammatical errors. no grammatical errors.
Presentation seems
polished.

Marks Obtained

pg. 11

You might also like