0% found this document useful (0 votes)
1 views5 pages

Matrix Program

Uploaded by

ullasinias
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views5 pages

Matrix Program

Uploaded by

ullasinias
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Non- lab program

Develop a C Program to accept rows & columns of a matrix and enter elements to the
matrix

#include <stdio.h>
#include <conio.h>
void main()
{
int rows, cols, matrix[5][5],i, j;
clrscr();
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);

// Input matrix elements


printf("Enter elements of the matrix:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
scanf("%d", &matrix[i][j]);
}
}
// output matrix elements
printf("Elements of the matrix:\n");
for ( i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("%d", matrix[i][j]);
}
printf(“\n”);
}
getch();
}
Non- lab program
Develop a C Program to accept rows & columns of a two matrix and enter elements to both
the matrix, then perform addition of matrix

#include <stdio.h>
#include <conio.h>
void main()
{
int rows, cols, a[5][5],i, j, b[5][5], c[5][5];
clrscr();
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);

// Input 1st matrix elements


printf("Enter elements of the matrix:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
scanf("%d", &a[i][j]);
}
}

// Input 2nd matrix elements


printf("Enter elements of the matrix:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
scanf("%d", &b[i][j]);
}
}

// calculate addition of matrix


printf("Enter elements of the matrix:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}

// output result matrix elements


printf("Elements of the matrix:\n");
for ( i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("%d", matrix[i][j]);
}
printf(“\n”);
}
getch();
}

Lab program
Develop a c program find transpose of a matrix

#include <stdio.h>
#include <conio.h>
void main()
{
int rows, cols, matrix[5][5], transpose[5][5],i,j;
clrscr();
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);

// Input matrix elements


printf("Enter elements of the matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}

// Compute transpose
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}

// Display original matrix


printf("\nOriginal Matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
// Display transpose matrix
printf("\nTranspose Matrix:\n");
for (i = 0; j < rows; j++) {
for (j = 0; j < cols; j++) {
printf("%d\t", transpose[i][j]);
}
printf("\n");
}
getch();
}

lab program
Develop a C Program to accept rows & columns of a two matrix and enter elements to both
the matrix, then perform Multiplication of matrix

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,p,q,i,j,k,a[5][5],b[5][5],c[5][5];
clrscr();
printf("Enter the size of first matrix:\n");
scanf("%d %d",&m,&n);
printf("Enter the size of second matrix:\n");
scanf("%d %d",&p,&q);
if(n!=p)
{
printf("Matrix Multiplication is not possible");
}
else
{
printf("\nEnter A Matrix elements:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter B Matrix elements:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Matrix Multiplication\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\nC Matrix elements are:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
getch();
}

You might also like