This is for [Data Structure] cource. Could you please provide the answers for th
ID: 3862112 • Letter: T
Question
This is for [Data Structure] cource.
Could you please provide the answers for these two questions in either Java or C?
*****Please don't post a picture. Write the answers in here or provide a source code link if possible because it's very hard to read.
-----------------------------------------------------------------------
UPDATE for question 5:
In this question,
we would like to write a C function that adds two polynomials, A and B, to obtain D=A+B.
We represent A and B in a single 1D array, so , we need startA and finishA to denote the starting
and finishing point, so as to B...
For D, *startD and *finishD also denote the starting point and finishing point of D.
in this question, we uses only one global arrary---terms, to store all our polynomials
The C declarations needed are:
typedef struct{
float coef;
int expon;
} polynomial;
polynomial terms[MAX_TERMS];
int avail;
we assume that, polynomial A and B is already stored in terms[], and denoted by startA, finishA, startBfinishB. Variable avail denotes the next available position in the array terms[].
In the question, based on the above information, we asked you to write a polynomial adding function.
2. Determine the worst-case complexity of the following program void transpose (int al IMAX SI int temp; for i 0; i MAX SIZE-1; i++) for (j i 1; i MAX SIZE; i++) SWAP (aliUlalj][il,temp);Explanation / Answer
1. add function along with no.of times each instruction would be executed:
#include<stdio.h>
#include<stdlib.h>
#define MAX_TERMS
typedef struct{
float coef;
int expon;
} polynomial;
polynomial terms[MAX_TERMS];
int avail;
void add(int startA,int finishA, int startB,int finishB, int *startD, int *finishD){
int i,j,k; //1 time
//assuming all the polynomials are stored in descending orders of exponents
i=startA; //1 time
j=startB; //1 time
k=*startD; //1 time
while(i<=finishA&&j<=finishB){ // 2n+1 times
//higher coefficient will appear first
if(terms[i].expon>terms[j].expon){//2n+1 times
terms[k].coef=terms[i].coef;//n times
terms[k].expon=terms[i].expon;//n times
k++;// n times
i++;//n times
}
else
if(terms[i].expon<terms[j].expon){//n+1 times
terms[k].coef=terms[j].coef;//n+1 time
terms[k].expon=terms[j].expon;//n+1 times
k++;// n+1 times
j++;//n+1 times
}
// coefficients with same exponents get added
else //0 times
{
terms[k].coef=+terms[i].coef+terms[j].coef; //0 times
terms[k].expon=terms[j].expon; //0 times
i++; //0 times
k++;//0 times
j++;//0 times
}
}
while(i<=finishA){ // 1 time
terms[k].coef=terms[i].coef;// 1 time
terms[k].expon=terms[i].expon;// 1 time
k++;// 1 time
i++;// 1 time
}
while(j<=finishB){ // 1 time
terms[k].coef=terms[j].coef;// 0 time
terms[k].expon=terms[j].expon;// 0 time
k++;// 0 time
j++;// 0 time
}
*finishD=k-1; // 1 time
}
int main(){
int n, ,i,j,startA,startB,finishA,finishB,startD,finishD;
// to input polynomial A
scanf("%d", &n);
startA=0;
finishA=n-1;
for(i=0;i<n;i++)
scanf("%f %d", &terms[i].coef, &terms[i].expon);
// to input polynomial B
startB=n;
scanf("%d", &n);
finishB=startB+n-1;
for(i=startB;i<=finishB;i++)
scanf("%f %d", &terms[i].coef, &terms[i].expon);
startD=finishB+1;
add(startA,finishA,startB,finishB,&startD,&finishD);
printf("%d",finishD);
//// to display polynomial D(sum)
for(i=startD;i<=finishD;i++){
printf("%f %d ",terms[i].coef,terms[i].expon);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.