Reputation: 75
My Solve Function is not working and I don't know what is wrong with my Code. Any kind of help will be appreciated.
Program Required Output: Solve: This function will take a polynomial and a variable as input. It will solve the polynomial for given value of variable and will return computed result.
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
void Input(int terms, int deg[], int coef[])
{
int temp = 0;
cout << "Enter Terms = ";
cin >> terms;
cout << "Enter Degrees in Descending Order = ";
for (int i = 0; i < terms; i++)
{
cin >> deg[i];
int temp = deg[i];
if (deg[i] > 15)
{
cout << "Enter the Degree again (must be less tham 15) = ";
cin >> deg[i];
}
}
cout << endl;
for (int i = 0; i < terms; i++)
{
for (int i = 0; i < terms; i++)
{
if (deg[i] < deg[i+1])
{
temp = deg[i];
deg[i] = deg[i+1];
deg[i+1] = temp;
}
}
}
cout << "Enter Coefficients = ";
for (int i = 0; i < terms; i++)
{
cin >> coef[i];
}
}
int Solve(int terms, int deg[], int coef[], int variable)
{
int sum = 0;
int result;
cout << "Enter Variable = ";
cin >> variable;
Input(terms, deg, coef);
for (int i = 0; i < terms; i++)
{
result = pow(variable,deg[i]);
sum = result * coef[i];
sum = sum + result;
cout << result;
}
cout << sum << endl;
return sum;
}
int main()
{
int terms = 0;
int deg[5] = {0,0,0,0,0};
int coef[5] = {0,0,0,0,0};
int var = 0;
Solve(terms, deg, coef, var);
}
OUTPUT I AM, GETTING:
Enter Variable = 2
Enter Terms = 3
Enter Degrees in Descending Order = 3 2 1
Enter Coefficients = 2 2 2
0
Upvotes: 1
Views: 41
Reputation: 75
Correct Solution
Thanks to @drescherjm
void Input(int &terms, int deg[], int coef[])
{
int temp = 0;
cout << "Enter Terms = ";
cin >> terms;
cout << "Enter Degrees in Descending Order = ";
for (int i = 0; i < terms; i++)
{
cin >> deg[i];
int temp = deg[i];
if (deg[i] > 15)
{
cout << "Enter the Degree again (must be less tham 15) = ";
cin >> deg[i];
}
}
cout << endl;
for (int i = 0; i < terms; i++)
{
for (int i = 0; i < terms; i++)
{
if (deg[i] < deg[i+1])
{
temp = deg[i];
deg[i] = deg[i+1];
deg[i+1] = temp;
}
}
}
cout << "Enter Coefficients = ";
for (int i = 0; i < terms; i++)
{
cin >> coef[i];
}
}
int Solve(int terms, int deg[], int coef[], int variable)
{
int sum = 0;
int result;
cout << "Enter Variable = ";
cin >> variable;
Input(terms, deg, coef);
for (int i = 0; i < terms; i++)
{
int calc;
result = pow(variable,deg[i]);
calc = result * coef[i];
sum = sum + calc;
}
cout << sum << endl;
return sum;
}
int main()
{
int terms = 0;
int deg[5] = {0,0,0,0,0};
int coef[5] = {0,0,0,0,0};
int var = 0;
Solve(terms, deg, coef, var);
}
Upvotes: 2