Showing posts with label 2 -dimensional array. Show all posts
Showing posts with label 2 -dimensional array. Show all posts

Saturday, 17 August 2013

C Programming Language Tutorial XII – Array (Multi-Dimensional-II)



Let us perform multiplication of matrix. In this case if you have x and y matrix then the elements of 1st row of x matrix get multiply with the all elements of y matrix column wise to get 1st row of resultant matrix. This process continues till the all elements of resultant matrix forms. Therefore process going on multiple times i.e. if you have a matrix of 3x3 then the whole process going on 27 times i.e. 3x3x3.

Let us solve the program by using function as used in previous lesson C Programming Language Tutorial XII –Array (Single Dimensional)




//Multiplication of matrix
#include<stdio.h>
#include<conio.h>

void getmatrix(int [][3]);  //function for scaning elements of matrix
void putmatrix(int [][3]);  //function for printing elements of matrix as output

int i,j;  //global varibles

void main()
{
 int x[3][3],y[3][3],z[3][3]={0};  //1st element of z matrix assign to zero
 int k;
 clrscr();

 printf("\nEnter 3 X 3 elements of x matrix\n");
 getmatrix(x);  //function calling for x matrix
 printf("\nEnter 3 X 3 elements of y matrix\n");
 getmatrix(y);  //function calling for y matrix

 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
for(k=0;k<3;k++)
   {
             z[i][j] = z[i][j] + x[i][k] * y[k][j];  //Given process performs for multiple times so k variable is taken. This k is set at row index of x and column index of y matrix.
    }
   }
  }

  printf("\nElements of x matrix:\n");
  putmatrix(x);   //function calling for x matrix
  printf("\nElements of y matrix:\n");
  putmatrix(y); //function calling for y matrix
  printf("\nResultant matrix:\n");
  putmatrix(z); //function calling for z matrix

  getch();
 }

 void getmatrix(int a[][3])
 {
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   scanf("%d",&a[i][j]);
  }
 }
 }
 void putmatrix(int a[][3])
 {
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
   {
            printf("%d ",a[i][j]);
   }
            printf("\n");
  }
 }

o/p:
Enter 3 X 3 elements of x matrix
1
1
1
2
1
2
1
1
1
Enter 3 X 3 elements of x matrix
2
2
2
1
1
1
2
2
2
Elements of x matrix:
1 1 1
2 1 2
1 1 1
Elements of y matrix:
2 2 2
1 1 1
2 2 2
Resultant matrix:
5 5 5
9 9 9
5 5 5

Sorting of matrix is done by three ways either row wise sorting, column wise sorting or sort all elements of matrix.
If let us assume x matrix has elements
4          8          7
3          2          9
2          3          1 then

 Above matrix is of 3x3 sized. Hence row wise and column wise sorting process performs at 27 times because every element compares 3 times. In case of sorting of all element performs 81 times because every element compares 9 times.

Let us see row wise sorting:
In following program user defined size matrix is used i.e. you first specify the fixed maximum size and then scan size at run time by user.

//Program to sort elements of matrix row wise
#include<stdio.h>
#include<conio.h>

void main()
{
 int x[30][30],r,c;   //Declaration of matrix with maximum size
 int i,j,k,temp;
 clrscr();

 printf("\nEnter no. of rows & columns: ");
 scanf("%d%d",&r,&c);    //scan no. of rows and columns (size) from user

 printf("\nEnter %d X %d elements of x matrix\n",r,c);
 for(i=0;i<r;i++)
 {
  for(j=0;j<c;j++)
  {
   scanf("%d",&x[i][j]);
}
 }

  printf("\nBefore row wise sorting elements of matrix:\n");
  for(i=0;i<r;i++)
  {
   for(j=0;j<c;j++)
   {
            printf("%d ",x[i][j]);
   }
            printf("\n");
  }

 //process
 for(i=0;i<r;i++)
 {
  for(j=0;j<c;j++)
  {
   for(k=0;k<r;k++)
             {
              if(x[i][j] < x[i][k])   //condition for ascending  sorting of rows

             {
              temp = x[i][j];
              x[i][j] = x[i][k];
              x[i][k] = temp;
             }
            }
          }
     }

  printf("\nAfter row wise sorting elements matrix:\n");
  for(i=0;i<r;i++)
  {
   for(j=0;j<c;j++)
   {
            printf("%d ",x[i][j]);
   }
            printf("\n");
  }

getch();
 }

