Question
4. Write a program that generates one hundred random integers between 0 and 9 and displays the count for each number. (Hint: Use rand()%10 to generate a random integer between 0 and 9. Use an array of ten integers, say counts, to store the counts for the number of 0’s, 1’s,…..,9’s.)

this program kinda works but i cant figure out why its making more than 100 random numbers
ls) [*] test2.cpp test3.cpp #include<stdio.h> #include<stdlib.h> int main() 4 { int a[14],is for(i=0;i<100; i++){ a[rand()%10
0 1
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<stdlib.h>
int main(){
// we should initialize the array with 0's otherwise it will have garbage values
// we need array with 10 elements not 100 elements
   int a[10]={0},i;
   for(i=0;i<100;i++){
       a[rand()%10]++;
   }
   for(i=0;i<10;i++)
       printf("%d -> %d\n",i,a[i]);

}

Halli.LPP REO ĐWN 1 #include<stdio.h> 2 #include<stdlib.h> 3. int main(){ I we should initialize the array with @s otherwise

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Answer #1

#include<stdio.h>
#include<stdlib.h>
int main(){
// we should initialize the array with 0's otherwise it will have garbage values
// we need array with 10 elements not 100 elements
   int a[10]={0},i;
   for(i=0;i<100;i++){
       a[rand()%10]++;
   }
   for(i=0;i<10;i++)
       printf("%d -> %d\n",i,a[i]);

}

Halli.LPP REO ĐWN 1 #include<stdio.h> 2 #include<stdlib.h> 3. int main(){ I we should initialize the array with @s otherwise

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
4. Write a program that generates one hundred random integers between 0 and 9 and displays...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT