You are on page 1of 3

LAB EXERCISE SOLUTION

WEEK – 9

1. Program to accept and print 1_D array


Solution:
#include<stdio.h>
void main()
{
int a[10],range,i;
printf("enter the range value");
scanf("%d",&range);
printf("enter the input values");
for(i=0;i<=range;i++)
scanf("%d",&a[i]);
printf("the output values");
for(i=0;i<=range;i++)
printf("%d\n",a[i]);
getch();
}
2. Program to accept and print 2_D array
Solution:
#include<stdio.h>
void main()
{
int a[5][5],row,col,i,j;
printf("Enter the number of rows and columns two dimensional array");
scanf("%d%d",&row,&col);

printf("Enter the input values for 2_D array");


for(i=0;i<row;i++)
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
printf("The output values for 2_D array");
for(i=0;i<row;i++)
{
printf("\n");
for(j=0;j<col;j++)
printf("%d\t",a[i][j]);
}
getch();
}

3. Program to find the sum of two matrices


Solution:
#include<stdio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],row,col,i,j;
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d",&row,&col);

printf("Enter the A matrix");


for(i=0;i<row;i++)
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j];

}
printf("\nEnter the B matrix");
for(i=0;i<row;i++)
for(j=0;j<col;j++)
{
scanf("%d",&b[i][j]);
}

for(i=0;i<row;i++)
for(j=0;j<col;j++)
{
c[i][j]=a[i][j]+b[i][j];
}

printf("\n The addition of two matrix \n");


for(i=0;i<row;i++)
{
printf("\n");
for(j=0;j<col;j++)

printf("%d\t",c[i][j]);

}
getch();
}
4. Program to find subtraction of two matrices
Solution: Logic to do this program
c[i][j]=a[i][j] - b[i][j];

You might also like