So ive tried to use the rand() command in C, in this case to generate random number between 1 to 6. that part went perfectly, but then i tried to use the switch method to count how many times each number apeared, and the results were... weird.
heres the code:
include
int main() {
int num;
int number;
int one;
int two;
int three;
int four;
int five;
int six;
printf("enter a number: ");
scanf("%d", &num);
for(int n = 1; n < num + 1; n++){
number = (rand() % 6) +1;
printf("%d", number);
switch(number){
case 1:
one++;
break;
case 2:
two++;
break;
case 3:
three++;
break;
case 4:
four++;
break;
case 5:
five++;
break;
case 6:
six++;
break;
}
}
printf("number of ones: %d\n"
"number of twos: %d\n"
"number of threes: %d\n"
"number of fours: %d\n"
"number of fives: %d\n"
"number of sixes: %d\n",
one, two, three, four, five, six);
return 0;
}
when i ran it and wrote in 10, 10 random numbers realy apeared, but these are the results:
number of ones: 1978168284
number of twos: -2
number of threes: 1307605979
number of fours: 1978192464
number of fives: 6422480
number of sixes: 6422299
I looked at the code a few times and I just have no idea how that happend... if anyone has an edvice of fixisg that, please tell me (:
Aucun commentaire:
Enregistrer un commentaire