Hi,
(Please dont be discouraged from the length of this post, I would greatly appreciate any pointers, advice or solution.)
I have programmed a bit but I'm fairly new to C, (I'm spoiled from higher level languages, so obviously you can tell this is hurting me). Anyways, the problem I'm having lies in the add_polynom function.
Basically the point of the function is to simply add two polynomials.
Everything is fine and dandy until I start adding a second poly
onto the first. For some reason values of the first array also gets modified, and I have debugged for days with no solution.
I have bolded where I think the problem lies.
Intended Algo: Add coefficients with same exponent. Next detect if any exponent that is in ex2 exists in ex1. If there are any, make room for the needed exp that are to be put in coef1 and ex1, then put 'em in. Finally put the array in order (not done yet).
Code:
Ex:
in a separate Main file.
int coef1[] = {3, 4, 5, 6, 3};
int ex1[] = {3, 1, 2, 0, 5};
int coef2[] = {4, 5, 6, 7, 9};
int ex2[] = {4, 1, 2, 0, 8};
dataSize = 5; // Indicate the size of the arrays
add_polynom(coef1, ex1, coef2, ex2);
output :
ADD:
int coefficient1[] = {3, 4, 5, 6, 3};
int exponent1[] = {3, 1, 2, 0, 5};
PLUS
int coefficient2[] = {4, 5, 6, 7, 9};
int exponent2[] = {4, 1, 2, 0, 8};
ADD coefficient when exponents of both arrays are the same and put that result in coefficient1
coefficient1 [3 9 11 13 3]
exponent1 [3 1 2 0 5]
2 indices of exponent2 are not in exponent1
coefficient2 [4 5 6 7 9]
exponent2 [4 1 2 0 8]
coefficient 1 must be expanded to add the two missing exponents into exponent1 and coefficient1
coefficient1 [3 9 11 13 3 rndNum rndNum]
exponent1 [3 1 2 0 5 rndNum rndNum]
Copy over the required values (this is what is expected final answer)
coefficient1 [3 9 11 13 3 4 9]
exponent1 [3 1 2 0 5 4 8]
This is what is happening
coefficient [4 8 11 13 3 4 9]
exponent [3 1 2 0 5 4 8]
For some reason 4 and 8 from exponent are copied over to the beginning indices of coefficient. WHYY?? It's really got me down.
I would appreciate any help.
Code:
int dataSize;
/*
Initialize all coefficients and exponents of the polynomial to zero.
*/
void init_polynom( int coeff[ ], int exp[ ] )
{
int j;
for (j=0; j<=ASIZE-1; j++)
{
coeff[j] = 0;
exp[j]= 0;
//exp[j] = 0;
}
} /* end init_polynom */
void printNumArray(int a[])
{
int i;
printf("[");
/* (ASIZE/8)-1*/
for (i=0; i<= dataSize - 1; i++)
{
if (i!= dataSize - 1)
printf("%d\t", a[i]);
else
printf("%d", a[i]);
}
printf("]");
}
/*
Get inputs from user using scanf() and store them in the polynomial.
*/
void get_polynom( int coeff[ ], int exp[ ] )
{
int tmpArr[ASIZE];
int in;
int x;
int dctr;
scanf("%d", &in);
dctr = in;
dataSize = in;
for (x=0; x<= ((dctr*2)-1); x++)
{
scanf("%d", &in);
tmpArr[x] = in;
}
for (x=0; x<= dctr-1; x++)
{
coeff[x] = tmpArr[2*x];
exp[x] = tmpArr[(2*x)+1];
}
//printf("coeff:\t");
//printNumArray(coeff);
//printf("\nexp:\t");
//printNumArray(exp);
} /* end get_polynom */
/*
Check if the coefficients of all the entries sum up to 0.
*/
int zero_sum(int coeff[])
{
int i;
int sum;
for (i=0; i<=dataSize-1; i++)
sum += coeff[i];
if (sum == 0)
return 1;
else
return 0;
}
/*
Convert the polynomial to a string s.
*/
void polynom_to_string( int coeff[ ], int exp[ ], char s[ ] )
{
/*
int cr_coeff;
int cr_exp;
int i;
if (zero_sum(coeff) == 0) /* Sum is not zero
{
for (i=0; i<= dataSize-1; i++)
{
cr_coeff = coeff[i];
cr_exp = exp[i];
if (cr_coeff != 0)
{
if (cr_coeff == 1)
{
if (cr_exp == 1)
{
/* coef and exp are both 1
s += ((char)cr_coeff)+"x^"+((char)cr_exp)+"+";
}
else
{
/* only coeff is one
s += cr_coeff"x^"+cr_exp+"+";
}
}
else
{
if (cr_exp == 1)
{
/* only cr_exp is one
s += cr_coeff"x^"+cr_exp+"+";
}
else
{
/* Neither cr_exp or coeff are one
s += cr_coeff"x^"+cr_exp+"+";
}
}
}
}
}
else /* Sum is zero
{
s = "0";
printf("%s", s);
}
printf("FINAL ANSWER->%s<-", s)*/
} /* end polynom_to_string */
/*
Evaluate the polynomial for the value of x and store the result p(x) in variable result.
*/
void eval_polynom( int coeff[ ], int exp[ ], double x, double *result)
{
int i, j;
double s, ml;
for(i=0;i<=dataSize-1;i++)
{
ml = x;
// Exponent Operation
for (j=0; j<=exp[i]-2; j++)
{
ml *= x;
}
// Coefficient Multiplication
ml *= coeff[i];
//printf("\nreswithcof:%f\n", ml);
s += ml;
//("sum:%f\n", s);
}
*result = s;
} /* end eval_polynom */
/*
Add two polynomials and the result is stored in the first polynomial (arrays co1[] and ex1[]).
*/
void add_polynom( int co1[ ], int ex1[ ], int co2[ ], int ex2[ ] )
{
//addOp(&co1, &ex1, &co2, &ex2);
int i,j;
// Add all the data from col2 to col1 and make col2 entries found 0
for (i = 0; i<= dataSize-1; i++)
{
for (j = 0; j <= dataSize-1; j++)
{
/* Check with one i polynomial with all of j's polynomials */
if (ex1[i] == ex2[j])
{
//printf("ex1 == ex2");
co1[i] = co1[i] + co2[i];
co2[i] = 0;
}
}
}
// Loop through col2 and see if they are missing columns
int msCtr = 0; // Number of missing items counter;
int tCtr = 0; // Temporary counter
int m, n;
int d_ind_ex[dataSize];
int d_ind_co[dataSize];
for (m = 0; m <= dataSize-1; m++)
{
for (n = 0; n <= dataSize-1; n++)
{
printf("ex2->%d<- ex1->%d<-\n", ex2[m], ex1[n]);
if (ex2[m] == ex1[n])
{
tCtr++;
}
if ((n == dataSize-1) && (tCtr == 0))
{
printf("An exponent %d not found in ex1\n", ex2[m]);
d_ind_ex[msCtr] = ex2[m];
d_ind_co[msCtr] = co2[m];
msCtr++;
// tCtr = 0;
}
}
tCtr = 0;
}
d_ind_ex[msCtr];
d_ind_co[msCtr];
int k = 5;
int l, jj;
// K starts at end of co1 and ends at missing char index size
// Add the new data entries to co1 and ex1
for (k = i, jj = 0; k <= i+msCtr-1; k++, jj++)
{
dataSize++;
printf("i>%d< k>%d< jj>%d<, d_ind_co>%d<,
d_ind_ex>%d<\n", i, k, jj, d_ind_co[jj], d_ind_ex[jj]);
printf("\n\n\n\nbefore ->\n");
printNumArray(co1);
printf("\n");
printNumArray(ex1);
printf("\nK val%d, jj val%d<", k, jj);
co1[k] = d_ind_co[jj];
printf("\n");
printNumArray(co1);
printf("\n");
printNumArray(ex1);
printf("\n<-");
printf("k>%d<, BBB%dBBB", k, ex1[k]);
ex1[k] = d_ind_ex[jj];
printf("\nafter->\n");
printNumArray(co1);
printf("\n");
printNumArray(ex1);
printf("\n<--\n\n\n");
}
printf("co1:\n");
printNumArray(co1);
printf("\nex1:\n");
printNumArray(ex1);
printf("\n\nco2:\n");
printNumArray(co2);
printf("\nex2:\n");
printNumArray(ex2);
} /* end add_ polynom */
/************* END OF FILE ********************/