• 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
  • 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

problem with output in array

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i need to output the index for the lowest value and now all i am able to output is [0][1] im not sure why
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch Mike. Always use code tags while referring to Java code:



Much more readable now.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, and welcome to the Ranch!

In the future, please UseCodeTags(←click) when posting code so it will retain formatting and be readable. I added them for you here, and that helped a little, but your indentation is a bit wonky. Please google for and follow Sun's (Oracle's) Java coding conventions.

Look at this bit:


That says:
1. Initialize i to 1.
2. As long as i is larger than tmp.length, do the following:
2.1 execute the loop body
2.2 increment i
2.3 go back to 2.

Do you see the problem there?

 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff
I am not able to understand what is the logic that Mike is using to find the smallest element in the array of array of double primitives. Can you explain?
 
mike longs
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
would it be the fact that i am looking if i is larger than temp.length when it should be looking for if i is larger than one of indexs in the array ?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mikey mike wrote:would it be the fact that i am looking if i is larger than temp.length when it should be looking for if i is larger than one of indexs in the array ?


No.
Your for loop will only execute if the condition (i > temp.length) is true.
What's the initial value of i. Is that greater than temp.length ?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.
 
mike longs
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i is set to 1, and temp is an array so im not exactly sure what temp.length would even be besides and array thats 11 rows and 1 column( if you count 0 )
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
note that java does not have multi-dimensional arrays. It only has 1-d arrays, but an array can hold...other arrays.

So, your temp array is really an array that holds arrays of doubles. temp holds 12 arrays, so it has a length of 12.

So, the first time you get to your loop, i is 1, and temp.length is 12. So...how many times will your loop run?
 
mike longs
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it would only run one time... thats why i was only getting the number in the first index



and that is able to output any number that is the lowest... but i am still having trouble getting the index to output instead of the actual value
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mikey mike wrote:it would only run one time... thats why i was only getting the number in the first index



actually, it shouldn't run at all.


The firs time you come to this line of code, i is set to 1. temp.length is 12. so, the very first time you hit this line, the condition is false, so you drop out of this for loop entirely - no line of code in the body was ever executed.
 
mike longs
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yea your right but with my updated code


it works fine im just having a problem outputting the index instead of the actual value, i can do this on a one dime array but the 2 dime seems to throw me off
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what do you mean by " the index instead of the actual value". The index of what?
 
mike longs
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok so for the lowest temp in the array is 3 and i keep only outputting the "3" but i am supposed to return the "index of the lowest temp in the array" so it seems to me it would be at [0][1]. or say that it was the six instead of the "3" it would be [1][1]. and im not sure when the index would output what kind of format it would return in
 
mike longs
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok so im pretty sure i figured it out for the min. now i am working on the max and ihave this



i can get it to output the 98 as the max, but if i change indexHighTemp = i , it outputs an 11 instead of a 5. im not sure why my loop is running that many times
and both of these min and max are supposed to be set in one program with indexHighTemp and indexLowTemp as 2 seperate methods and i am having a problem setting the methods
 
Ranch Hand
Posts: 88
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would help if you named your variables for what they really are. indexHighTemp is a double and being used for a temperature and not an index in your code. It should be called highTemp.
 
reply
    Bookmark Topic Watch Topic
  • New Topic