CSEE1121: Computer Programming Lab: Objective
CSEE1121: Computer Programming Lab: Objective
Lab#07
Multi-Dimensional Arrays
Objective:
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
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.
#include<stdio.h>
int main()
{
/* 2D array declaration*/
int disp[3][5];
pg. 1
int 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},
};
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];
.....
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>
int main()
int num[2][2], i, j;
printf("Enter 4 numbers:\n");
scanf("%d", &num[i][j]);
displayNumbers(num);
return 0;
{
pg. 4
// Instead of the above line,
int i, j;
printf("Displaying:\n");
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. #________________
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)
Marks Obtained
pg. 10
LABORATORY SKILLS RUBRIC (Cognitive)
Total Marks: 40
Marks Obtained
pg. 11