Wednesday, 8 January 2014

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

No comments:

Post a Comment