Vous êtes sur la page 1sur 6

Lab07

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

An element of a two Each element itself is


dimensional array an array of 8 elements

Element 0 Element 1 Element 2 Element 3 Element 4

A Two Dimensional Array, Consisting of 4 elements where as each element is an array of 8 elements

Subscripts for two dimensional arrays


For a single dimensional array a single set of square brackets [ ] is used, we call it single
subscript, for a two dimensional array two sets of square brackets are used i.e. [ ] [ ] .
The first subscript is used to point to elements of the two dimensional array , where as the
second subscript is used to access sub-elements.

Each element of a two dimensional array is accessed using double square brackets.

[0][0] [0][1] [0][2] [0][3]

Element 0 Element 1 Element 2 Element 3 Element 4

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][0]: Row 0 Column 0, First element of first row


[0][1]: Row 0 Column 1, Second element of first row
[0][2]: Row 0 Column 2, Third element of first row

0 1 2 3 4 5 6 7

Row 0

Row 1

Row 2

Row 3

Declaring Two Dimensional Array


Declaring a two dimensional array is similar to declaring a single dimensional array, the general
form is:
type arrayName [ x ][ y ];

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}
};

int d[4][5]={ //Mixed method


{1,2,3},
{4,5,6}
};

int e[][4] = { //Method 4


{1,2,3},
{4,5,6}
};
getch();
}
There are many possible ways to declare and initialize a two dimensional
array, finally a two dimensional array can be initialize using a loop or
constant assignment after the array has been declared. See example below

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;

printf(" First element of first row a[%d][%d] =%d",0,0,a[0][0]);


printf("\n Second element of the first row a[%d][%d]=%d",0,1,a[0][1]);

getch();
}

Initializing using loop


To initialize a two dimensional array usually two/nested loops are used. One loop passes through the
element of the array where as second loop passes through sub-element of each element. See example
below
#include<stdio.h>
#include<conio.h>
main(){

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

The distance formula

Program to Find the square root in C

/* 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

Vous aimerez peut-être aussi