Search This Blog

Thursday, October 13, 2011

Write a program to read a line of text and to convert it into a coded text by changing its characters to integer and adding k to each character

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  char line[80];
   6:  int i,l,k;
   7:  clrscr();
   8:  printf("Enter the line\n");
   9:  for(i=0;i<80;i++)
  10:  {
  11:  scanf("%c",&line[i]);
  12:  if(line[i]=='\n')
  13:  break;
  14:  }
  15:  l=i;
  16:  printf("Enter the num for coding: ");
  17:  scanf("%d",&k);
  18:  printf("\nCoded text is:\n");
  19:  for(i=0;i<l;i++)
  20:  printf("%4d",line[i]+k);
  21:  getch();
  22:  }

Program to read a line of text and display characters vertically.

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  char line[80];
   6:  int i,l;
   7:  printf("Enter the Line:\n");
   8:  for(i=0;i<80;i++)
   9:  {
  10:  scanf("%c",&line[i]);
  11:  if(line[i]=='\n')
  12:  break;
  13:  }
  14:  l=i;
  15:  printf("the characters in the line are\n");
  16:  for(i=0;i<l;i++)
  17:  printf("%3d %c\n",i,line[i]);
  18:  getch();
  19:  }

Write a program to read two matrices from the keyboard and find its product

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<stdlib.h>
   4:  void getproduct(int,int,int,float[10][10],float[10][10]);
   5:  void main()
   6:  {
   7:  int i,j,k,m1,n1,m2,n2;
   8:  float x[10][10],y[10][10];
   9:  printf("\nEnter the rows of matrix X: ");
  10:  scanf("%d",&m1);
  11:  printf("\nEnter the clomuns of matrix X: ");
  12:  scanf("%d",&n1);
  13:  printf("\nEnter the rows of matrix Y: ");
  14:  scanf("%d",&m2);
  15:  printf("\nEnter the columns of matrix y: ");
  16:  scanf("%d",&n2);
  17:  if(n1==m2)
  18:  k=n1;
  19:  else
  20:  {
  21:  printf("\nMultiplication of X and Y is not possible:");
  22:  getch();
  23:  exit(0);
  24:  }
  25:  printf("\nEnter the elements of first matrix:\n");
  26:  for(i=0;i<m1;i++)
  27:  for(j=0;j<n1;j++)
  28:  scanf("%f",&x[i][j]);
  29:  printf("\nEnter the elements of second matrix:\n");
  30:  for(i=0;i<m2;i++)
  31:  for(j=0;j<n2;j++)
  32:  scanf("%f",&y[i][j]);
  33:  getproduct(m1,k,n2,x,y);
  34:  getch();
  35:  }
  36:  void getproduct(int m1,int k,int n2,float x[10][10],float y[10][10])
  37:  {
  38:  int u,v,i;
  39:  float z[10][10];
  40:  for(u=0;u<m1;u++)
  41:  for(v=0;v<n2;v++)
  42:  {
  43:  z[u][v]=0.0;
  44:  for(i=0;i<k;i++)
  45:  z[u][v]+=x[u][i]*y[i][v];
  46:  }
  47:  printf("MATRIX Z=X*Y\n\n");
  48:  for(u=0;u<m1;u++)
  49:  {
  50:  for(v=0;v<n2;v++)
  51:  {
  52:  printf("%8.3f",z[u][v]);
  53:  }
  54:  printf("\n");
  55:  }
  56:  getch();
  57:  }