o/p:

Enter no. of rows & columns: 3 3

Enter 3 X 3 elements of x matrix
3
5
1
3
8
7
5
4
2

Before row wise sorting elements of matrix:
3 5 1
3 8 7
5 4 2

After row wise sorting elements matrix:
1 3 5
3 7 8
2 4 5

//Program to print biggest & smallest elements of matrix
#include<stdio.h>
#include<conio.h>

void main()
{
 int x[30][30],r,c; //Declaration of matrix with maximum size
 int i,j,k,h,temp;
 clrscr();

 printf("\nEnter no. of rows & columns: ");
 scanf("%d%d",&r,&c);   //scan no. of rows and columns (size) from user

 printf("\nEnter %d X %d ele of x matrix\n",r,c);
 for(i=0;i<r;i++)
 {
  for(j=0;j<c;j++)
  {
   scanf("%d",&x[i][j]);
  }
 }

  printf("\nBefore sorting ele matrix:\n");
  for(i=0;i<r;i++)
  {
   for(j=0;j<c;j++)
   {
            printf("%d ",x[i][j]);
   }
            printf("\n");
  }

 //process
 for(i=0;i<r;i++)
 {
  for(j=0;j<c;j++)
  {
   for(k=0;k<r;k++)
   {
            for(h=0;h<c;h++)
            {
             if(x[i][j] < x[k][h])   //condition for ascending sorting
             {
              temp = x[i][j];
              x[i][j] = x[k][h];
              x[k][h] = temp;
             }
            }
   }
  }
 }
  printf("\nAfter sorting elements matrix:\n");
  for(i=0;i<r;i++)
  {
   for(j=0;j<c;j++)
   {
            printf("%d ",x[i][j]);
   }
            printf("\n");
  }
  printf("\nBiggest elements of matrix : %d",x[r-1][c-1]);
  printf("\nSmallest elements of matrix : %d",x[0][0]);

getch();
 }

o/p:
Enter no. of rows & columns: 3 3

Enter 3 X 3 elements of x matrix
54
34
11
23
87
76
54
33
44

Before sorting elements matrix:
54 34 11
23 87 76
54 33 44

After sorting elements matrix:
11 23 33
34 44 54
54 76 87

Biggest elements of matrix : 87
Smallest elements of matrix : 11

C Programming Language Tutorial XII – Array (Multi-Dimensional-I)




Two – Dimensional Array
-   Two dimensional arrays consist of number of rows and columns.
-   Two dimensional arrays are commonly referred as matrix.

Declaring two dimensional array

data-type array-name[rows][cols];

e.g. int x[3][3];

If you assign it as


x[3][3]={{11,22,33},{44,55,66},{77,88,99}};


The first value is at x[0][0]=11, x[0][1]=22…………….. x[2][2]=99.

If you watch carefully then you will notice that every cell has two subscripts (index/location values). So you can access these locations by using nested for loop. Outer loop is for rows and inner for columns.



Let us clear this by using simple program.

//scan & print elements of matrix
#include<stdio.h>
#include<conio.h>

void main()
{
 int x[3][3],i,j;        
clrscr();
 printf("\nEnter elements of x matrix");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   scanf("%d",&x[i][j]);
  }
 }

 printf("\nElements of x matrix");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("%d\t",x[i][j]);
  }
  printf("\n");
 }
 getch();
}
o/p:
Enter elements of x matrix
1
2
3
4
5
6
7
8
9
Elements of x matrix
1     2     3
4     5     6
7     8     9

In case of transpose of matrix rows and columns are interchanged.

Program to print transpose of matrix
//program to scan & print elements of matrix
#include<stdio.h>
#include<conio.h>

void main()
{
 int x[3][3],t[3][3],i,j;
 clrscr();

 printf("\nEnter 3 x 3 elements of matrix: ");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   scanf("%d",&x[i][j]);
     }
 }

 //process
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
     t[i][j] = x[j][i];          // columns and rows of x copy to rows and columns of t
  }
 }

 printf("\nTransposed elements of x matrix: ");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("%d ",t[i][j]);
  }
  printf("\n");
 }
 getch();
}
o/p:
Enter 3 x 3 elements of matrix:
1   2   3
4   5   6
7   8   9
Transposed elements of x matrix:
1   4   7
2   5   8
3   6   9

