• Post Reply Bookmark Topic Watch Topic
  • New Topic

Minimum Value and Sum of Values in Array  RSS feed

 
Joe Saunders
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jittu goud
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


what is the length of the array --and how many values are you assigining in your loop
 
Joe Saunders
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The length of the array is 15 and I'm assigning values for 0-14... ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked all right when I tried it.
 
Joe Saunders
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's odd... What would cause you to get the correct value, but for me to get something way too high??
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, . Thank you guys.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!