Rewrite “poly.c” and using structure. • Determine the required members of the st
ID: 3866568 • Letter: R
Question
Rewrite “poly.c” and using structure.
• Determine the required members of the structure.
• How the readability of your code was increased by using structure?
poly.c:
#include<stdio.h>
double all[100];
int u,v;
void maxi(int a,int b){
if (a <= b){
u=b,v=a;
}
else u=a,v=b;
}
void setzero(double x[]){
int i;
for (i = 0;i < 100;i++){
x[i] = 0;
}
}
void fillup(double x[],double y[],int a, int b){
int i;
for (i=v;i<u;i++){
if (v == a) x[i] = 0;
else if (v==b) y[i] = 0;
}
}
void plus(double x[],double y[],int u){
int i;
for (i=0;i<=u;i++){
all[i] =x [i] + y[i];
}
}
void minu(double x[],double y[],int u){
int i;
for (i=0;i<=u;i++){
all[i]=x[i] - y[i];
}
}
void mult(double x[],double y[],int a,int b){
int i,j;
for (i=0;i<a;i++){
for (j=0;j<b;j++){
all[i+j]+=x [i] *y [j];
}
}
}
void prt(double x[], int a){
int i;
if (x[0]!=0) printf("(%.f) + ",x[0]);
if (x[1]!=0) printf("(%.f)x + ",x[1]);
for (i=2;i<a;i++){
if (x[i]!=0){
printf("(%.f)x^%d",x[i],i);
if (i<a-1) printf(" + ");
}
}
printf(" ");
}
int main(){
int x,a,b;
double polya[]={1,2,3};
double polyb[]={0,3,2,1};
a=sizeof(polya)/sizeof(double);
b=sizeof(polyb)/sizeof(double);
x=a+b-1;
maxi(a,b);
printf("f(x) = ");
prt(polya,a);
printf("g(x) = ");
prt(polyb,b);
mult(polya,polyb,a,b);
printf("f(x)*g(x) = ");
prt(all,x);
fillup(polya,polyb,a,b);
setzero(all);
plus(polya,polyb,u);
printf("f(x)+g(x) = ");
prt(all,u);
setzero(all);
minu(polya,polyb,u);
printf("f(x)-g(x) = ");
prt(all,u);
return 0;
}
Explanation / Answer
we can create a new struct called poly
struct poly {
// the whole poly array.
double arr_a;
double arr_b;
// size of arrays.
size_t len_a;
size_t len_b;
};
we'd like to add a constructor-ish method that will set a, b too.
* Readability of code has increased considerably due to the fact that we no longer need to use many variables instead a we can use a single bound variable on which properties can be defined.
* With proper variable names grouped variables like structs tend to reduce the overhead in determining what the variable does for the user.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.