I'm stuck [array problem]

Started by
16 comments, last by 3dburns 20 years, 1 month ago
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]
Advertisement
A simple answer is: use loops.

A more complex answer might be possible if you mentioned what programming language this is.
oh its in c++. I tried using loops but I''m stuck.
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.
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]
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...

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?
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]
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.
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??

This topic is closed to new replies.

Advertisement