Saturday 31 August 2013

TWO DIMENSIONAL ARRAYS | C Programming Language Tutorial pdf



TWO DIMENSIONAL ARRAYS:

There could be situations where a table of values will have to be stored.
Consider a student table with marks in 3 subjects.
=> The above table contains a total of 15 values.
=> We can think this table as a matrix consisting of 5 rows & 3 columns.
=> Each row represents marks of student # 1 in all (different) subjects.
=> Each column represents the subject wise marks of all students.
=> In mathematics we represent a particular value in a matrix by using two subscripts such as Vij.
=> Here V denotes the entire matrix Vij refers to the value in “ i ”th row and “ j ”th column.

EXAMPLE:
In the above table V23 refers to the value '80'.
=> C allows us to define such tables of items by using two-dimensional arrays.
Definition:
A list of items can be given one variable name using two subscripts and such a variable is called a two – subscripted variable or a two – dimensional array.
Two – Dimensional arrays can be declared as:
<data type> <array name>[row size] [column size] ;

=> The above table can be defined in 'C' as
int V[5][3];
Representation of two Dimensional array in memory.

Initializing Two- Dimensional Arrays:
=> Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their declaration with a list of initial values enclosed in braces.
int table[2] [3] = {0,0,0,1,1,1};
=> This initializes the elements of first row to zero and the second row to one.
=> This initialization is done row by row.
=> The above statement can be equivalently written as int table[2][3] = ?{0,0,0},{1,1,1}?;
we can also initialize a two – dimensional array in the form of a matrix as shown.
int table[2][3] = {
{0,0,0},
{1,1,1}
};
Commas are required after each brace that closes of a row, except in case of last row.
=> If the values are missing in an initializer, they are automatically set to zero.
Ex: int table [2] [3] = {
{1,1}, 1 1 0
{2}    2 0 0
};

This will initialize the first two elements of the first row to one,
=> The first element of the second row to two and all other elements to zero.
=> When all the elements are to be initialized to zero, the following short-cut method may be used.
int m[3][5] = { {0}, {0}, {0}};
=> The first element of each row is explicitly initialized to zero while the other elements are automatically initialized to zero.
=> The following statement will also achieve the same result int m[3][5] = { 0, 0 };

(1) Write a program to display the elements of two dimensional array.
# include<stdio.h>
# include<conio.h>
void main( )
{
int i,j;
int a[3][3] = { { 1,2,3}, {4,5,6}, {7,8,9}};
clrscr( );
printf(“elements of an array \n \n”);
for( i=0; i<3; i++)
{
for ( j=0; j<3; j++)
printf (“%d\t”, a[ i ][ j ]);
} /* end of inner for loop */
printf(“\n”);
} /* end of outer for loop */
getch( );
} /* end of main() function */
Output:
Elements of an Array
1 2 3
4 5 6
7 8 9

(2) Write a program to display 2-Dimensional array elements together with their addresses.
# include<stdio.h>
# include<conio.h>
void main( )
{
int i,j;
int a[3][3] = ?{1,2,3{,{4,5,6},{7,8,9}?;
clrscr( );
printf(“Array Elements and address \n \n”);
printf(“col-0 col -1 col-2 \n”);
prinf(“-------- -------- ------- \ n”);
printf(“Row 0\t”)
for( i=0; i<3; i++)
{
for( j=0; j<3; j++)
printf(“%d [%u]”, a[ i ][ j ], &a[ i ][ j ]);
printf(“ \n Row %d ”, i+1);
}
getch( );
}

Output:
Array Elements and address
              Col – 0   col – 1   col -2
Row 0    1[4052]  2[4054]   3[4056]
Row 1    4[4058]  5[4060]   6[4062]
Row 2    7[4064]  8[4066]   9[4068]

(3) Program to read the matrix of the order upto 10 x 10 elements and display the same in matrix form.
# include<stdio.h>
# include<conio.h>
void main( )
{
int i, j, row, col, a[10][10];
clrscr( ) ;
printf(“\n Enter Matrix Order upto (10 x 10) A :”);
scanf(“ %d %d ”, &row, &col);
printf(“\n Enter Elements of matrix A: \n”);
for( i=0; i<row ; i++)
{
for( j=0; j<col; j++)
{
scanf(“%d”, &a[ i ][ j ]);
}
}
printf(“ \n The matrix is: \n”);
for( i=0; i<row; i++)
{
for( j=0; j<col; j++)
{
printf(“ %d ”, a[ i ][ j ]);
}
printf(“\n”);
}
getch( );
}
Output :
Enter order of matrix upto (10 x 10) A : 3 3
Enter Elements of a matrix A :
3 5 8
4 8 5
8 5 4
The matrix is
3 5 8
4 8 5
8 5 4

(4) Program read the elements of the matrix of the order upto 10 x 10 & transpose its elements.
# include<stdio.h>
# include<conio.h>
void main( )
{
int i,j, row, col, a[10][10], b[10][10];
clrscr( );
printf(“ \n Enter order of matrix upto (10 x 10) A:”);
scanf(“ %d %d ”, &row, &col);
printf(“\n Enter Elements of matrix A: \n”);
for( i=0; i < row; i++)
{
for( j=0; j<col; j++)
scanf(“ %d ”, &a[ i ][ j ]);
}
/* transposing logic simply copying one matrix elements to another in reverse
order */
for( i=0; i < row; i++)
{
for( j=0; j < col; j++)
b[ j ][ i ]=a[ i ][ j ];
}
printf(“ \n The Matrix Transpose is \ n”);
for( i=0; i<row; i++)
{
for( j=0; j<col; j++)
printf(“%d”, b[ i ][ j ]);
printf (“ \ n”);
}
getch( ); }

Output:
Enter order of matrix upto (10 * 10) A : 3 3
Enter Elements of matrix A:
3 5 8
4 8 5
8 5 4
The Matrix Transpose is
3 4 8
5 8 5
8 5 4

(5) Program to perform addition and subtraction of two matrices. Whose orders are upto 10 x 10.
# include<stdio.h>
# include<conio.h>
main( )
{
int i,j,r1,c1, a[10][10], b[10][10];
clrscr( );
printf(“Enter Order of Matrix A & B upto 10 x 10:”);
scanf(“%d %d”, &r1, &c1);
printf(“Enter Elements of Matrix of A: \n”);
for( i=0; i < r1; i++)
{
for( j=0; j<c1; j++)
scanf(“ %d ”, &a[ i ][ j ]);
}
printf(“Enter Elements of Matrix of B: \ n”);
for( i=0; i < r1; i++)
{
for( j=0; j < c1; j++)
scanf(“ %d ”, &b[ i ][ j ]);
}
printf(“\n Matrix Addition \n”);
for( i=0; i < r1; i++)
{
for( j=0; j < c1; j++)
printf(“%d\t”, a[ i ][ j ] + b[ i ][ j ]);
printf (“ \n”);
}
printf(“n Matrix Subtraction/Difference \n”);
for( i=0; i < r1; i++)
{
for( j=0; j < c1; j++)
printf(“%d\t”, a[ i ][ j ] – b[ i ][ j ]);
printf(“\n”);
}
getch( );
}

Output:
Enter order of Matrix A & B upto 10 x 10 : 3 3
Enter Elements of Matrix of A:
4 5 8
2 9 8
2 9 4
Enter Elements of Matrix of B:
1 3 5
0 5 4
6 7 2
Matrix Addition
5 8 13
2 14 12
8 16 6
Matrix Subtraction
 3 2 3
 2 4 4
-4 2 2

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved