When I use the function multiplyPolynomials (the last function in the code) the function works as intended but as soon as the debugger is done with executing the function the numbers in the array result[] disappear.
Code:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int readPolynomial(int poly[]);
void printPolynomial(int numCoefs, int poly[]);
void multiplyPolynomials(int poly1[], int poly2[], int result[]);
void main()
{
int polynome1[2][5] = {{0,0,0,0,0},{0,0,0,0,0}};
int polynome2[2][5] = {{0,0,0,0,0},{0,0,0,0,0}};
int result[2][9] = {{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0}};
printf("Numerator of polynomial 1:\n");
int numerator1 = readPolynomial(polynome1[0]);
printPolynomial(numerator1, polynome1[0]);
printf("denominator of polynomial 1:\n");
int denominator1 = readPolynomial(polynome1[1]);
printPolynomial(denominator1, polynome1[1]);
printf("Numerator of polynomial 2:\n");
int numerator2 = readPolynomial(polynome2[0]);
printPolynomial(numerator2, polynome2[0]);
printf("denominator of polynomial 2:\n");
int denominator2 = readPolynomial(polynome2[1]);
printPolynomial(denominator2, polynome2[1]);
multiplyPolynomials(polynome1[0], polynome2[1], result[0]);
printPolynomial(numerator1, result[0]);
}
int readPolynomial(int poly[])
{
int number_of_coefficients = 0;
printf("Number of coefficients? ");
scanf("%i", &number_of_coefficients);
printf("Please enter %i coefficients: ", number_of_coefficients);
for(int i = 0; i<number_of_coefficients; ++i)
{
scanf("%i", &poly[i]);
}
return number_of_coefficients;
}
void printPolynomial(int numCoefs, int poly[])
{
printf("%i*x^0 + %i*x^1 + %i*x^2 + %i*x^3 + %i*x^4", poly[0], poly[1], poly[2], poly[3], poly[4]);
}
void multiplyPolynomials(int poly1[], int poly2[], int result[])
{
int sum = 0;
for(int i = 0; i <5; ++i)
{
for(int j = 0; j<5; ++j)
{
sum = poly1[i]*poly2[j];
result[i+j] = sum + result[i+j];
}
}
}
If I let the debugger stop just before finishing the function multiplyPolynomials and I check the array result[] then I get the correct results (and the intended results), as soon as the debugger has finished with the function that array is empty.
If I let the debugger execute the entire program then the last call of the printPolynomial function doesn't work.
As far as I understood an array automatically works with pointers so everything that is being done to the array by a function should be stored during the remainder of the execution of the program (as long as you don't change anything in the array).
I tried to solve the problem by returning the result-array in the function multiplyPolynomials.
Though that seemed to work (the numbers are still there in the array result[] after the function multiplyPolynomial has been executed) the printfunction still doesn't work.
Furthermore I think that this is not particularly an elegant method.
Code:
#include<stdio.h>
int readPolynomial(int poly[]);
void printPolynomial(int numCoefs, int poly[]);
int multiplyPolynomials(int poly1[], int poly2[], int result[]);
void main()
{
int polynome1[2][5] = {{0,0,0,0,0},{0,0,0,0,0}};
int polynome2[2][5] = {{0,0,0,0,0},{0,0,0,0,0}};
int result[2][9] = {{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0}};
printf("Numerator of polynomial 1:\n");
int numerator1 = readPolynomial(polynome1[0]);
printPolynomial(numerator1, polynome1[0]);
printf("denominator of polynomial 1:\n");
int denominator1 = readPolynomial(polynome1[1]);
printPolynomial(denominator1, polynome1[1]);
printf("Numerator of polynomial 2:\n");
int numerator2 = readPolynomial(polynome2[0]);
printPolynomial(numerator2, polynome2[0]);
printf("denominator of polynomial 2:\n");
int denominator2 = readPolynomial(polynome2[1]);
printPolynomial(denominator2, polynome2[1]);
multiplyPolynomials(polynome1[0], polynome2[1], result[0]);
printPolynomial(numerator1, result[0]);
}
int readPolynomial(int poly[])
{
int number_of_coefficients = 0;
printf("Number of coefficients? ");
scanf("%i", &number_of_coefficients);
printf("Please enter %i coefficients: ", number_of_coefficients);
for(int i = 0; i<number_of_coefficients; ++i)
{
scanf("%i", &poly[i]);
}
return number_of_coefficients;
}
void printPolynomial(int numCoefs, int poly[])
{
printf("%i*x^0 + %i*x^1 + %i*x^2 + %i*x^3 + %i*x^4", poly[0], poly[1], poly[2], poly[3], poly[4]);
}
int multiplyPolynomials(int poly1[], int poly2[], int result[])
{
int sum = 0;
for(int i = 0; i <5; ++i)
{
for(int j = 0; j<5; ++j)
{
sum = poly1[i]*poly2[j];
result[i+j] = sum + result[i+j];
}
}
for(int i = 0; i<9; ++i)
{
return result[i];
}
}
What am I doing wrong?
Note that this program is far from finished. For example: right now I am not doing anything with the variable numCoefs (printPolynomial) but I am going to use that in a conditional statement in the function printPolynomial so that only the x'es with a non-zero coefficient are being printed. So please ignore these kind of things.