• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# I'm stuck [array problem]

## Recommended Posts

I'm doing a program where I have to ask the user to input as many numbers as they want as long as they are between 1 and 50. (Yes, it's for school.) Now, I know how to do that but I have to make a Histogram that shows how many numbers the user inputed. (Example: If Entered 4,5 and 50, the program should say: 1-5 ** ... 45-50 *) I have my array set up so that when the user inputs their number, a counter for that particular number incriments up one. So if I enter 3 then it takes Nums[3-1]++. So I have the actual number of times each number is inputed but I need to combine them all in fives. (1-5, 6-10 etc) I've thought hard about how to do it but after two wasted class periods, I'm stuck. Any help or suggestions would be very appreciated. [edited by - 3dburns on March 20, 2004 2:31:47 AM]

##### Share on other sites
A simple answer is: use loops.

A more complex answer might be possible if you mentioned what programming language this is.

##### Share on other sites
oh its in c++. I tried using loops but I''m stuck.

##### Share on other sites
When you say you''re stuck do you mean you can''t get the code to compile or the code doesn''t do what you want, or you just can''t figure out the syntax for what you want to do? Try showing the code for what you tried.

##### Share on other sites
You need to use some kind of loop with a if-else if - else block.

[edited by - yspotua on March 20, 2004 2:48:35 AM]

##### Share on other sites
I just can''t figure out what to do.

I already have an array of 50 int''s I just need to combine all those counters down to counters of numbers between 1-5, 6-10 etc...

##### Share on other sites
So for the first histogram bar you want the number of 1s plus the number of 2s plus the number of 3s plus the number of 4s plus the number of 5s, right? What happens if you try to turn that English into code?

##### Share on other sites
int x = 1;while (x <= 5) {  Hist[0] += Nums[x-1];  x++;}

that would work wouldn't it. should i just right 9 more while loops like that or...

[edited by - 3dburns on March 20, 2004 3:02:30 AM]

##### Share on other sites
That doesn''t look like it would work because you''re never incrementing x. You''ll just add Nums[0] to Hist[0] over and over again. But it seems like you''re on one right track.

##### Share on other sites
ok so i fixed that but i need a loop around that so i can do the 6-10 and all the following counters...any ideas??

##### Share on other sites
Well six to ten is a lot like one to five. Except every number is five bigger.

##### Share on other sites
int x=1;int y=5;while (x <= 50) {  int count=1;  while (x <= y) {     Hist[count-1] += Nums[x-1];     x++;  }  count++;  y += 5;}

that should work right...i hope.

##### Share on other sites
I''m wondering if using a for loop is much nicer to use than a while loop. You code would condense to the following

for ( int x = 1 ; x <= 5 ; x++ )
Hist[0] += Nums[x-1];

This is much nicer to read and it''s good to start getting a better style early.

All you need to do now is put in another loop for the histogram array, ie. replace the "0" in Hist[0] with another loop variable. You would essentially place your current code inside another loop and do a little extra maths when referencing the Nums elements.

I could just type the answer but you''ll learn so much more if you work that bit out.

Good luck.

R

--------------------------------------------------------------------------
There is no point in flaming if you''ve merely poured fuel on your own head

##### Share on other sites
quote:
Original post by 3dburns
that should work right...i hope.

You'll need to move the int count=1; to before the first while. Right now count will get reset everytime through the loop, so you'll add every number to your first histogram box.

[edited by - SiCrane on March 20, 2004 3:17:42 AM]

##### Share on other sites
for (int y = 0; y < 10; y++) {    for (int x = ((y+1) * 5) - 4; x <= (y+1) * 5; x++) {        Hist[y] += Nums[x-1];    }}

I think that should do it.

Yea??

##### Share on other sites
Looks fine to me.

##### Share on other sites
Finally after two days of drawing up blanks i get it in the middle of the night. Thanks for all the help. For future reference should I put this type of post in For Beginners or General Programming.

##### Share on other sites
Excellent !

I hate to pick nits but the following is also good. Mainly because the calculations are confined to one place and that the loop variables are kept simple to it''s easy to see how many loop are actually going to be performed.

for (int y = 0; y < 10; y++)    for (int x = 0 ; x < 5; x++)        Hist[y] += Nums[(y * 5) + x];

R

--------------------------------------------------------------------------
There is no point in flaming if you''ve merely poured fuel on your own head