Public Group

#### Archived

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

# I'm stuck [array problem]

This topic is 5261 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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??

1. 1
2. 2
frob
16
3. 3
Rutin
12
4. 4
5. 5

• 13
• 12
• 59
• 14
• 15
• ### Forum Statistics

• Total Topics
632124
• Total Posts
3004243

×