You are on page 1of 13

AST 131

Programming with C/C++


Lecture-08: Matrix I
Tahmina Akter

Lecturer of Applied Statistics,


Institute of Statistical Research and Training (ISRT),
University of Dhaka, Bangladesh.
Email: takter2@isrt.ac.bd

May, 2018

ogramming with
Task
2x2 Matrix input
#include <stdio.h>
int main(void)
{
int i, j;
int A[2][2]={{1,5}, {2,9}};
for(i=0; i<2; i++){
for(j=0; j<2; j++){
printf("%d\t", A[i][j]);
}
printf("\n");
}
return 0;
}
Matrix input
#include <stdio.h>
int main(void)
{
int m, n, i, j;
printf("Enter the number of rows and columns of A
matrix\n");
scanf("%d %d", &m, &n);
int A[m][n];
printf("Enter the elements of A matrix \n");
for(i=0; i<m; i++){
for(j=0; j<n; j++){
scanf("%d", &A[i][j]);
}

}
Matrix input
for(i=0; i<m; i++){
for(j=0; j<n; j++){
printf("%d\t", A[i][j]);
}
printf("\n");
}
return 0;
}
Sum of two Matrix
#include <stdio.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\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\n");


Sum of two Matrix cont..
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);

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

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


for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
return 0;
}
Trace of matrix
#include<stdio.h>
#include<math.h>
int main(){

int a[3][3],i,j;
int sum=0;

printf("Enter the 9 elements of matrix: ");


for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
Trace of matrix
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
printf("The trace is: \n\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
if(i==j){
sum=sum+a[i][j];
}
}
}
printf("The trace is: \n\n");
printf("%d\t", sum);
return 0;
}
Matrix multiplication
#include <stdio.h>
int main()
{
int m, n, p, q, i, j, 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\n");
for(i=0; i<m; i++){
for(j=0; j<n; j++){
scanf("%d", &first[i][j]);
}
}
Matrix multiplication
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\n");
}

for(i=0; i<p; i++){


for(j=0; j<q; j++){
scanf("%d", &second[i][j]);
}
}
Matrix multiplication
for(i=0; i<m; i++){
for(j=0;j<q; j++){
for(k=0;k<p;k++){
sum=sum+first[i][k]*second[k][j];
}
multiply[i][j]=sum;
sum=0;
}
}
printf("Product of entered matrices:-\n");
for(i=0;i<m;i++){
for(j=0;j<q;j++){
printf("%d\t",multiply[i][j]);
}
Matrix multiplication
printf("\n");
}
return 0;
}

You might also like