Program to pass a matrix as an argument to a function and display the matrix in the function. x[i][j]=2xi+3xj

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void displaymatrix(int nr,int nc,int x[10][10])
   4:  {
   5:  int i,j;
   6:  printf("The matrix is given below:\n\n");
   7:  for(i=0;i<nr;i++)
   8:  {
   9:  for(j=0;j<nc;j++)
  10:  {
  11:  printf("%d ",x[i][j]);
  12:  }
  13:  printf("\n");
  14:  }
  15:  }
  16:  void main()
  17:  {
  18:  int i,j,nr,nc,x[10][10];
  19:  printf("\nNo of rows:");
  20:  scanf("%d",&nr);
  21:  printf("\nNo of columns:");
  22:  scanf("%d",&nc);
  23:  for(i=0;i<nr;i++)
  24:  for(j=0;j<nc;j++)
  25:  x[i][j]=2*i+3*j;
  26:  displaymatrix(nr,nc,x);
  27:  getch();
  28:  }

Check equality of two matrices

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<stdlib.h>
   4:  void main()
   5:  {
   6:  int x[10][10],y[10][10],xr,xc,yr,yc,i,j,equal=1;
   7:  printf("Matrix X:\n\n");
   8:  printf("Number of rows: ");
   9:  scanf("%d",&xr);
  10:  printf("\nNumber of clomuns: ");
  11:  scanf("%d",&xc);
  12:  for(i=0;i<xr;i++)
  13:  {
  14:  for(j=0;j<xc;j++)
  15:  {
  16:  printf("\nEnter the element x[%d][%d]",i,j);
  17:  scanf("%d",&x[i][j]);
  18:  }
  19:  }
  20:  printf("Matrix Y\n\n");
  21:  printf("Number of rows: ");
  22:  scanf("%d",&yr);
  23:  printf("\nNumber of columns: ");
  24:  scanf("%d",&yc);
  25:  for(i=0;i<yr;i++)
  26:  {
  27:  for(j=0;j<yc;j++)
  28:  {
  29:  printf("\nEnter the element y[%d][%d]",i,j);
  30:  scanf("%d",&y[i][j]);
  31:  }
  32:  }
  33:  if(xr!=yr||xc!=yc)
  34:  {
  35:  printf("\nMatrices cant be campare No of rows or columns are diffrent:\n");
  36:  getch();
  37:  exit(1);
  38:  }
  39:  else
  40:  {
  41:  for(i=0;i<xr;i++)
  42:  for(j=0;j<xc;j++)
  43:  if(x[i][j]!=y[i][j])
  44:  {
  45:  equal=0;
  46:  break;
  47:  }
  48:  if(equal)
  49:  printf("\nMatrices are identical");
  50:  else
  51:  printf("\nmatrice are not identical");
  52:  }
  53:  getch();
  54:  }

The trace of the matrix is given by the sum of its elements of its major diagonal. Write a program to evaluate the trace of the above matrix

       



   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  int i,j,sum=0;
   6:  int x[4][4]={
   7:          {2,4,6,8},
   8:          {3,5,7,9},
   9:          {4,2,6,8},
  10:          {1,3,5,7},
  11:          };
  12:  for(i=0;i<4;i++)
  13:      {
  14:      for(j=0;j<4;j++)
  15:      {
  16:      if(i==j)
  17:      sum+=x[i][j];
  18:      printf("%5d",x[i][j]);
  19:      }
  20:      printf("\n");
  21:      }
  22:  printf("the trace of the matrix is: %d\n",sum);
  23:  getch();
  24:  }

X is a matrix whose elements x(i,j) is given by x(i,j)=2*i+3*j. Write a program generate the matrix and display the same

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  int i,j,r,c;
   6:  int x[10][10];
   7:  printf("Enter the number of rows: ");
   8:  scanf("%d",&r);
   9:  printf("Enter the number of columns: ");
  10:  scanf("%d",&c);
  11:  for(i=0;i<r;i++)
  12:      {
  13:      for(j=0;j<c;j++)
  14:      {
  15:      x[i][j]=2*i+3*j;
  16:      }
  17:      }
  18:  printf("The matrix is given below\n\n");
  19:  for(i=0;i<r;i++)
  20:  {
  21:  for(j=0;j<c;j++)
  22:  {
  23:  printf("%5d",x[i][j]);
  24:  }
  25:  printf("\n");
  26:  }
  27:  getch();
  28:  }