programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Minimum Value and Sum of Values in Array

Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
For whatever reason, the for loop can find the maximum value of the randomly generated array O.K., but when it tries to find the minimum or sum the array it is wrong.
Any help is appreciated! I've been working on this for days though it should be easy enough...
Thanks in advanced.

Marshal
Posts: 28261
95
• Number of slices to send:
Optional 'thank-you' note:
The minimum is wrong? In what way? What did you expect, and what did you get instead? (I can see how the sum is wrong, but it isn't obvious to me why the minimum is wrong.)

Joe Saunders
Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
It gives me a value of 0 for the minimum value every time.
Sorry, I put the sum back to "sum = sum + number[a]". I had changed this when trying to get it to work, however even this still doesn't calculate the sum.

Ranch Hand
Posts: 46
• Number of slices to send:
Optional 'thank-you' note:

what is the length of the array --and how many values are you assigining in your loop

Joe Saunders
Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
The length of the array is 15 and I'm assigning values for 0-14... ?

author
Posts: 23951
142
• Number of slices to send:
Optional 'thank-you' note:

Joe Saunders wrote:The length of the array is 15 and I'm assigning values for 0-14... ?

Really?!? Take a look again. You are assigning from 0 to a value that is (<14). Is 14 less than 14?

Henry

Joe Saunders
Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
Okay, that makes sense . The minimum value is working now, but the I still get a much too high of a number for the sum.

jittu goud
Ranch Hand
Posts: 46
• Number of slices to send:
Optional 'thank-you' note:

Joe Saunders wrote:Okay, that makes sense . The minimum value is working now, but the I still get a much too high of a number for the sum.

if that made sense ...then sum should have made some sense too

Joe Saunders
Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
I'm looking, I don't see the issue... it would make sense I was getting a very large sum if I was accidentally assigning too many values to the array, but from what I see, I'm not..

Henry Wong
author
Posts: 23951
142
• Number of slices to send:
Optional 'thank-you' note:
Well, you have the same issue with sum, as with the other loops -- but that will cause the sum to be slightly too small, not too big (and I assume that you fixed that loop already).

BTW, just how big a value are we talking about here?

Henry

Sheriff
Posts: 22785
131
• Number of slices to send:
Optional 'thank-you' note:

Henry Wong wrote:BTW, just how big a value are we talking about here?

From 1 to 100 (inclusive), so with only 15 values there should not be any overflow.

Joe, what is the sum you are getting? It should be anywhere between 15 (all ones) and 1500 (all hundreds). Well, after you've fixed your loop condition, otherwise it's between 14 and 1400.

Joe Saunders
Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
I understand why it should be < 1500 and > 15 but I get numbers with 5 digits such as 75615, depending on which values are assigned.

Henry Wong
author
Posts: 23951
142
• Number of slices to send:
Optional 'thank-you' note:

Joe Saunders wrote:I understand why it should be < 1500 and > 15 but I get numbers with 5 digits such as 75615, depending on which values are assigned.

Please post your latest code -- as it isn't possible to get what you are describing with your previous code.

Never mind.... I think I figured it out. Hint: is "75615" really the sum? Or is the sum "756"?

Henry

Joe Saunders
Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
This is my latest code.

I thought that might be the issue Henry, however it doesn't seem to be that way.

To show... I ran the program and get these numbers:
83, 19, 30, 55, 7, 28, 49, 10, 15, 9, 32, 39, 74
The sum should be 450, however I receive a sum of 53144 from the program.

Marshal
Posts: 79412
377
• Number of slices to send:
Optional 'thank-you' note:
It worked all right when I tried it.

Joe Saunders
Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
That's odd... What would cause you to get the correct value, but for me to get something way too high??

Paul Clapham
Marshal
Posts: 28261
95
• Number of slices to send:
Optional 'thank-you' note:
How many numbers are in that list? (Hint: you have too many magic numbers in your code.)

And why are you printing your results in such a strange way? (Hint: change it to print an exclamation mark after the sum and you'll see why I call it strange.)

Joe Saunders
Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
Oh, . Thank you guys.

reply
Similar Threads