I'm stuck [array problem]

Started by
16 comments, last by 3dburns 20 years, 1 month ago
Well six to ten is a lot like one to five. Except every number is five bigger.
Advertisement
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.
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
R--------------------------------------------------------------------------There is no point in flaming if you've merely poured fuel on your own head
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]
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??
Looks fine to me.
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.
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
R--------------------------------------------------------------------------There is no point in flaming if you've merely poured fuel on your own head

This topic is closed to new replies.

Advertisement