Thursday, January 26, 2012

Array [XV] 2-D Array – Matrix Addition-Multiplication




Input two Matrix, store the sum to an another Matrix.



#include <stdio.h>
#include <conio.h>
#define MAX 10

/* Matrix Addition*/

  void disp(int x[][MAX],int r,int c)
      {
           int i,j;
           for(i=0;i<r;i++)
              {
                   printf("\t");
                   for(j=0;j<c;j++)
                        {
                            printf("%3d ",x[i][j]);
                        }
                   printf("\n");
              }
      }
  void input(int x[][MAX],int r,int c)
      {
           int i,j;
           for(i=0;i<r;i++)
              {
                   for(j=0;j<c;j++)
                        {
                            printf("\n\tElement At [%d,%d] ; ",i,j);
                            scanf("%d",&x[i][j]);
                        }
               }
      }
  void madd(int x[][MAX],int y[][MAX],int z[][MAX],int r,int c)
      {
           int i,j;
           for(i=0;i<r;i++)
              {
                   for(j=0;j<c;j++)
                        {
                            z[i][j]=x[i][j]+y[i][j];
                        }
              }
      }

  void main()
      {
           int a[MAX][MAX],b[MAX][MAX],c[MAX][MAX],n,m;
           clrscr();
           printf("\n\tEnter Row Size [Below 10] : ");
           scanf("%d",&n);
           printf("\n\tEnter Column Size [Below 10] : ");
           scanf("%d",&m);
          printf("\n\tFirst Matrix : \n");
           input(a,n,m);
           printf("\n\tSecond Matrix : \n");
           input(b,n,m);
           printf("\n\tFirst Matrix : \n");
           disp(a,n,m);
           madd(a,b,c,n,m);
           printf("\n\tSecond Matrix : \n");
           disp(b,n,m);
           printf("\n\tResultant Matrix : \n");
           disp(c,n,m);
           getch();
      }

Input two Matrix, Multiply both and store in a third Matrix.


#include <stdio.h>
#include <conio.h>

/* Matrix Multiplication */

#define MAX 10
  void disp(int x[][MAX],int r,int c)
      {
           int i,j;
           for(i=0;i<r;i++)
              {
                   printf("\t");
                   for(j=0;j<c;j++)
                        {
                            printf("%3d ",x[i][j]);
                        }
                   printf("\n");
              }
      }
  void input(int x[][MAX],int r,int c)
      {
           int i,j;
           for(i=0;i<r;i++)
              {
                   for(j=0;j<c;j++)
                        {
                            printf("\n\tElement At [%d,%d] ; ",i,j);
                            scanf("%d",&x[i][j]);
                        }
               }
      }
  void mmult(int x[][MAX],int y[][MAX],int z[][MAX],int r,int c,int r1)
      {
           int i,j,k,s;
           for(i=0;i<r;i++)
              {
                   for(j=0;j<c;j++)
                        {
                            s=0;
                            for(k=0;k<r1;k++)
                                {
                                      s=s+x[i][k]*y[k][j];
                        }
                        z[i][j]=s;
              }
      }

      }
  void main()
      {
           int a[MAX][MAX],b[MAX][MAX],c[MAX][MAX],n,m,n1,m1;
           clrscr();
           printf("\n\tEnter Row Size for First Matrix [Below 10] : ");
           scanf("%d",&n);
           printf("\n\tEnter Column Size for First Matrix [Below 10] : ");
           scanf("%d",&m);
           printf("\n\tEnter Row Size for Second Matrix [Below 10] : ");
           scanf("%d",&n1);
           printf("\n\tEnter Column Size for Second Matrix [Below 10] : ");
           scanf("%d",&m1);
           if(n==n1 || n==m1)
              {
                   printf("\n\tFirst Matrix : \n");
                   input(a,n,m);
                   printf("\n\tSecond Matrix : \n");
                   input(b,n1,m1);
                   mmult(a,b,c,n,m1,n1);
                   printf("\n\tFirst Matrix : \n");
                   disp(a,n,m);
                   printf("\n\tSecond Matrix : \n");
                   disp(b,n1,m1);
                   printf("\n\tResultant Matrix : \n");
                   disp(c,n,m1);
              }
           else
              {
                   printf("\n\tInvalid Size for Multiplication..");
              }
           getch();
      }


