MATRICES
In this Concept you will learn how to work with arrays in C language.
Array: An Array is a Collection of data that holds limited amount of data of same type.
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("The matrices can't be multiplied with each other.\n");
else
{
printf("Enter elements of second matrix\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 the matrices:\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}
Array: An Array is a Collection of data that holds limited amount of data of same type.
There are different types of arrays.
- One-dimensional array
- Multi-dimensional arrays
Array declaration : datatype arrayname[size];
Ex: int arr[5];//Now arr can hold five values.
Initialization of array:
for(i=0;i<5;i++)
{
scanf("%d",&arr[i]);
}
for printing
{
printf("%d",&arr[i]);
}
Multi dimensional arrays
Array declaration: datatype arrayname[size][size];
Ex: float arr[5][5];//Now arr can hold 25 values i.e, 5 ROWS and 5 COLOUMS
Initialization of two dimensional array:
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
scanf("%d",&a[i][j]);
}
}
for printing two dimensional array
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("%d",a[i][j]);
}
}
Write a program for addition of matrices
#include <stdio.h>
int main()
{
int i,j,n;
int a[5][5],b[5][5],c[5][5];
printf("Enter No.of rows and coloums\n");
//Here rows and coloums are treated as same
scanf("%d",&n);
printf("Here rows and coloums are treated as same\nn = %d\n",n);
printf("Enter the elements of first matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of second matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Sum of the entered matrices is\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j] = a[i][j]+b[i][j];
printf("%d\t",c[i][j]);
}
printf("\n");
}
return 0;
}
Subtraction of two matrices
#include <stdio.h>
int main()
{
int i,j,n;
int a[5][5],b[5][5],c[5][5];
printf("Enter No.of rows and coloums\n");
//Here rows and coloums are treated as same
scanf("%d",&n);
printf("Here rows and coloums are treated as same\nn = %d\n",n);
printf("Enter the elements of first matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of second matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Subtarction of the entered matrices is\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j] = a[i][j]-b[i][j];
printf("%d\t",c[i][j]);
}
printf("\n");
}
return 0;
}
Finding the transpose of a matrix
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
// Storing elements of the matrix
printf("\nEnter elements of matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
// Displaying the matrix a[][] */
printf("\nEntered Matrix: \n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ", a[i][j]);
if (j == c-1)
printf("\n\n");
}
// Finding the transpose of matrix a
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
transpose[i][j] = a[i][j];
}
// Displaying the transpose of matrix a
printf("\nTranspose of Matrix:\n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",transpose[j][i]);
if(j==r-1)
printf("\n\n");
}
return 0;
}
Multiplication of two matrices
#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 number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("The matrices can't be multiplied with each other.\n");
else
{
printf("Enter elements of second matrix\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 the matrices:\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}
Sum of the diagonal elements of matrix
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], sum=0;
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
for (c = 0; c < m; c++) {
for (d = 0 ; d < n; d++) {
if(c==d)
sum = sum+first[c][d] ;
}
}
printf("\nThe sum of diagonal elements of matrix is %d",sum);
return 0;
}
Comments
Post a Comment