Hi, I'm having trouble getting the correct output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include <iostream>
#include <stdio.h>
#include <cmath>
using namespace std;
int degree;
int power;
double x;
int Convert(int output);
int main()
{
int output;
char answer;
double exponentiated;
double exponent;
do
{
cout << "Enter degree: \n";
cin >> degree;
cout << "Enter power: \n";
cin >> power;
cout << "Enter 'x': \n";
cin >> x;
cout << "Output: ";
cout << Convert(output);
exponent = (degree - power);
exponentiated = pow(x,exponent);
cout << output * exponentiated << " times Y^" << power << endl;
cout << "\nWould you like to try another? (y/n): ";
cin >> answer;
} while (answer == 'y' || answer == 'Y');
system("PAUSE");
return 0;
}
int Convert(int output)
{
long factorial1;
factorial1 = 1L;
for(long i=2L; i <= degree; i++)
factorial1 *= i;
long factorial2;
factorial2 = 1L;
for(long i=2L; i <=power; i++)
factorial2 *= i;
long factorial3;
factorial3 = 1L;
for(long i=2L; i <= (degree - power); i++)
factorial3 *= i;
return output = (factorial1) / ((factorial2) * (factorial3));
}
|
The problem seems to be here:
1 2
|
exponentiated = pow(x,exponent);
cout << output * exponentiated << " times Y^" << power << endl;
|
Whenever I exclude the output * exponentiated and just put
|
cout << output << " times" << exponentiated
|
It works. But I want the program to do the calculations itself.
Edit:
I think I fixed the problem. I moved output * exponentiated to the Convert() function.
1 2 3 4
|
exponent = (degree - power);
exponentiated = pow(x,exponent);
output = (factorial1) / ((factorial2) * (factorial3)) * exponentiated;
|
Last edited on
I've fixed the problem which I think was because I had to use Convert(output) instead of just output.
Putting it in a set of parenthesis didn't work :/
I was still getting answers such as 210.4049302e+10X^3.
When the answer should have been 540X^3.
Last edited on