• Advertisement

Archived

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

I'm stuck [array problem]

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
A simple answer is: use loops.

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

  • Advertisement