array solution !?

already been answered thanks by Return 0
Last edited on
This is very easy. What code do you have so far?
i've only manage to enter the array of size 10 so far and I think I should do a for loop to check the elements one by one to count them right ?
but I don't know how to write the for loop !
During the loop after the user input and before the next iteration begins, check the user input and increase the count of the appropriate variable. You'll need a variable for each number to keep the count running.

1
2
3
4
5
6
7
8
9
10
for ( int i = 0; i < SIZE; i++ )
{
      cin >> num[i];
      if ( num[i] == 1 )
         num1++;
      if ( num[i] == 2 )
         num2++;
      etc...
}


Edit: Fixed silly error
Last edited on
and after this how do I know which variable to print out so I can get the result of the count ?
All you would be doing is outputting the value of num1, num2, num3, etc...


so the concept that the user enter an element of the array and each element is checked by the if statements to count the numbers and loop the whole thing again but this way I got 1 count for all the numbers why is that
Last edited on
Oops, sorry about that, I meant num[i]. You don't want to be comparing i you want to compare num[i]. i would just be equal to whatever iteration you were on.... I updated my previous example. Also you should have num0 in case 0 is entered.
Last edited on
so how its going to be
ok its fine now thanks a lot !!
Last edited on
Change your if( i == 1 ) to if ( num[i] == 1 )

Also I wanted to ask how particular is your teacher? Because I've had one before who would argue that you aren't using the array in this.
so is there another solution ?
Well there are several options. You could also loop through the array and count the frequency outside of your loop that accepts user input. Which would make you use the array more. I don't know what your professor requires, but both options are fine.
Last edited on
I know time is an issue so if you think you can pull it off START A NEW PROJECT, DO NOT ALTER OR OVER WRITE YOU CURRENT WORKING ONE and code it like Return says. I've had useless TA's with the worst teachers before and I know it isn't fun.
Topic archived. No new replies allowed.