P
P
Pbalordorbaor2020-10-29 22:54:27
C++ / C#
Pbalordorbaor, 2020-10-29 22:54:27

Please explain at what point in the code I got a random number in the output?

#include <iostream>

int main() {
    int a = 4668;
    int b = 10415;
    int c;
    for(int i=a; i<b; i++) {
        if(i%3==0 || i%11==0) {
            if(i%2!=0 && i%13!=0 && i%22!=0 && i%33!=0) {
                c++;
            }
        }
    }
    std::cout << c << "\n";
}

What happens in my console:
[email protected]:~/VS Code/test$ g++ programm.cpp -o programm
[email protected]:~/VS Code/test$ ./programm
1680771509
[email protected]:~/VS Code/test$ ./programm
-1092250235
[email protected]:~/VS Code/test$ ./programm
-1058511643
[email protected]:~/VS Code/test$ ./programm
1213003333
[email protected]:~/VS Code/test$ ./programm
-359841931
[email protected]:~/VS Code/test$ ./programm
-127210811


It was assumed that the program, having looked at the numbers in the given range, would select numbers divisible by 3 or 11 and not divisible by 2, 13, 22, 33, and then display their number to the console.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vreitech, 2020-10-29
@Pbalordorbaor

here on this:
int c;
if you do not want to receive such errors, do not forget that before using a variable, it must be declared and initialized. you've only done the first one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question