//prog to print sum of rows, columns and all elements of matrix
#include<stdio.h>
#include<conio.h>

void main()
{
 int x[3][3],i,j;
 int rsum=0,csum=0,sum=0;
 clrscr();

 printf("\nEnter elements of x matrix: ");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   scanf("%d",&x[i][j]);
  }
 }

 //process
for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   sum = sum + x[i][j];
   rsum = rsum + x[i][j];
   csum = csum + x[j][i];
  }
  printf("\nSum of row    = %d",rsum);
  printf("\nSum of column = %d",csum);
  rsum=0;
  csum=0;
 }
 printf("\nSum of all elements = %d",sum);
 getch();
 }
o/p:
Enter elements of matrix:
1
2
3
4
5
6
7
8
9
Elements of x matrix
1     2     3
4     5     6
7     8     9
Sum of row       = 6
Sum of column = 12
Sum of row       = 15
Sum of column = 15
Sum of row       = 18
Sum of column =  24
Sum of all elements = 45

Square matrix has two diagonals 1st and 2nd respectively. You can perform process on it. Here the next program is to print sum of 1st and 2nd diagonal.

//sum of 1st & 2nd diagonal
#include<stdio.h>
#include<conio.h>

void main()
{
  int x[4][4],i,j,dsum1=0,dsum2=0;
  clrscr();

  printf("Enter 4X4 elements of x mat\n");
  for(i=0;i<4;i++)
  {
             for(j=0;j<4;j++)
             {
                  scanf("%d",&x[i][j]);
             }
  }

  //process
  for(i=0;i<4;i++)
  {
    for(j=0;j<4;j++)
    {
        if(i == j)                 //In case of 1st diagonal the indices of row and column are                                             same hence whenever this condition becomes true then elements of 1st diagonal will add to dsum1
        {
             dsum1 = dsum1 + x[i][j];
        }
      if(i+j == 3)               //In case of 2nd diagonal the sum of indices of row and column is                                             less by 1 of given size hence whenever this condition becomes true then elements of 2nd diagonal will add to dsum2

      {
            dsum2 = dsum2 + x[i][j]; 
      }
    }
  }

printf("\nSum of 1st diagonal = %d",dsum1);
printf("\nSum of 2nd diagonal = %d",dsum2);
getch();
}

o/p:
Enter 4X4 elements of x mat
1 2 3 4
4 5 3 2
2 3 3 2
7 6 2 3
Sum of 1st diagonal = 12
Sum of 2nd diagonal = 17

Here you have to see one more type of sum i.e. triangle sum. Square matrix has upper and lower triangle. If you have matrix as follows




Look at above matrix the elements(1,2,3,5,6) locate under upper triangle and others are elements of lower triangle. Then now think of how to access elements to calculate sum of upper and lower triangle.

If you watched carefully then i=j will find that in case of upper triangle the index i is less than or equal to j and in case of upper triangle opposite position is appeared.

Let us check this logic through a program below:
//prog to cal. sum of upper & lower triangle of matrix
#include<stdio.h>
#include<conio.h>

void main()
{
  int x[4][4],i,j,upp=0,low=0;
  clrscr();

  printf("\nEnter 4X4 elements of x matrix\n");
  for(i=0;i<4;i++)
  {
     for(j=0;j<4;j++)
     {
            scanf("%d",&x[i][j]);
     }
 }

   printf("\n4X4 elements of x matrix\n");
  for(i=0;i<4;i++)
  {
     for(j=0;j<4;j++)
     {
            printf("%d\t",x[i][j]);
     }
            printf("\n");
 }

  for(i=0;i<4;i++)
  {
     for(j=0;j<4;j++)
     {
        if(i <= j)      //upper triangle
        {
            upp = upp + x[i][j];
         }
         if(i >= j)    //lower triangle
        {
             low = low + x[i][j];
        }
     }
  }

 printf("\nSum of elements of upper triangle = %d",upp);
 printf("\nSum of elements of lower triangle = %d",low);
 getch();
}

o/p:
Enter 4X4 elements of x matrix
1
1
1
1
2
2
2
2
1
1
1
1
3
3
3
3
4X4 elements of x matrix
1     1     1     1
2     2     2     2
1     1     1     1
3     3     3     3
Sum of elements of upper triangle = 15
Sum of elements of lower triangle = 20


Popular Posts