5/10/2016

Matrix in C programming

1.A C program to add two matrices.

#include<stdio.h>

#include<conio.h>

int main()

{

  int m, n, c, d, first[10][10], second[10][10], sum[10][10];


  printf("Enter the number of rows and columns of matrix\n");


  scanf("%d%d", &m, &n);

  printf("Enter the elements of first matrix(row wise)\n");


  for( c = 0 ; c < m ; c++ )

  for( d = 0 ; d < n ; d++ )

  scanf("%d", &first[c][d]);


  printf("Enter the elements of second matrix(row wise)\n");


  for( c = 0 ; c < m ; c++ )

  for( d = 0 ; d < n ; d++ )

  scanf("%d", &second[c][d]);


  for ( c = 0 ; c < m ; c++ )

  for ( d = 0 ; d < n ; d++ )

  sum[c][d] = first[c][d] + second[c][d];

/* Matrix addition */


  printf("Sum of entered matrices:-\n");


  for( c = 0 ; c < m ; c++ )

  {

  for ( d = 0 ; d < n ; d++ )

  printf("%d\t", sum[c][d]);


  printf("\n");

  }


  return 0;

}


Output:
Enter the number of rows and columns of matrix
3 3
Enter the elements of first matrix(row wise)
2 3 4 5 6 7 8 9 1
Enter the elements of second matrix(row wise)
4 5 6 7 8 9 1 2 3
Sum of entered matrices:-
5         8         10
12         14         16
9         11         4



2.A C program to subtract matrices.

#include<stdio.h>
int main()
{
  int m, n, c, d, first[10][10], second[10][10], difference[10][10];

  printf("Enter the number of rows and columns of matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix(row wise)\n");

  for (c = 0; c < m; c++)
  for (d = 0 ; d < n; d++)
  scanf("%d", &first[c][d]);

  printf("Enter the elements of second matrix(row wise)\n");

  for (c = 0; c < m; c++)
  for (d = 0; d < n; d++)
  scanf("%d", &second[c][d]);

  for (c = 0; c < m; c++)
  for (d = 0; d < n; d++)
  difference[c][d] = first[c][d] - second[c][d];
/* Subtract Matrices*/

  printf("Difference of entered matrices:-\n");

  for (c = 0; c < m; c++)
{
  for (d = 0; d < n; d++)
  printf("%d\t",difference[c][d]);

  printf("\n");
}
 return 0;
 }

Output:
Enter the number of rows and columns of matrix
3 3
Enter the elements of first matrix(row wise)
2 3 4 5 6 7 8 9 1
Enter the elements of second matrix(row wise)
4 5 6 7 8 9 1 2 3
Difference of entered matrices:-
-2         -2        -2
-2        -2         -2
7         7         -2


3.A C program which gives the transpose matrix of a entered matrix. 

#include<stdio.h>
int main()
{
  int m, n, c, d, matrix[10][10], transpose[10][10];
  printf("Enter the number of rows and columns of matrix\n");
  scanf("%d%d",&m,&n);   printf("Enter the elements of matrix(row wise)\n");
  for( c = 0 ; c < m ; c++ )
{
  for( d = 0 ; d < n ; d++ )
{
  scanf("%d",&matrix[c][d]);
}
}

  for( c = 0 ; c < m ; c++ )
{
  for( d = 0 ; d < n ; d++ )
{
  transpose[d][c] = matrix[c][d];
  /* transpose by interchanging rows and columns */
}
}

  printf("Transpose of entered matrix :-\n");
  for( c = 0 ; c < n ; c++ )
{
  for( d = 0 ; d < m ; d++ )
{
  printf("%d\t",transpose[c][d]);
  printf("\n");
}
  return 0;
}

Output:
Enter the number of rows and columns of matrix
3 3
Enter the elements of matrix(row wise)
2 3 4 
5 6 7 
8 9 1
Transpose of entered matrix :-
2         5         8
3         6         9
4         7         1




4.A C program which accepts two matrix and multiply them.

#include<stdio.h>

int main()
{
  int m, n, p, q, c, d, k, sum = 0;
  int first[10][10], second[10][10], multiply[10][10];

  printf("Enter the number of rows and columns of first matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix(row wise)\n");

  for ( c = 0 ; c < m ; c++ )
  for ( d = 0 ; d < n ; d++ )
  scanf("%d", &first[c][d]);

  printf("Enter the number of rows and columns of second matrix\n");
  scanf("%d%d", &p, &q);

  if ( n != p )
  printf("Matrices with entered orders can't be multiplied with each other.\n");
  else
{
  printf("Enter the elements of second matrix(row wise)\n");

  for ( c = 0 ; c < p ; c++ )
  for ( d = 0 ; d < q ; d++ )
  scanf("%d", &second[c][d]);

  for( c = 0 ; c < m ; c++ )
{
  for( d = 0 ; d < q ; d++ )
{
  for( k = 0 ; k < p ; k++ )
{
  sum = sum + first[c][k]*second[k][d];
}

  multiply[c][d] = sum;
  sum = 0;
}
}

  printf("Product of entered matrices:-\n");

  for( c = 0 ; c < m ; c++ )
{
  for( d = 0 ; d < q ; d++ )
  printf("%d\t", multiply[c][d]);

  printf("\n");
}
}

  return 0;
}

Output:
Enter the number of rows and columns of first matrix
3 3
Enter the elements of first matrix(row wise)
2 3 4 5 6 7 8 9 1
Enter the number of rows and columns of second matrix
3 2
Enter the elements of second matrix(row wise)
4 5 6 7 8 9
product of entered matrices:-
58         67
112        130
94         112

Other Output:
Enter the number of rows and columns of first matrix
3 3
Enter the elements of first matrix(row wise)
2 3 4 5 6 7 8 9 1
Enter the number of rows and columns of second matrix
2 3
Matrices with entered orders can't be multiplied with each other.

_________________________




Please post comment.Also you can post programming related problems.




No comments:

Post a Comment