Here is a Pascal's triangle . After the output of the array , user chooses a row to be integrated . There is another loop for integration but I get the answer in int form . And the required answer needs to be in double form . If I change int to double then all the code just mixes with double and int.Below is the array
1
1 1
1 2 1
1 3 3 1 ------> For this I need the answer stated below
1 4 6 4 1
Code
#include <iostream>
using namespace std;
int main() {
int integ, row_nb, pascal;
const int rows = 5;
const int columns = 5;
double integrals, P;
int **array = new int *[rows]; //generating array
for (int i = 0; i <= rows; i++)
array[i] = new int[columns];
for (int i = 0; i <= rows; i++) { //loop for te pascal's triangle
for (int j = 0; j <= i; j++) {
if (j == 0 || i == 0) {
pascal = 1; //first element of pascal's triangle
}
else {
pascal = pascal *(i - j + 1) / j; //pascal's triangle formula
}
array[i][j] = pascal;
cout << " " << pascal;
}
cout << "\n";
}
cout << "enter which row to intergrate: "; // loop for the desired array
cin >> row_nb;
cout << "P(x) = "; //loop for p(x)
for (int i = 0; i < row_nb + 1; i++) {
cout <<array[row_nb][i] << " ";
}
cout << "\n";
cout << "f P(x) = ";
for (int i = 0; i < row_nb + 1; i++) {
cout << (array[row_nb][i]) / (i + 1) << " "; // loop for the result
// (but i want this result in double form rather than int )
}
return 0;
}
For example , for
1,3,3,1I get the answer1,1,1,0BUT the answer should be1, 1.5, 1, 0.5
Aucun commentaire:
Enregistrer un commentaire