#c #arrays #loops
#c #массивы #петли
Вопрос:
Моя функция решения не работает, и я не знаю, что не так с моим кодом. Любая помощь будет оценена.
Требуемый программой вывод: Решите: эта функция будет принимать многочлен и переменную в качестве входных данных. Он решит многочлен для заданного значения переменной и вернет вычисленный результат.
#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 resu<
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 resu<
cout << resu<
}
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);
}
ВЫВОД, КОТОРЫЙ Я ЕСТЬ, ПОЛУЧАЮ:
Enter Variable = 2
Enter Terms = 3
Enter Degrees in Descending Order = 3 2 1
Enter Coefficients = 2 2 2
0
Комментарии:
1. Похоже, что в
Solve()
переменнойterms
всегда 0, потому что вы передаете ее по значению вInput()
2. Любое возможное решение для этого?
3. Передача по ссылке, а не по значению. Помните, что передача по значению передает копию переменной в функцию.
4. ОК. Дай мне попробовать. Спасибо, приятель
5. Если вы не знаете
void Input(int amp; terms, int deg[], int coef[])
, это исправление.
Ответ №1:
Правильное решение
Благодаря @drescherjm
void Input(int amp;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 resu<
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);
}