trouble with array

Hello everyone i am tryin to get the solution
to this but i have difficulty solving it
write the codes randomly generate 10 numbers (between 1 to 5) and stores every single number into an array. and then count the number of occurrences of each particular number in the array and display the finished program

a finished test
Enter number : 2
Enter number : 2
Enter number : 3
Enter number : 1
Enter number : 2
Enter number : 3
Enter number : 1
Enter number : 3
Enter number : 2
Enter number : 2

1 appears 2 times
2 appears 5 times
3 appears 3 times
these are my codes so far any help for me?

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
int main{
srand(time(NULL))

for(int x=1; x<=10;x++){
int x =(rand()%10)+1;

int y[9]={x, x, x, x, x, x, x, x, x, x}
for(int a=0;a<=9;a++){
cout<<"Enter number:
cout<<y[a];

}
}
}
Last edited on
There are a lot of problems in your code. Please use code tags. And indentation as well.

1)What is this?int y[9]={x, x, x, x, x, x, x, x, x, x}
It isn't doing what you expect it will do.

2)The program definition asks you to generate 10 random numbers between 1-5. You are generating a number between 1 and 10.

3) Think out an algorithm for the program, and then proceed. By the looks of your code, it seems that you just wrote it for the heck of it, and probably use trial and error as a methodology... Don't do that...
Last edited on
i am quite new at C++ and i did use effort
i hope you are forgiving to a new user and hope
for help from everyone
Yea this needs a lot of work. You really need to learn about declaring and initializing arrays. I would work on that, then think about the logic of your program before you do anything else. We're always happy to help, but we won't do it for you. Any more questions, feel free to ask.
You need an array with 5 elements. Set it to 0 like so: int y[5] = { 0 };. Place it before the loop.

The random number must be 5 not 10: int r =(rand()%5); // Don't reuse x! Don't add 1!
Use 'r' as an index of the y array and increase the content of the field

The first for loop ends now! i.e. place the } before the second loop


The second for loop goes from 0 to 5 (with < not <=)
Last edited on
you dont need stdlib.h time.h should be ctime and stdlib.h should be cstdlib
@coder777: he does need 10 items. You're correct about the random number's UB being 5, but wrong about the implementation. To generate a random number in [1,5], you need rand()%5+1;. He was correct, except that he generated [1,10].

@cplusstory:
A variable (or array) should only be initialized. Do this first. Then, values are assigned. An array with ten ints is initialized as so: int myInts[10];, and it will have 10 ints from myInts[0] to myInts[9]. The last element is always at size-1.

In C(++), variables can only hold values, not a function prescription. An example:
int a = 6, b = 5, c = a+b;
c now equals 11, not "a+b". This means any change to a and b will not affect c. In your code:
int y[9]={x, x, x, x, x, x, x, x, x, x};
'x' would always be the same value, decided at the moment a value is assigned to x. Yes, y[i] will be a random number, but it will be the same for all i.

Lastly, you have no counting code whatsoever.
Topic archived. No new replies allowed.