Write a C program to read in a 2-dimestional arrays and store this array in ARY
ID: 3575998 • Letter: W
Question
Write a C program to read in a 2-dimestional arrays and store this array in ARY whose dimensions M and N are read in. Write a function called COPYR that will copy all the elements of the given double array ARY row by row to a single array called VCTR Write a function called COPYC that will copy all the elements of the Given double array ARY column by column to a single array called VCTC Write a function called POLYNOMIAL that will calculate all the elements of the double array ARY one by one with the given polynomial function called POLY. You will store the new double array values to CALCULATED double arrays. POLY(X) = 2X^3 + 5X^2 + 3Sin(X) - 4COS(X) The double array ARY given as: ARY = 2.1 3.5 1.1 5.5 0.5 3.5 4.6 0.75 5.3 0.95 0.3 1.2Explanation / Answer
The complete working C program is given below:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define sf scanf
#define pf printf
#define MAX_LEN 250
float ARY[MAX_LEN][MAX_LEN],CALCULATED[MAX_LEN][MAX_LEN];
void COPYR(float ARY[][MAX_LEN],float VCTR[],int n,int m);
void COPYC(float ARY[][MAX_LEN],float VCTC[],int n,int m);
void POLYNOMIAL(float ARY[][MAX_LEN],float CALCULATED[][MAX_LEN],int n,int m);
int main()
{
int n,m,i,j;
sf("%d%d",&n,&m);
//float ARY[n][m];
for(i=0;i<n;i++)
for(j=0;j<m;j++)
sf("%f",&ARY[i][j]);
float VCTR[n*m],VCTC[n*m];
pf("The Given array ARY is: ");
for(i=0;i<n;i++)
{ for(j=0;j<m;j++)
pf("%-6.2f ",ARY[i][j]);
pf(" ");
}
COPYR(ARY,VCTR,n,m);
COPYC(ARY,VCTC,n,m);
POLYNOMIAL(ARY,CALCULATED,n,m);
pf(" ");
return 0;
}
void COPYR(float ARY[][MAX_LEN],float VCTR[],int n,int m)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
VCTR[i*m+j]=ARY[i][j];
pf(" The ROW-VEC is: ");
for(i=0;i<n*m;i++)
pf("%-2.2f ",VCTR[i]);
}
void COPYC(float ARY[][MAX_LEN],float VCTC[],int n,int m)
{
int i,j;
for(j=0;j<m;j++)
for(i=0;i<n;i++)
VCTC[i+j*n]=ARY[i][j];
pf(" The COL-VEC is: ");
for(i=0;i<n*m;i++)
pf("%-2.2f ",VCTC[i]);
}
void POLYNOMIAL(float ARY[][MAX_LEN],float CALCULATED[][MAX_LEN],int n,int m)
{
//poly(x)=2X^3 + 5X^2 + 3Sin(X) - 4Cos(X)
int i,j;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
float x=ARY[i][j];
CALCULATED[i][j]= pow(2*x,3) + pow(5*x,2) + 3*sinf(x) - 4*cosf(x);
}
pf(" The CALCULATED array is: ");
for(i=0;i<n;i++)
{ for(j=0;j<m;j++)
pf("%-9.2f ",CALCULATED[i][j]);
pf(" ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.