Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Minimum Value and Sum of Values in Array

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Sheriff
Posts: 26798
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The length of the array is 15 and I'm assigning values for 0-14... ?
 
author
Posts: 23908
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23908
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 22461
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23908
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 74085
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It worked all right when I tried it.
 
Joe Saunders
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 26798
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, . Thank you guys.
 
There is no "i" in denial. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic