Two Dimensional Arrays: An Element of A Two Dimensional Array Each Element Itself Is An Array of 8 Elements
Two Dimensional Arrays: An Element of A Two Dimensional Array Each Element Itself Is An Array of 8 Elements
CF Zero Semester
Two Dimensional Arrays
In C/C++, An array is a list of elements of the same type. Based on this definition we can define
a two dimensional array as an array consisting of elements of same type however each element
of a two dimensional array is also an array. See the diagram and explanation below
A Two Dimensional Array, Consisting of 4 elements where as each element is an array of 8 elements
Each element of a two dimensional array is accessed using double square brackets.
CFPLabExercisepreparedbyNaumanShamim Page1
Another way to look at two dimensional arrays
Keeping in view the subscripts of a two dimensional array, one can also consider a two
dimensional array as a matrix of rows and columns. The first subscript is used to access the row
while the second subscript is used to access the elements in that row
0 1 2 3 4 5 6 7
Row 0
Row 1
Row 2
Row 3
type : Anyoftheprimitivedatatypessuchasint,float,char
arrayName: Justlikenameofanyvariable
x : Numberofelementsofthetwodimensionalarrayornumberofrows
y : Numberofsubelementsofeachelementoftwodarrayorcolumns
Examples
int matrix[4][8] ;
Anarrayoffourelements,eachelementisanarrayof8integersoranarrayof4rowswitheachrow
having8integers(columns)
CFPLabExercisepreparedbyNaumanShamim Page2
Initializing Two Dimensional Arrays
Therefourpossiblewaystoinitializeatwodimensionalarray
1. Declaringandinitializingusingcompletelistofinitializerslists,andeachinitializerslistisalso
complete
2. Declaringandinitializingusingpartiallistofinitializerslists,andeachinitializerslistiscomplete
3. Declaringandinitializingusingcompletelistofinitializerslistswithinitializerslistispartial
4. Declaringwithoutsizeandcompletelistofinitializerslistswithinitializerslistispartial
5. Declaringwithoutsizeusinglistofpartiallistofinitializerslistswithinitializerslistispartial
Example
#include<stdio.h>
#include<conio.h>
main(){
int a[4][5]={
{10,20,30,40,50}, //Method 1
{95,90,80,70,60},
{18,19,11,23,34},
{13,14,15,16,17},
};
int b[4][5]={
{10,20,30,40,50}, //Method 2
{95,90,80,70,60} };
int c[4][5]={
{1,2,3,4,5}, //Method 3
{7,8,9},
{5},
{2,8,3,7}
};
CFPLabExercisepreparedbyNaumanShamim Page3
Note: In case of partial list, rest of the elements are initialized to
default value of the data type.
Example
#include<stdio.h>
#include<conio.h>
main(){
int a[4][5];
a[0][0]=10;
a[0][1]=20;
getch();
}
int a[4][5];
for(int i=0;i<4;i++)
for(int j=0;j<5;j++){
a[i][j]=(i+1)*j;
printf("\n a[%d][%d] =%d",i,j,a[i][j]);
}
getch();
}
Task-01
Declare and initialize a two dimensional array of integers, consisting of 5 rows with each row
having 3 elements or columns. You need to do this using
1. A complete list of initializers-list with each list having all elements
2. A partial list of initializers, where at least one list is missing and other lists have all elements
3. A partial list of initializers where at least one list is missing and other lists have missing elements
CFPLabExercisepreparedbyNaumanShamim Page4
Task-02
Write a program for the addition of matrices, do the following as directed
1. Create a two dimensional array A[4][4] , initialize it with initializers-lists
2. Create a two dimensional array B[4][4], initialize it with initializers-lists
3. Create a two dimensional array C[4][4]
4. Store the sum of A and B in C according to matrix addition , to achieve this you need to do the
following steps
a. Store sum of A[0][0] and B[0][0] in C[0][0] i.e. C[0][0]=A[0][0]+B[0][0]
b. Repeat for all elements of A and B and store results in C
Task-03
Modify the above program according to the following specification
1. Get the values of A and B from user using scanf()
2. Sum the matrices using loops
Task-04
Create a two dimensional array consisting of 4 rows and 4 columns, initialize the array from user input
using scanf, once the array is initialized do the following
1. Access each element of the array , if the value of the array element is even print its row number,
column number and value on the screen as a triplet i.e. (Row, Column, Value) suppose first
element of the array a[4][4] is 10 the program should print (0,0,10)
Task -05
Write a program that initializes a two dimensional array of 4 rows and 5 columns, the program should
1. Find the largest value in each row ,
2. Find the smallest value in each row
3. Find the sum of each row
The program should print the results as a triplet i.e. (Row Number, Largest Value, Smallest Value, Sum)
Task -06
Re-write the above program and find the smallest and largest number of the entire array, the program
should also find the sum of the entire two dimensional array
Taks-07
Suppose you are provided a two dimensional array consisting of N rows where each row consists of two
columns, the array consists of x and y value of N points in a two dimensional system.
CFPLabExercisepreparedbyNaumanShamim Page5
Write a program that finds the distance of each point from every other point using distance formula. The
program should also find the following
1. Two points that are farthest to each other
2. Two points that are closest to each other
/* sqrt example */
#include <stdio.h> /* printf */
#include <math.h> /* sqrt */
int main ()
{
double param, result;
param = 1024.0;
result = sqrt (param);
printf ("sqrt(%f) = %f\n", param, result );
return 0;
}
CFPLabExercisepreparedbyNaumanShamim Page6