- This program will read a two dimensional array (Matrix), number of rows (R) and number of columns (C) will be read through the User.
#include<stdio.h>
#define MAXROW 10 //macros
#define MAXCOL 10 //macros
int main()
{
int matrix[MAXROW][MAXCOL];
int i,j,r,c;
printf("Enter number of Rows :");
scanf("%d",&r);
printf("Enter number of Cols :");
scanf("%d",&c);
printf("\nEnter matrix elements :\n");
for(i=0;i< r;i++)
{
for(j=0;j< c;j++)
{
printf("Enter element [%d,%d] : ",i+1,j+1);
scanf("%d",&matrix[i][j]);
}
}
printf("\nMatrix is :\n");
for(i=0;i< r;i++)
{
for(j=0;j< c;j++)
{
printf("%d\t",matrix[i][j]);
}
printf("\n"); /*new line after row elements*/
}
return 0;
}
2.C Program to read a matrix and find sum, product of all elements of two dimensional (matrix) array
#include <stdio.h> #define MAXROW 10 #define MAXCOL 10 int main() { int matrix[MAXROW][MAXCOL]; int i,j,r,c; int sum,product; printf("Enter number of Rows :"); scanf("%d",&r); printf("Enter number of Cols :"); scanf("%d",&c); printf("\nEnter matrix elements :\n"); for(i=0;i< r;i++) { for(j=0;j< c;j++) { printf("Enter element [%d,%d] : ",i+1,j+1); scanf("%d",&matrix[i][j]); } } /*sum and product of all elements*/ /*initializing sun and product variables*/ sum =0; product =1; for(i=0;i< r;i++) { for(j=0;j< c;j++) { sum += matrix[i][j]; product *= matrix[i][j]; } } printf("\nSUM of all elements : %d \nProduct of all elements :%d",sum,product); return 0; }
This C program will read a Matrix (two dimensional arrays) and print the sum of all elements of each row.
#include <stdio.h>
#define MAXROW 10
#define MAXCOL 10
int main()
{
int matrix[MAXROW][MAXCOL];
int i,j,r,c;
int sum,product;
printf("Enter number of Rows :");
scanf("%d",&r);
printf("Enter number of Cols :");
scanf("%d",&c);
printf("\nEnter matrix elements :\n");
for(i=0;i< r;i++)
{
for(j=0;j< c;j++)
{
printf("Enter element [%d,%d] : ",i+1,j+1);
scanf("%d",&matrix[i][j]);
}
}
printf("\n");
/*sum of all rows*/
for(i=0;i< r;i++)
{
sum=0; /*initializing sum*/
for(j=0;j< c;j++)
{
printf("%d\t",matrix[i][j]); /*print elements*/
sum += matrix[i][j];
}
printf("\tSUM : %d",sum);
printf("\n"); /*after each row print new line*/
}
}
Output
Enter number of Rows :3
Enter number of Cols :3
Enter matrix elements :
Enter element [1,1] : 1
Enter element [1,2] : 2
Enter element [1,3] : 3
Enter element [2,1] : 4
Enter element [2,2] : 5
Enter element [2,3] : 6
Enter element [3,1] : 7
Enter element [3,2] : 8
Enter element [3,3] : 9
1 2 3 SUM : 6
4 5 6 SUM : 15
7 8 9 SUM : 24
TRANSPOSE OF THE MATRIX USING C LANGUAGE
#include <stdio.h>
#define MAXROW 10
#define MAXCOL 10
int main()
{
int matrix[MAXROW][MAXCOL];
int i,j,r,c;
printf("Enter number of Rows :");
scanf("%d",&r);
printf("Enter number of Cols :");
scanf("%d",&c);
printf("\nEnter matrix elements :\n");
for(i=0;i< r;i++)
{
for(j=0;j< c;j++)
{
printf("Enter element [%d,%d] : ",i+1,j+1);
scanf("%d",&matrix[i][j]);
}
}
/*Transpose a matrix */
printf("\nTranspose Matrix is :");
for(i=0;i< c;i++)
{
for(j=0;j< r;j++)
{
printf("%d\t",matrix[j][i]); /*print elements*/
}
printf("\n"); /*after each row print new line*/
}
return 0;
}
Output
Enter number of Rows :2
Enter number of Cols :3
Enter matrix elements :
Enter element [1,1] : 1
Enter element [1,2] : 2
Enter element [1,3] : 3
Enter element [2,1] : 4
Enter element [2,2] : 5
Enter element [2,3] : 6
Transpose Matrix is :
1 4
2 5
3 6
#include <stdio.h> #define MAXROW 10 #define MAXCOL 10 int main() { int matrix[MAXROW][MAXCOL]; int i,j,r,c; printf("Enter number of Rows :"); scanf("%d",&r); printf("Enter number of Cols :"); scanf("%d",&c); printf("\nEnter matrix elements :\n"); for(i=0;i< r;i++) { for(j=0;j< c;j++) { printf("Enter element [%d,%d] : ",i+1,j+1); scanf("%d",&matrix[i][j]); } } /*Transpose a matrix */ printf("\nTranspose Matrix is :"); for(i=0;i< c;i++) { for(j=0;j< r;j++) { printf("%d\t",matrix[j][i]); /*print elements*/ } printf("\n"); /*after each row print new line*/ } return 0; }
Output
Enter number of Rows :2
Enter number of Cols :3
Enter matrix elements :
Enter element [1,1] : 1
Enter element [1,2] : 2
Enter element [1,3] : 3
Enter element [2,1] : 4
Enter element [2,2] : 5
Enter element [2,3] : 6
Transpose Matrix is :
1 4
2 5
3 6
C program to read a matrix and print it's diagonals
#include <stdio.h> #define MAXROW 10 #define MAXCOL 10 int main() { int matrix[MAXROW][MAXCOL]; int i,j,r,c; printf("Enter number of Rows :"); scanf("%d",&r); printf("Enter number of Cols :"); scanf("%d",&c); printf("\nEnter matrix elements :\n"); for(i=0;i< r;i++) { for(j=0;j< c;j++) { printf("Enter element [%d,%d] : ",i+1,j+1); scanf("%d",&matrix[i][j]); } } /*check condition to print diagonals, matrix must be square matrix*/ if(r==c) { /*print diagonals*/ for(i=0;i< c;i++) { for(j=0;j< r;j++) { if(i==j) printf("%d\t",matrix[j][i]); /*print elements*/ else printf("\t"); /*print space*/ } printf("\n"); /*after each row print new line*/ } } else { printf("\nMatrix is not a Square Matrix."); } return 0; }
Output
First Run: Enter number of Rows :2 Enter number of Cols :3 Enter matrix elements : Enter element [1,1] : 1 Enter element [1,2] : 2 Enter element [1,3] : 3 Enter element [2,1] : 4 Enter element [2,2] : 5 Enter element [2,3] : 6 Matrix is not a Square Matrix. Second Run: Enter number of Rows :4 Enter number of Cols :4 Enter matrix elements : Enter element [1,1] : 1 Enter element [1,2] : 2 Enter element [1,3] : 3 Enter element [1,4] : 4 Enter element [2,1] : 5 Enter element [2,2] : 6 Enter element [2,3] : 7 Enter element [2,4] : 8 Enter element [3,1] : 9 Enter element [3,2] : 10 Enter element [3,3] : 11 Enter element [3,4] : 12 Enter element [4,1] : 13 Enter element [4,2] : 14 Enter element [4,3] : 15 Enter element [4,4] : 16 1 6 11 16
Program to multiply two matrices in C language
#include <stdio.h> #define MAXROW 10 #define MAXCOL 10 /*User Define Function to Read Matrix*/ void readMatrix(int m[][MAXCOL],int row,int col) { int i,j; for(i=0;i< row;i++) { for(j=0;j< col;j++) { printf("Enter element [%d,%d] : ",i+1,j+1); scanf("%d",&m[i][j]); } } } /*User Define Function to Read Matrix*/ void printMatrix(int m[][MAXCOL],int row,int col) { int i,j; for(i=0;i< row;i++) { for(j=0;j< col;j++) { printf("%d\t",m[i][j]); } printf("\n"); } } int main() { int a[MAXROW][MAXCOL],b[MAXROW][MAXCOL],result[MAXROW][MAXCOL]; int i,j,r1,c1,r2,c2; int sum,k; printf("Enter number of Rows of matrix a: "); scanf("%d",&r1); printf("Enter number of Cols of matrix a: "); scanf("%d",&c1); printf("\nEnter elements of matrix a: \n"); readMatrix(a,r1,c1); printf("Enter number of Rows of matrix b: "); scanf("%d",&r2); printf("Enter number of Cols of matrix b: "); scanf("%d",&c2); printf("\nEnter elements of matrix b: \n"); readMatrix(b,r2,c2); if(r1==c2) { /*Multiplication of two matrices*/ for(i=0;i< r1;i++) { for(j=0;j< c1;j++) { sum=0; for(k=0;k< r1;k++) { sum=sum + (a[i][k]*b[k][j]); } result[i][j]=sum; } } /*print matrix*/ printf("\nMatrix after multiplying elements (result matrix):\n"); printMatrix(result,r1,c1); } else { printf("\nMultiplication can not be done."); } return 0; }
Output
Enter number of Rows of matrix a: 3 Enter number of Cols of matrix a: 3 Enter elements of matrix a: Enter element [1,1] : 1 Enter element [1,2] : 2 Enter element [1,3] : 3 Enter element [2,1] : 4 Enter element [2,2] : 5 Enter element [2,3] : 6 Enter element [3,1] : 7 Enter element [3,2] : 8 Enter element [3,3] : 9 Enter number of Rows of matrix b: 3 Enter number of Cols of matrix b: 3 Enter elements of matrix b: Enter element [1,1] : 1 Enter element [1,2] : 1 Enter element [1,3] : 1 Enter element [2,1] : 2 Enter element [2,2] : 2 Enter element [2,3] : 2 Enter element [3,1] : 3 Enter element [3,2] : 3 Enter element [3,3] : 3 Matrix after multiplying elements (result matrix): 14 14 14 32 32 32 50 50 50
0 Comments:
Post a Comment