For Loop Variable Scope

Hello all,

I am new to C++, and am having trouble with a for loop. Here is the source code:

// STUFF

#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
int degree;
do
{
cout << "Enter the degree of the polynomial: ";
cin >> degree;
if (degree != 4 && degree != 5)
{cout << "Error: degree of polynomial must be 4 or 5" << endl;}
}
while (degree != 4 && degree != 5);

double a;
do
{
cout << "Enter the x^5 coefficient: ";
cin >> a;
if (a < 0)
{
cout << "Error: coefficient must be be non-negative" << endl;
}
}
while (a < 0);

double b;
do
{
cout << "Enter the x^4 coefficient: ";
cin >> b;
if (b < 0)
{
cout << "Error: coefficient must be be non-negative" << endl;
}
}
while (b < 0);

double c;
do
{
cout << "Enter the x^3 coefficient: ";
cin >> c;
if (c < 0)
{
cout << "Error: coefficient must be be non-negative" << endl;
}
}
while (c < 0);

double d;
do
{
cout << "Enter the x^2 coefficient: ";
cin >> d;
if (d < 0)
{
cout << "Error: coefficient must be be non-negative" << endl;
}
}
while (d < 0);

double e;
do
{
cout << "Enter the x^1 coefficient: ";
cin >> e;
if (e < 0)
{
cout << "Error: coefficient must be be non-negative" << endl;
}
}
while (e < 0);

double f;
do
{
cout << "Enter the x^0 coefficient: ";
cin >> f;
if (f < 0)
{
cout << "Error: coefficient must be be non-negative" << endl;
}
}
while (f < 0);

if (degree == 4)
{a = 0;}

double l;
double u;

do
{
cout << "Enter the lower limit for the definite integral: ";
cin >> l;
if (l < 0)
{cout << "Error: lower limit must be non-negaive" << endl;}
}
while (l < 0);

do
{
cout << "Enter the upper limit for the definite integral: ";
cin >> u;
if (u < 0)
{cout << "Error: upper limit must be non-negaive" << endl;}
}
while (u < 0);

int n;
do
{
cout << "Enter the number of subintervals for the approximation: ";
cin >> n;
if (n < 1)
{cout << "Error: the number of subintervals must be an integer greater than 0" << endl;}
}
while (n < 1);

double h; // h = step length
h = (u - l) / n; // compute step length

double estimate;

double term_one;
term_one = (h / 3) * (a * pow(l, 5) + b * pow(l, 4) + c * pow(l,3) + d * pow(l, 2) + e * l + f); // h/3 * f(x0)

double term_two;
double sum_one = 0; // sum(x2j)
for (int j = 1; j <= (n / 2) - 1; j++)
{
sum_one = sum_one + (a * pow(l+2*j*h, 5) + b * pow(l+2*j*h, 4) + c * pow(l+2*j*h, 3) + d * (l+2*j*h, 2) + e * (l+2*j*h) + f);
cout << sum_one << endl;
}

cout << sum_one;
//term_two = 2 * sum_one;

double term_four;
term_four = (h / 3) * (a * pow(u, 5) + b * pow(u, 4) + c * pow(u,3) + d * pow(u, 2) + e * u + f); // h/3 * (f(xn)


cout << "Term one =" << term_one << endl;
cout << "term four = " << term_four << endl;
system("pause");


double error;

return 0;
}

When I run this code, the for loop is skipped. If I don't assign sum_one a value (ie double sum_one;) I get an error that sum_one has not been initialized. Basically I need to update sum_one in the for loop, and then say term_two = 2 * sum_one.

Thank you for your assistance.

The code you have above compiles and runs (although I had to remove the system("pause")) . It also compiles and runs if you remove the comment on the start of the line:

 
//term_two = 2 * sum_one; 


I note that at the end, you create a double value called error, that is never used.

Here's an example run and also some warnings from a compile with -Wall switch:

$ ./a.exe
Enter the degree of the polynomial: 65
Error: degree of polynomial must be 4 or 5
Enter the degree of the polynomial: 5
Enter the x^5 coefficient: 5
Enter the x^4 coefficient: 4
Enter the x^3 coefficient: 3
Enter the x^2 coefficient: 2
Enter the x^1 coefficient: 1
Enter the x^0 coefficient: 3
Enter the lower limit for the definite integral: 10
Enter the upper limit for the definite integral: 90
Enter the number of subintervals for the approximation: 6
3.38759e+08
5.49873e+09
5.49873e+09Term one =2.41428e+06
term four = 1.32396e+11

$ g++ -Wall 02.cpp
02.cpp: In function 'int main()':
02.cpp:138: warning: left-hand operand of comma has no effect
02.cpp:129: warning: unused variable 'estimate'
02.cpp:154: warning: unused variable 'error'
So when I run the above code, I keep getting sum_one = 0. For some reason the for loop is not working. Any ideas?
[code] "Your code goes here" [/code]
1
2
3
4
5
6
sum_one = sum_one 
	+ (a * pow(l+2*j*h, 5) 
	+ b * pow(l+2*j*h, 4) 
	+ c * pow(l+2*j*h, 3) 
	+ d * (l+2*j*h, 2) //left-hand operand of comma has no effect
	+ e * (l+2*j*h) + f);

Provide your test case.
Topic archived. No new replies allowed.