a program in c that adds and subtracts two arrays of size (3 rows of 4 columns)
ID: 1862587 • Letter: A
Question
a program in c that adds and subtracts two arrays of size (3 rows of 4 columns) using functions to add the arrays and subtract them.
- Prompt the user give data for the first array: 4 real numbers (type double) per line separated by spaces
- prompt the user to enter the data for the second array
- call a function that you write to add two arrays
-call a function to subtract the second array from the first array and place the result in another array
-Print each array to the screen, with line of text that says which array it is.
Explanation / Answer
#include<malloc.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
void add(int **a,int **b,int r1,int c1,int r2,int c2)
{
int i,j,temp;
if(r1==r2 && c1==c2)//If row and column sizes of both arrays match
{
printf("The summation matrix is :n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
temp=a[i][j]+b[i][j];
printf("%d ",temp);
}//end of loop j
printf("n");
}//end of loop i
}//end of if
else
printf("The matrices cannot be added as we have different row and column no.s for the two matrices n");
}//end of add
------------------------------------
void sub(int **a,int **b,int r1,int c1,int r2,int c2)
{
int i,j,temp;
if(r1==r2 && c1==c2)//If row and column sizes of both arrays match
{
printf("The difference matrix is :n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
temp=abs(a[i][j]-b[i][j]);
printf("%d ",temp);
}//end of loop j
printf("n");
}//end of loop i
}//end of if
else
printf("The difference cannot be found as we have different row and column no.s for the two matrices n");
}//end of sub
void main()
{
int **a,**b,r1,r2,c1,c2,i,j,ch;
//Function prototypes
void add(int **,int **,int,int,int,int);
void sub(int **,int **,int,int,int,int);
//Accepting row and column sizes from user
printf("Enter row and column sizes of Matrix 1 n");
printf("Row ");
scanf("%d",&r1);
printf("Column ");
scanf("%d",&c1);
printf("Enter row and column sizes of Matrix 2 n");
printf("Row ");
scanf("%d",&r2);
printf("Column ");
scanf("%d",&c2);
//Allocating memory to the 2D arrays
a=(int **)malloc(r1*sizeof(int *));
for(i=0;i<r1;i++)
a[i]=(int *)malloc(c1*sizeof(int));
b=(int **)malloc(r2*sizeof(int *));
for(i=0;i<r2;i++)
b[i]=(int *)malloc(c2*sizeof(int));
//Accepting array elements
printf("Enter elements of Matrix 1n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
printf("Enter elements of Matrix 2n");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
printf("Array 1 is : n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d ",a[i][j]);
printf("n");
}//end of loop i
printf("Array 2 is : n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d ",b[i][j]);
printf("n");
}//end of loop i
printf("n");
printf(" MENU n");
printf(" 1.ADDITION n");
printf(" 2.DIFFERENCE n");
printf(" 3.EXIT n");
printf("n");
do
{
printf("n");
printf("Enter choice n");
scanf("%d",&ch);//Accepting users choice
switch(ch)//Switching through the different functions according to choice
{
case 1:add(a,b,r1,c1,r2,c2);
break;
case 2:sub(a,b,r1,c1,r2,c2);
break;
case 3:printf("Terminating program n");
break;
default:printf("Wrong choice entered n");
}//end of switch
}//end of do
while(ch!=4);
}//end of main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.