menu

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

by
1 vote
#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's going on in my console:
cyrill@monkey:~/VS Code/test$ g++ programm.cpp -o programm
cyrill@monkey:~/VS Code/test$ ./programm
1680771509
cyrill@monkey:~/VS Code/test$ ./programm
-1092250235
cyrill@monkey:~/VS Code/test$ ./programm
-1058511643
cyrill@monkey:~/VS Code/test$ ./programm
1213003333
cyrill@monkey:~/VS Code/test$ ./programm
-359841931
cyrill@monkey:~/VS Code/test$ ./programm
-127210811
The program is supposed to look through the numbers in a given range, select the numbers divisible by 3 or 11 and not divisible by 2, 13, 22, 33, and then display their number in the console.

1 comment

at what point in the code did I get a random number in the output?

int c;

1 Answer

by
 
Best answer
0 votes
on this one:
int c;
If you don't want to get errors like that, don't forget to declare and initialize the variable before using it. you only did the former.