Not generating the 1000 random numbers needed

Asked

Viewed 270 times

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

  • 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

  • Is language C or C++ ?

  • 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?

1 answer

7

Your code has several syntax errors and was not (before editing, which should not be done) well disorganized, this makes it difficult to understand it. Even you who wrote it end up getting lost in it. The nomenclature of variables would also help to better describe what the code does.

But the main problem was the misuse of the operator != (operator testing the difference, that is if the operands are different from each other, that has whichever difference from one to the other) when should use < (operator testing whether the first operand is less than the second). The first does while it is different, ie it is to do nothing. I do not know how I was doing something. The second operator, as the reading itself describes it does while "is less than". So numero starts at 1 and goes on counting until it reaches 1001. While it is less than 1001, it goes on repeating, so it will repeat a thousand times.

#include <stdio.h>
#include <stdlib.h>

int 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);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

In it I did a little different, I put the srand() to start sowing a little better the pseudorandom numbers.

  • Oh yes I understood. One more question. The operator != is the same thing as < ? And does his organization interfere with something when the computer reads the lines and runs the program or is it more a matter of organization? Thank you.

  • No need to do it now, but if you solved your problem you can accept the answer as the correct one. See in [tour] how it works. When you have enough reputation (short) you will be able to vote on all the questions and answers of the site that you find interesting for you, even those that are not related to your questions.

  • Beauty!!! Thank you very much, your help was very helpful. I would like to accept this answer, it is a pity that I can not at this time. But as soon as that is released to me I will do ok? vlew!!

  • I edited it to improve the answer. The organization in general does not interfere for the computer, but it complicates the reading a lot and that is what is really important me programming. Anyway, your code was so disorganized that there were errors that maybe you weren’t even noticing. There are some disorganizations that can become defective. I think accepting the answer you can at any time. You can do it now or wait to see if a better answer comes up. Acceptance you can only ask one answer per question that is yours. And you can change it later if you wish and you have something better.

  • I will create a question with the question of André Ah yes I understood. One more question. The operator != is the same thing as < ? And does his organization interfere with something when the computer reads the lines and runs the program or is it more about organization? Thank you.

  • The operator != checks whether the compared values are different, while the < checks whether the first value is less than the second. Regarding the organization of the code is to make it easier for you (or someone else) to understand the code afterwards and be able to modify it.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.