Use array? Need help with extending program

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.
- Create a linked list or use one of the stl containers for this
http://en.cppreference.com/w/cpp/container
- As you get all the x's the program needs to calculate, put them in the list.
- When the user enters 0, remove each item from the list and do the calculation
- Print the result of each calculation to stdout
can you show a quick sample?
so, the stdin would be the x values and its calculation and the stdout would be all the calculation ? I been looking over your code and try to apply it to the one i had but the results aren't too satisfying.
post what you have

E:

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
59
60
61
62
63
64
#include <iostream>
#include <utility>
#include <forward_list>
using namespace std;

float power(float, int);
ostream &operator << (ostream &, list<pair<int, float>> &);

int main(){
    int hiPow = -1, n;
    forward_list<pair<int, float>> out;
    forward_list<float> fn; //c++11
    char c;
    float x, con, coef, result = 0;
    cout << "Enter the highest exponent of your function: ";

    while (!(cin >> hiPow) || hiPow < 0) {
	if( hiPow < 0 )
	    cout << "\n\nThis exponent is negative. \n\nPlease enter a positive integer now: ";
	else { //bad input
	    cin.clear(); //clear bad input flag
	    cin.ignore(numeric_limits<streamsize>::max(), '\n');// clear stream buffer
	}
    }

    for(int i = 0; i < hiPow; i++){
	cout << "Enter the coefficient of the x^" <<(hiPow - i)<<" term: ";
	cin >>coef;
	fn.emplace_front(coef);
    }
    cout << "Enter the constant number for your function: ";
    cin >> con;

    do
    {
	cout << "Enter the value of x: ";
	cin >> x;
	// Call a function that uses the values in fn to compute a new value for x
	// Push x and the result of this function call into out
	cout<< "Do you have another value for x you want calculated?\n"
	    << "Enter 1 for yes 0 for no: ";
	cin >> c;
	c = tolower(c);
    } while( c == 'y' || c == '1' );

    cout << out << endl;
    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);
}

ostream &operator << (ostream &oss, list<pair<int, float>> &lst) {
	for (auto &w : lst) {
	    oss << "f(" << w.first << ") = " << w.second << endl;
	}
	return oss;
}
Last edited on
I used the codes off your template so it quite wrong and i looked around the internet for more information on listed and container, i couldn't find much. Also, I tried to run our code but it shows a few errors.
I messed around with the functions and end up with this.
ERROR 61 expected unqualified-id before '{' token
61 expected `,' or `;' before '{' token
Can someone look at the code and give me some advice?
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
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <list>
#include <string>
using namespace std;

float power(float, int);
ostream &operator << (ostream &, list<pair<int, float> > &);

int main()
{
    int hiPow = -1, n;
    list<pair<int, float> > out;
    list<float> fn; //c++11
    char c;
    float x, con, coef, result = 0;
    cout << "Enter the highest exponent of your function: ";

    while (!(cin >> hiPow) || hiPow < 0) {
	if( hiPow < 0 )
	    cout << "\n\nThis exponent is negative. \n\nPlease enter a positive integer now: ";
	else { //bad input
	    cin.clear(); //clear bad input flag
	    cin.ignore(numeric_limits<streamsize>::max(), '\n');// clear stream buffer
	}
    }

    for(int i = 0; i < hiPow; i++){
	cout << "Enter the coefficient of the x^" <<(hiPow - i)<<" term: ";
	cin >>coef;
	fn.push_front(coef);
    }
    cout << "Enter the constant number for your function: ";
    cin >> con;

    do
    {
	cout << "Enter the value of x: ";
	cin >> x;
	// Call a function that uses the values in fn to compute a new value for x
	// Push x and the result of this function call into out
	cout<< "Do you have another value for x you want calculated?\n"
	    << "Enter 1 for yes 0 for no: ";
	cin >> c;
	c = tolower(c);
    } while( c == 'y' || c == '1' );

    cout << out << endl;
    cout << "f("<<x<<") = "<< result;

    return 0;
}

float power(float x, int y){
    float z=1;
    for(int i=1;i<=y;++i){
	z*=x;}
    return (z);
}

ostream &operator << (ostream &oss, list<pair<int, float> > &lst);

{	for (auto &w : lst) 
    {
	    oss << "f(" << w.first << ") = " << w.second << endl;
	}
	return oss;
}
Last edited on
anyone ? :|
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <list>
#include <limits>
using namespace std;

static float power(float x, int y)
{
  float z=1;
  for (int i = 1; i <= y; ++i) {
    z *= x;
  }
  return z;
}

static float ploynomial(const list<pair<int, float> >& pow_coeffs, float x)
{
  float result = 0.0;
  for (auto &cp: pow_coeffs) {
    int pow = cp.first;
    float coeff = cp.second;
    result += coeff * power(x, pow);
    //cout << "pow=" << pow << " coeff=" << coeff << " result=" << result << endl;
  }
  //cout << "result=" << result << endl;
  return result;
}

ostream &operator << (ostream &oss, list<pair<int, float> > &lst)
{
  for (auto &w : lst)
    oss << '(' << w.first << "," << w.second << ')';
  return oss;
}

int main()
{
  cout << "Enter the highest exponent of your function: ";

  int hiPow;
  while (!(cin >> hiPow) || hiPow < 0) {
    if( hiPow < 0 )
      cout << "\n\nThis exponent is negative. \n\nPlease enter a positive integer now: ";
    else { //bad input
      cin.clear(); //clear bad input flag
      cin.ignore(numeric_limits<streamsize>::max(), '\n');// clear stream buffer
    }
  }

  list<pair<int, float> > pow_coeffs;
  for (int i = hiPow; i > 0; --i){
    cout << "Enter the coefficient of the x^" << i <<" term: ";
    float coef;
    cin >>coef;
    pow_coeffs.push_back(pair<int,float>(i, coef));
  }
  cout << "Enter the constant number for your function: ";
  float con; cin >> con;
  pow_coeffs.push_back(pair<int,float>(0, con));

  cout << "pow_coeffs=" << pow_coeffs << endl;

  list<float> X; // all the x's we want to compute f(x) for
  char c;
  do {
    cout << "Enter the value of x: ";
    float x; cin >> x; X.push_back(x);
    cout<< "Do you have another value for x you want calculated?\n"
	<< "Enter 1 for yes 0 for no: ";
    cin >> c;
    c = tolower(c);
  } while( c == 'y' || c == '1' );

  // compute each f(x)
  for (auto &x : X) {
    float result = ploynomial(pow_coeffs, x);
    cout << "f("<<x<<") = "<< result << endl;
  }

  return 0;
}
Topic archived. No new replies allowed.