2
I developed a C code to generate a sequence of random numbers to be typed by the user, but it’s the first time I make a source code of the type for a college job, and according to the research I did I was able to come up with an idea:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
int i;
int quant_random; //Quantidade de aleatórios gerados
inicio:
printf("Escreva uma quantidade de numeros aleatorios a serem gerados: ");
scanf("%d", quant_random);
if (quant_random <= 0 || quant_random > 100)
{
printf("\nValor incorreto, digite um numero entre 1 e 100");
goto inicio; //Estou usando esse goto para levar ele para a frase de novo quando a pessoa digitar um valor incorreto
}
else
{
srand(100); //Modelo que encontrei em site como base, só não estou entendendo a lógica de como ele funciona
for (i = 0; i < 5; i++)
{
/* gerando valores aleatórios entre zero e 100 */
printf("\n\n%d ", rand() % 100);
}
getch();
}
return 0;
}
When executing it, however, it asks to enter as a value, but under both conditions: entering a code in the range from 1 to 100 or even outside this scope, it does not show any return. Because as I put in if it is less than or equal to 0 or greater than 100 it should return to the initial message through the goto. Someone can understand the logic to hit him?
The statement says that the number typed by the user is the amount to be random numbers to be generated.
Missed the
&
inscanf("%d", quant_random);
. Note how you are starting from a fixed seed (srand(100);
) the sequence of pseudo-random numbers generated will always be the same every time you run the program. Avoid using droplet.– anonimo