Thursday, January 19, 2012

Array [XIV] 2-D Array Sorting




Sorting a 2-D array Row-wise


#include <stdio.h>
#include <conio.h>

//Sorting Columnwise

  void main()
      {
           int Arr[10][10],i,j,m,n,k,temp;
           printf("\n\tEnter Row Size [Below 10] : ");
           scanf("%d",&n);
           printf("\n\tEnter Column Size [Below 10] : ");
           scanf("%d",&m);
           printf("\n");
           for(i=0;i<n;i++)
              {
                   for(j=0;j<m;j++)
                        {
                            printf("\tElement at [%d,%d] : ",i,j);
                            scanf("%d",&Arr[i][j]);
                        }
              }
           printf("\n");
           printf("\n\tOriginal Array\n");
           for(i=0;i<n;i++)
              {
                   printf("\t");
                   for(j=0;j<m;j++)
                        {
                            printf("%3d",Arr[i][j]);
                        }
                   printf("\n");
              }
           for(k=0;k<n;k++)
              {
                   for(i=0;i<m;i++)
                        {
                            for(j=0;j<m-i-1;j++)
                                {
                                      if(Arr[k][j]>Arr[k][j+1])
                                                {
                                                          temp=Arr[k][j];
                                                          Arr[k][j]=Arr[k][j+1];
                                                          Arr[k][j+1]=temp;
                                                }
                                }
                        }
              }

           printf("\n\tSorted Array\n");
           for(i=0;i<n;i++)
              {
                   printf("\t");
                   for(j=0;j<m;j++)
                        {
                            printf("%3d",Arr[i][j]);
                        }
                   printf("\n");
              }
           getch();
      }


Sorting a 2-D array Column-wise


#include <stdio.h>
#include <conio.h>

//Sorting Rowwise

  void main()
      {
           int Arr[10][10],i,j,m,n,k,temp;
           clrscr();
           printf("\n\tEnter Row Size [Below 10] : ");
           scanf("%d",&n);
           printf("\n\tEnter Column Size [Below 10] : ");
           scanf("%d",&m);
           printf("\n");
           for(i=0;i<n;i++)
              {
                   for(j=0;j<m;j++)
                        {
                            printf("\tElement at [%d,%d] : ",i,j);
                            scanf("%d",&Arr[i][j]);
                        }
              }
           printf("\n");
           printf("\n\tOriginal Array\n");
           for(i=0;i<n;i++)
              {
                   printf("\t");
                   for(j=0;j<m;j++)
                        {
                            printf("%3d",Arr[i][j]);
                        }
                   printf("\n");
              }
           for(k=0;k<m;k++)
              {
                   for(i=0;i<n;i++)
                        {
                            for(j=0;j<n-i-1;j++)
                                {
                                      if(Arr[j][k]>Arr[j+1][k])
                                                {
                                                          temp=Arr[j][k];
                                                          Arr[j][k]=Arr[j+1][k];
                                                          Arr[j+1][k]=temp;
                                                }
                                }
                        }
              }

           printf("\n\tSorted Array\n");
           for(i=0;i<n;i++)
              {
                   printf("\t");
                   for(j=0;j<m;j++)
                        {
                            printf("%3d",Arr[i][j]);
                        }
                   printf("\n");
              }
           getch();
      }



Sorting a 2-D array


#include <stdio.h>
#include <conio.h>

