If you run the program, I would like it to add for additional "x" and show result for multiple f(x)=> for the program to run like the example. for example:
Enter the highest exponent of your function
5
Enter the coefficient of the x^5 term
3
Enter the coefficient of the x^4 term
1
Enter the coefficient of the x^3 term
8
Enter the coefficient of the x^2 term
7
Enter the coefficient of the x^1 term
10
Enter the constant number for your function
2
Enter the value of x you would like calculated
3
Do you have another value for x you want calculated? Enter 1 for yes 0 for no
1
Enter the value of x you would like calculated
7
Do you have another value for x you want calculated? Enter 1 for yes 0 for no
1
Enter the value of x you would like calculated
-2
Do you have another value for x you want calculated? Enter 1 for yes 0 for no
0
f(x) = 3x^5 + 1x^4 + 8x^3 + 7x^2 + 10x^1 + 2
f(3) = 1121
f(7) = 55981
f(-2) = -134
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
|
#include <iostream>
using namespace std;
float power(float,int);
int main(){
int hiPow;
int n;
int a[n];
char c;
float x,con,coef,result=0;
cout << "Enter the highest exponent of your function: ";
label1:
cin >> hiPow;
if(hiPow<0){cout << "\n\nThis exponent is negative. \n\nPlease enter a positive integer now: ";
goto label1;}
for(int i=0;i<hiPow;i++){
cout << "Enter the coefficient of the x^" <<(hiPow-i)<<" term: ";
cin >>coef ;
if (coef==0) continue;
result+=coef*(power(x,(hiPow-i))) ;}
cout << "Enter the constant number for your function: "; cin >> con;
result+=con;
do
{
cout << "Enter the value of x: ";cin >> a[n];
cout<<"Do you have another value for x you want calculated? Enter 1 for yes 0 for no \n";
cin>>c;
}while(c=='y'||c=='1');
cout << "f("<<a[n++]<<") = "<< result;
return 0;
}
float power(float x, int y){
float z=1;
for(int i=1;i<=y;++i){
z*=x;}
return (z);
}
|
The program doesn't calculate multiple X's like i wanted to. I am still not completely understand the array concept. i think i have to use array to solve this. I just try to work one step at a time. any comment is highly appreciated.