Programming Assignment - I dont understand what he wants me to do?

Design a computer programming experiment in C++ to determine what happens when
a variable of type int is incremented above its range or decremented below its range. Repeat your experiment to determine what happens with a short int and long int. Next, modify your experiment to find out how unsigned variants of the integer types behave when their range is exceeded. Finally, try your experiment with a float and double.
He just wants you to create a variable of type int, and set it to its max. From there, increment it above that, and display the result. Repeat with going lower than the minimum. From there, do it with short, long, double, float, and unsigned variants of each.
Can you give me an example because I am a beginner and I'm not sure how to do that?
> Design a computer programming experiment in C++ to determine what happens when
> a variable of type int is incremented above its range or decremented below its range.
> Repeat your experiment to determine what happens with a short int and long int.

What happens is undefined behaviour. Technically, anything can happen; different things may happen if you compile with different options; different things may happen with different compilers; different things can happen each time you run the program etc. Experiment? Only if we use the term in a very loose sense.


> find out how unsigned variants of the integer types behave when their range is exceeded.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <limits>

int main()
{
    // increment an unsigned int beyond its range
    unsigned int a = std::numeric_limits<unsigned int>::max() ;
    std::cout << a << '\n' ;
    ++a ;
    std::cout << a << '\n' ;

    // decrement an unsigned int beyond its range
    a = std::numeric_limits<unsigned int>::min() ;
    std::cout << a << '\n' ;
    --a ;
    std::cout << a << '\n' ;
}

Like how would I do this all of these in one program?
>_> you dont, unless you use cin to direct an output.

*edit: or you could just show them one after another.
Last edited on
> I have to write a three to five page paper on this question after I write the code,
> so how would I do this without having to include header files like #include <limits>.
> He is wanting me to hardcode it

For an unsigned integral type, 0 is the minimum and -1U converted to the unsigned type would be the maximum.

For a floating point type, without <limits>? Does he expect you to play around with +inf and -inf.

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
#include <iostream>
#include <limits>

int main()
{
    {
        // increment an unsigned int beyond its range
        unsigned int a = -1U ;
        std::cout << a << '\n' ;
        ++a ;
        std::cout << a << '\n' ;

        // decrement an unsigned int beyond its range
        a = 0 ;
        std::cout << a << '\n' ;
        --a ;
        std::cout << a << '\n' ;
    }

    {
        // increment a double beyond its range
        double a = +1.0 / 0.0 ; // +inf
        std::cout << a << '\n' ;
        a += 0.12345 ;
        std::cout << a << '\n' ;

        // decrement a double beyond its range
        a = -1.0 / 0.0 ; // -inf
        std::cout << a << '\n' ;
        a -= 0.12345;
        std::cout << a << '\n' ;
    }

    // etc.
}
Im just a beginner, He wont let me turn all of this in like that. He probably understands it but it has to be what we have gone over yet and he hasn't gone over this yet
Topic archived. No new replies allowed.