I need to generate 5 random numbers in a range between -10 and +10. How could I do this? I've already seen that you can use shuffle
, malloc
or even realloc
, but being new to C, I do not quite understand how I can make it work. If anyone can help me, I'll be grateful.
The only part of my code, it's only that I have created the array with size 5, so I can then try to generate the random numbers.
Code:
include <stdio.h>
include <stdlib.h>
include <locale.h>
int main() {
setlocale(LC_ALL,"portuguese");
int num[5] // este será a variável que irei usar para gerar números aleatórios.
return 0;
}
I also want to check if the numbers that were randomly generated are positive or negative. But when doing the verification, even if it gives 5 positive numbers, it places negative as 1
.
Code:
int positivo;
int negativo;
if(num[i] > 0) {
positivo += 1;
}
else if(num[i] < 0 ) {
negativo += 1;
}
printf("\nPositivo(s) : %d ",positivo);
printf("Negativo(s) : %d ",negativo);