Запись функции void multiply (int * coeff1, int m, int * coeff2, int n, int * result), выполняющей операцию умножения двух полиномов переменной x, заданных через массивы коэффициентов coeff1, coeff2. Полученные полиномиальные коэффициенты записываются в массив результатов.
КОД:
#include <iostream>
using namespace std;
void multiply(int *coeff1, int m, int* coeff2, int n, int* result);
int main() {
const int m = 5;
const int n = 3;
int coeff1[5];
int coeff2[3];
int result[9];
multiply(coeff1,m,coeff2,n,result);
return 0;
}
void multiply(int *coeff1, int m, int* coeff2, int n, int* result) {
cout << "how many coeff1:";
cin >> m;
cout << "how many coeff2:";
cin >> n;
cout << "coeff1:";
for (int i = 0; i < m; i++) {
cin >> coeff1[i];
}
cout << "coeff2:";
for (int i = 0; i < n; i++) {
cin >> coeff2[i];
}
int licznik; //counter
licznik = m;
cout << "(";
for (int i = 0; i < m; i++) {
if (coeff1[i] != 0) {
cout<<coeff1[i];
if (licznik == 1) {
cout << "";
} else if (licznik > 2) {
if (coeff1[i] > 0) {
cout << "x^" << licznik - 1 << "+";
} else {
cout << "x^" << licznik - 1 << "";
}
} else {
cout << "x+";
}
}
licznik--;
}
cout << ") * (";
licznik = n;
for (int i = 0; i < n; i++) {
if (coeff2[i] != 0) {
cout<<coeff2[i];
if (licznik == 1) {
cout << "";
} else if (licznik > 2) {
if (coeff2[i] > 0) {
cout << "x^" << licznik - 1 << "+";
} else {
cout << "x^" << licznik - 1 << "";
}
} else {
cout << "x+";
}
}
licznik--;
}
cout << ") = ";
int potega;
int k = 0;
cout << "(";
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (coeff1[i] == 0) {
i++;
}
if (coeff2[j] == 0) {
j++;
}
result[k] = (coeff1[i] * coeff2[j]);
k++;
}
}
for(int i = 0; i < 9; i++) {
if(n > 1 && m > 1) {
cout << result[i] << " ";
}
}
cout << ")";
Пример:
(2x^4 + 5x^2 - 4) * (4x^2 + x + 3) = 8x^6 + 2x^5 + 6x^4 + 20x^4 + 5x^3 + 15x^2 - 16x^2 - 4x - 12 =
8x^6 + 2x^5 + 26x^4 + 5x^3 - x^2 - 4^x - 12.
Мой ввод:
how many coeff1:5
how many coeff2:3
coeff1:2
0
5
0
-4
coeff2:4
1
3
Выход:
(2x^4+5x^2+-4) * (4x^2+1x+3) = (8 2 6 20 5 15 -16 -4 -12 ) // should be: 8x^6 + 2x^5 + 6x^4 + 20x^4 + 5x^3 + 15x^2 - 16x^2 - 4x - 12
Проблема:
I multiply the coefficients but I have a problem with multiplying the unknowns - x and to combine the
coefficients with the unknowns