Sie sind auf Seite 1von 9

Programs on 2-Dimensional Arrays (Matrices)

1 /* Program to find perform addition of two matrices using 2 dimensional arrays */


#include <stdio.h>
#include<conio.h>
#include <math.h>
void main( )
{
int m,n,i,j,p,q;
int a[10][10],b[10][10],c[10][10];
clrscr( );
printf("Enter the order of matrix A : ");
scanf("%d%d",&m,&n);
printf("Enter the order of matrix B : ");
scanf("%d%d",&p,&q);

if((m!=p) || (n!=q))
{
printf("cannot perform addition");
getch( );
exit(0);
}
printf("Enter the elements ( %d ) for matrix A",m*n);
for (i=0; i<m;i++)
for(j=0;j<n;j++)
scanf ("%d",&a[i][j]);

printf("Enter the elements ( %d ) for matrix B",p*q);


for (i=0; i<p;i++)
for(j=0;j<q;j++)
scanf ("%d",&b[i][j]);

printf("The elements of matrix A are\n");


for (i=0; i<m;i++)
{
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
printf("\n");
}

printf("The elements of matrix B are\n");


for (i=0; i<p;i++)
{
for(j=0;j<q;j++)
printf("\t%d",b[i][j]);
printf("\n");

1
}

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


for(j=0;j<q;j++)
c[i][j]=a[i][j]+b[i][j];

printf("\n sum of A and B matrices are\n");


for (i=0; i<m;i++)
{
for(j=0;j<q;j++)
printf("\t%d",c[i][j]);
printf("\n");
}
getch( );
}

2 /* Program to find product of two matrices using 2 dimensional arrays */

#include <stdio.h>
#include <math.h>
#include<conio.h>
void main( )
{
int m,n,i,j,p,q,k;
int a[10][10],b[10][10],mul[10][10];
clrscr( );
printf("Enter the order of matrix A : ");
scanf("%d%d",&m,&n);
printf("Enter the order of matrix B : ");
scanf("%d%d",&p,&q);

if(n!=p)
{
printf("multiplication not possible\n");
getch( );
exit( );
}

else
printf("Enter the elements ( %d ) for matrix A",m*n);
for (i=0; i<m;i++)
for(j=0;j<n;j++)
scanf ("%d",&a[i][j]);

printf("Enter the elements ( %d ) for matrix B",p*q);

2
for (i=0; i<p;i++)
for(j=0;j<q;j++)
scanf ("%d",&b[i][j]);

printf("The elements of matrix A are\n");


for (i=0; i<m;i++)
{
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
printf("\n");
}

printf("The elements of matrix B are\n");


for (i=0; i<p;i++)
{
for(j=0;j<q;j++)
printf("\t%d",b[i][j]);
printf("\n");
}

printf("\n product of A and B matrices are\n");

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


for (j = 0;j < q; j++)
{
mul[i][j] = 0;
for (k = 0;k < p; k++)
mul[i][j] = mul[i][j] + a[i][k] * b[k][j];
}

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


{
for(j=0;j<q;j++)
printf("\t%d",mul[i][j]);
printf("\n");
}
getch( );
}

3 /* Program to read to and print from 2 dimensional arrays */


#include <stdio.h>
#include<conio.h>
#include <math.h>
void main( )

3
{
int m,n,i,j;
int a[10][10];
clrscr( );
printf("Enter the order of matrix : ");
scanf("%d%d",&m,&n);

printf("Enter the elements ( %d ) for matrix A",m*n);


for (i=0; i<m;i++)
for(j=0;j<n;j++)
scanf ("%d",&a[i][j]);

printf("The elements of matrix are\n\n");


for (i=0; i<m;i++)
{
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
printf("\n");
}
getch( );
}

4/* Program to find transpose of a given matrix */


#include <stdio.h>
#include<conio.h>
#include <math.h>
void main()
{
int m,n,i,j;
int a[10][10],t[10][10];
clrscr();
printf("Enter the order of matrix : ");
scanf("%d%d",&m,&n);

printf("Enter the elements ( %d ) for matrix A",m*n);


for (i=0; i<m;i++)
for(j=0;j<n;j++)
scanf ("%d",&a[i][j]);

printf("The elements of matrix are\n\n");


for (i=0; i<m;i++)
{
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
printf("\n");
}

4
for (i=0;i<n;i++)
for(j=0;j<m;j++)
t[i][j]=a[j][i];

printf("The transpose matrix is\n\n");


for (i=0; i<n;i++)
{
for(j=0;j<m;j++)
printf("\t%d",t[i][j]);
printf("\n");
}
getch();
}

