Help


// Starting with x=-3, going up by 0.5 each time untill it reaches 3.0


#include <iostream>
using namespace std;


int main() {

double x;
int y=x;



for(int x=-3; x<=3.0; x = x+0.5)
{
cout <<x<< endl;

if (x<=3.0)
cout <<"y is:"<<x<< endl;
}


return 0; }

it doesnt seem to be addidng by 0.5 like i want it to and it stops at 0
instead of 3.0. Am i using the wrong loop for this or am i missing something.

any advice would be much appriciated.





Last edited on
Hi,

Have a read of this, particularly the fundamental data types, understand what an int can store:

http://www.cplusplus.com/doc/tutorial/variables/

Now this part:

http://www.cplusplus.com/doc/tutorial/control/

Realise that the type of the variables for the initial value, the increment value and the end condition in the loops should be an integer type.

Work out how many times you need to loop as an integer, use that with the for loop. Inside the loop have that initial value as a double type (a separate variable) , and do the addition with it.

Note that the float or double types don't represent numbers exactly, so don't be tempted to use a double in the for loop.

Good Luck !!
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()
{
    double y = 0;
    
    for( double x = -3; x <= 3.0; x = x + 0.5)
    {
        cout << "x is: " << x << endl;
        
        y = x;
        
        if (x <= 3.0)
            cout <<"x is still <= 3.0 and y is: "<< y << endl << endl;
    }
    
    return 0;
}


Read this code carefully in conjunction with what TheIdeasMan writes and you might be at the start of a successful introduction to C++
( I'm a bit more relaxed about using double in this loop so you'll have to make a choice on that. Double and float are necessarily approximations but the effect here is negligible or non-existent, but you need to be aware of it.)

Instead of x = x+ 0.5 you can show your expertise and use x += 0.5 instead.

You need to write tidy code! :)
Double and float are necessarily approximations but the effect here is negligible or non-existent, but you need to be aware of it.


The reason one needs to be aware of it, and not do it :+) is that it's possible to get off by one errors.

It might work, but there is a very good chance it won't. It's possible that x has the value you want plus 3e-16 say, so the loop executes 1 time less than what one might have expected.

closed account (48T7M4Gy)
OP, I think TIM's having a lend of us.

Integers are ideal and appropriate for loop counters (obviously) but there's no general restriction on using doubles or floats in the for-loop or any other equivalent control structure. Good computing practice dictates that the results of any computation are checked for accuracy and they are within acceptable tolerance. We wouldn't write a line of code otherwise. :)
Ah well, anyone can go against accepted practise - at their peril. That's all I am saying about what looks like to be yet another troll topic.
closed account (48T7M4Gy)
Don't be so rude TIM. You make an assertion that is completely unfounded and now you want to call me a troll. I doubt whether you have either the training or experience to know what is accepted computing practice. You obviously haven't had any training in numerical methods. And the same goes for your manners.
It's easy to get caught out with floating-point values. Some values such as 0.5 can be represented exactly. But in general one should expect the possibility that the value might be an approximation.

Consider for example,
1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{    
    for (double x = 0; x <= 3.0; x += 0.5)
    {
        std::cout << "x is: " << x << '\n';
    }
}
x is: 0
x is: 0.5
x is: 1
x is: 1.5
x is: 2
x is: 2.5
x is: 3

Now the same program, but instead of incrementing by 0.5, use 0.2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{    
    for (double x = 0; x <= 3.0; x += 0.2)
    {
        std::cout << "x is: " << x << '\n';
    }
}
x is: 0
x is: 0.2
x is: 0.4
x is: 0.6
x is: 0.8
x is: 1
x is: 1.2
x is: 1.4
x is: 1.6
x is: 1.8
x is: 2
x is: 2.2
x is: 2.4
x is: 2.6
x is: 2.8

Notice how in the first case, the last line was
x is: 3

But in the second, the last line is
x is: 2.8

Why the difference?
Shouldn't it be 3 in both cases? Well, 0.2 is an approximation, while 0.5 is a special case of a number which happens to be exact.

Try this and see what is the output:
1
2
    std::cout.precision(20);
    std::cout << 0.2 << '\n';



closed account (48T7M4Gy)
The potential for errors arising from the choice of data type and any inherent imprecision in the numbers has absolutely nothing to do whether a for-loop is used. The imperfection arises because of the '<=' part of the condition statement.

It doesn't matter where that condition is used in the program, the same cautions apply.

The whole exercise in niggling about precision instead of addressing it as a properly trained and experienced programmer would in a realtime situation, is a red herring.
Last edited on
Topic archived. No new replies allowed.