6
I am developing a C program that randomly generates a thousand numbers and that in the end appears the largest and smallest among them. For this, I am using the rand()
.
But I have a problem. When I run only less than 300 numbers are generated and not 0 to 1000. But 707 to 1000.
Look down at what I’ve done:
include stdio.h
include stdlib.h
include windows.h
main(){
int numero,numero2;
int total1 = 0;
int total2 = 1001;
for (numero = 1; numero != 1001; numero++){
numero2 = rand () % 1000;
printf("Numero %d: %d \n ",numero,numero2);
if (numero2 > total1){
total1 = numero2;
}
if (numero2 < total2){
total2 = numero2;
}
}
printf("\n");
printf("O maior numero e: %d \n\n",total1);
printf("O menor numero e: %d \n\n",total2);
system("pause");
return 0;
}
Where am I going wrong?
it would be good to start, indent the code correctly, so it is easier for everyone to know the error, until it makes your life easier
– Joannis
and the error is in the part of Return 0 in the middle of an if that in my view should be out of the for, and even then your application will not work... I would advise making from scratch
– Joannis
Is language C or C++ ?
– Tony
Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?
– Maniero