//Overall Matrix Sorting
  void main()
      {
           int Arr[10][10],i,j,n=0,m=0,temp;
           clrscr();
           printf("\n\tEnter Nos. of Rows [Below 10] : ");
           scanf("%d",&n);
          printf("\n\tEnter Nos. of Cols [Below 10] : ");
           scanf("%d",&m);
           for(i=0;i<n;i++)
              {
                   for(j=0;j<m;j++)
                        {
                            printf("\n\tEnter Element at [%d,%d] : ",i,j);
                            scanf("%d",&Arr[i][j]);
                        }
              }
           printf("\n\tOriginal Array \n");
           for(i=0;i<n;i++)
              {
                   printf("\t");
                   for(j=0;j<m;j++)
                        {
                            printf("%3d",Arr[i][j]);
                        }
                   printf("\n");
              }
           for(i=0;i<(n*m)-1;i++)
              {
                   for(j=0;j<(n*m)-1;j++)
                        {
                            if(Arr[j/m][j%m]>Arr[(j+1)/m][(j+1)%m])
                                {
                                      temp=Arr[j/m][j%m];
                                      Arr[j/m][j%m]=Arr[(j+1)/m][(j+1)%m];
                                      Arr[(j+1)/m][(j+1)%m]=temp;
                                }
                        }
              }
           printf("\n\tAfter Sorting\n");
           for(i=0;i<n;i++)
              {
                   printf("\t");
                   for(j=0;j<m;j++)
                        {
                            printf("%3d",Arr[i][j]);
                        }
                   printf("\n");
              }
           getch();
      }


Thursday, January 12, 2012

Array [XIII] 2-D Array


Multi dimensional Arrays:
Where Single Dimensional Arrays has one column and multiple rows, multi-dimensional arrays has multiple rows as well as multiple columns. Multi-dimensional data structure also known as matrices. A multi-dimensional array with 3 rows and 3 columns is shown. There are two array subscripts. One subscript denotes the row & the other the column.
N[0][0]
N[0][1]
N[0][2]
N[1][0]
N[1][1]
N[1][2]
N[2][0]
N[2][1]
N[2][2]


Input a 3/3 Matrix, Print the sum of rows, Columns, Left Diagonal and Right Diagonal.



#include <stdio.h>
#include <conio.h>

/*Input a 3/3 Matrix, Print the sum of rows, Columns, Left Diagonal and Rigth Diagonal*/

  void main()
      {
           int ar[3][3];
           int i,j,c,sr=0,sc=0,sld=0,srd=0;
           clrscr();
           for(i=0;i<3;i=i+1)
              {
                   for(j=0;j<3;j=j+1)
                        {
                            printf("\n\tEnter Element for [%d][%d] : ",i,j);
                            scanf("%d",&ar[i][j]);
                        }
              }
           //sum of row, column, left diagonal, right diagonal
           printf("\n\n");
           for(i=0;i<3;i=i+1)
              {
                   sr=sc=0;
                   for(j=0;j<3;j=j+1)
                        {
                            printf("%3d",ar[i][j]);
                            sr=sr+ar[i][j]; //row sum;
                            sc=sc+ar[j][i]; //column sum
                            if(i==j)
                                {
                                      sld=sld+ar[i][j];//left diagonal
                                }
                            if(i+j==2) // 2 means n-1 right diagonal
                                {
                                      srd=srd+ar[i][j];
                                }
                        }
                   printf("\tSum of row  %d   Sum of column  %d\n",sr,sc);
              }
           printf("\n\n\tSum of left diagonal  %d   Sum of right diagonal  %d\n ",sld,srd);
           getch();
      }

Input a 3/3 Matrix, and Transpose it.

#include <stdio.h>
#include <conio.h>

//Input a 3/3 Matrix, and Transpose it

  void main()
      {
           int ar[3][3];
           int i,j,c;
           clrscr();
           for(i=0;i<3;i=i+1)
              {
                   for(j=0;j<3;j=j+1)
                        {
                            printf("\n\tEnter Element for [%d][%d] : ",i,j);
                            scanf("%d",&ar[i][j]);
                        }
              }
           printf("\nOriginal Array \n");
           for(i=0;i<3;i=i+1)
              {
                   for(j=0;j<3;j=j+1)
                        {
                            printf("%3d",ar[i][j]);
                        }
                   printf("\n");
              }
           //transpose in same array
           printf("\nAfter Transpose\n");
           for(i=0;i<3;i=i+1)
              {
                   for(j=0;j<i;j=j+1)
                        {
                            c=ar[i][j];
                            ar[i][j]=ar[j][i];
                            ar[j][i]=c;
                        }
              }
           for(i=0;i<3;i=i+1)
              {
                   for(j=0;j<3;j=j+1)
                        {
                            printf("%3d",ar[i][j]);
                        }
                   printf("\n");
              }
           getch();
      }