count with array

Mar 21, 2014 at 3:50pm
Hello people, now I'm learning array.
I now know how to count integers with while loop
but I'm not sure how to count the integers with array.
so the question is:
1. program should keep reading integers as long as the integers are within [0,9999]
2. when user typed the integer not between 0 to 9999, the program print out the numbers of integers that were typed.

Sample
3
3
3
9999
9999
-1
You entered 3 3 times.
You entered 9999 2 times.

Can anyone help me with this, I think it is really simple, but I'm stuck from the beggining
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
int main () {
    int numbers[10000];
    int i;
    int n;
    for (i = 0; i >= 0; i = i+1){
        cin >> numbers[i];
    }
    for (
        cout << "You entered " << i << " " << n << " times.\n"; 
    return 0;
}
Mar 21, 2014 at 4:09pm
I think you should use two arrays. One that stores the numbers from 0 - 9999 in one array and the second that stores the numbers of times a certain number from the array is chosen.
Mar 21, 2014 at 4:29pm
I check the first array, and it didn't end when I typed -1 or 10000.
I think there is a problem with the first array, can you tell me what is wrong?
Mar 21, 2014 at 4:40pm
for (i = 0; i >= 0; i = i+1)

If i starts at 0 and continues to be incremented, your condition in the for loop will always be true. It's an infinite loop.
Mar 21, 2014 at 4:46pm
@wildblue
than it's gotta be
 
for (i = 0; i <=9999; i = i+1)

but the problem is that i cannot go below 0
Mar 21, 2014 at 5:06pm
I'm thinking that the user input should be in a while loop - - - you don't know how many times they are going to enter numbers right?

Running a for loop to initialize an array of all possible values could be done with the for loop you have but without the cin - that would mandate that someone enter 10000 values by hand. I don't think you need to do that though.

I think you can just do this with one array with 10000 elements (initialized to 0), and then increment the value of the element corresponding to the number entered by the user, e.g. if they enter 2000, array[2000] gets incremented by 1.
Mar 21, 2014 at 10:44pm
@wildblue
so it's
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream> 
using namespace std; 
int main() 
{ 
    int i=-1;
    int x; 
    int numbers[10000]; 
    for(x=0; x<10000; x=x+1) 
        numbers[x] = 0;
        do{ 
            if(i != -1) 
                numbers[i]=numbers[i]+1; 
                cin >> i; 
}
    while(i>-1 && i<10000);{ 
        for(x=0; x<10000; x=x+1) { 
            if(numbers[x]) cout << "You entered " << x << " " << numbers[x] << " times.\n"; 
} 
}
return 0;
}

my friend helped me doing it,he did the do part, but I didn't learn "do" yet
how can i remove "do"?
Last edited on Mar 21, 2014 at 11:03pm
Mar 21, 2014 at 11:35pm
You can initialize an array to all 0 values without having to go through a for loop.

int numbers[10000]={};

just using a while, not do while...

ask user to enter value within range (i)
while (number is within accepted range)
---array[i] increment by one
---ask use to enter value(i)
Mar 21, 2014 at 11:44pm
but if I take out "do" the code doesn't work...
Mar 22, 2014 at 12:06am
You can't just take out the do in its current location. The while loop syntax starts with a while statement.

There's more info on loop structure and syntax here :) http://www.cplusplus.com/doc/tutorial/control/
Last edited on Mar 22, 2014 at 12:07am
Topic archived. No new replies allowed.