5/* Program to find sum of specified row, specified column and sum of all elements
of array and sum of principal diagonal elements if the matrix is a square matrix */
#include <stdio.h>
#include<conio.h>
#include <math.h>
void main()
{
int m,n,i,j,r,c,sumrow=0,sumcol=0,sumall=0,sumdiag=0;
int a[10][10],t[10][10];
clrscr();
printf("Enter the order of matrix : ");
scanf("%d%d",&m,&n);

printf("Enter the elements ( %d ) for matrix A",m*n);


for (i=0; i<m;i++)
for(j=0;j<n;j++)
scanf ("%d",&a[i][j]);

printf("The elements of matrix are\n\n");


for (i=0; i<m;i++)
{
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
printf("\n");
}

printf("Enter the row no.\n");


scanf("%d",&r);

//to find row sum


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

5
sumrow=sumrow+a[r-1][j];

printf("sum of elements of row %d is %d \n",r,sumrow);

printf("Enter the column no.\n");


scanf("%d",&c);

//to find row sum


for (i=0;i<m;i++)
sumcol=sumcol+a[i][c-1];

printf("sum of elements of column %d is %d \n",c,sumcol);

//to find sum of diagonal elements


if(m==n)
{
for (i=0; i<m;i++)
for(j=0;j<n;j++)
{
if(i==j)
sumdiag=sumdiag+a[i][j];
}
printf("sum of diagonal elements is %d \n",sumdiag);
}

//to find sum all


for (i=0; i<m;i++)
for(j=0;j<n;j++)
sumall=sumall+a[i][j];

printf("sum of all elements of array is %d",sumall);

getch();
}

6/*program to find whether a given square matrix is symmetric or not*/


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{

int a[10][10],at[10][10],k,i,j,m,n;
clrscr();

6
printf("enter the order of matrix\n");
scanf("%d %d",&m,&n);
if(m!=n)
{
printf(“Enter square matrix !!!\n”);
getch();

exit(0);
}
printf("enter the matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);

 printf("entered matrix is\n");


for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf(“\n”);
}

for(i=0;i<m;i++)
for(j=0;j<n;j++)
at[i][j]=a[j][i];

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(at[i][j]!=a[i][j])
k=1;
}
}

if(k==1)
printf("not symmetric");
else
printf("symmetric");
getch();
}

7/* program to arrange the elements of each row of a matrix in ascending order */
#include<stdio.h>
#include<conio.h>

7
#include<math.h>
void main( )
{
int m,n,i,j,p,q,t,k;
int a[4][4],b[3][3],c[5][5];
clrscr( );
printf("Enter the order of matrix A : ");
scanf("%d%d",&m,&n);
printf("Enter the elements ( %d ) for matrix A",m*n);
for (i=0; i<m;i++)
for(j=0;j<n;j++)
scanf ("%d",&a[i][j]);
printf("The elements of matrix A before sorting are\n");
for (i=0; i<m;i++)
{
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
printf("\n");
}
//sort
for (i=0; i<m;i++)
for(j=1;j<n;j++) //Sorting in each pass
if(a[i][j]<a[i][j-1]) //move largest element to the end of array
{
t=a[i][j];
a[i][j]=a[i][j-1];
a[i][j-1]=t;
}
printf("The elements of matrix A after sorting each row are\n");
for (i=0; i<m;i++)
{
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
printf("\n");
}
getch( );
}

8/* program to find norm of a matrix */

#include <stdio.h>
#include<conio.h>
#include <math.h>
void main( )
{

8
int m,n,i,j,norm,sum=0;
int a[10][10];
clrscr( );
printf("Enter the order of matrix : ");
scanf("%d%d",&m,&n);

printf("Enter the elements ( %d ) for matrix A",m*n);


for (i=0; i<m;i++)
for(j=0;j<n;j++)
scanf ("%d",&a[i][j]);

printf("The elements of matrix are\n\n");


for (i=0; i<m;i++)
{
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
printf("\n");
}

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


for(j=0;j<n;j++)
sum=sum+ a[i][j]*a[i][j];

norm=sqrt(sum);
printf(“Norm of the given matrix is %d”,norm);
getch( );
}

Das könnte Ihnen auch gefallen