This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Dec 16, 2012 6:16 AM by 968909 RSS

Array - return greatest value in the array

968909 Newbie
Currently Being Moderated
I am trying to write a program that for the method 'greatest' it will return the greates value in the array between the elements beginning and end.

Below is my code. No errors but I am not getting the desired results.

My output:
2
4
1
5
3
6


My code:
public class Test {
     
     public void printArray(int x[]){
          for (int i=0; i < x.length; i++)
               System.out.println(x[i] + "");
              System.out.println("\n");
     }

     public static int greatest(int x[], int beginning, int end){
          
          int i;
          int index = 0;
          int largest = 0;
     
          
          for(int z = 0; z < x.length; z++)
          {
             if(largest < x[z])
              {
                largest = x[z];
                index = z;
               }
          }
          return index;
     }
     
     public static void main(String[] args){
          Test program = new Test();
          int a[] = {2,4,1,5,3,6};
          int val = greatest(a,0,a.length-1);
          program.printArray(a);
   }
}
Please, I just would like someone to point out where I need to focus. I do not want anyone to write this. Yes - its for college.
Please and Thank you
  • 1. Re: Array - return greatest value in the array
    DrClap Expert
    Currently Being Moderated
    Focus on the names of your variables. Which one contains the "greatest value"?
  • 2. Re: Array - return greatest value in the array
    Kayaman Guru
    Currently Being Moderated
    SunshineJava wrote:
    Below is my code. No errors but I am not getting the desired results.
    When you ask for help, you need to tell if there are any errors and what they are, as you did.
    But you also need to tell what your desired result is.

    If that code is what you're using, there are several things off. First of all, you're not even displaying the index of the largest number (which would be wrong with your current code anyways).

    You're also not using the end variable in your method.
  • 3. Re: Array - return greatest value in the array
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    SunshineJava wrote:
    I am trying to write a program that for the method 'greatest' it will return the greates value in the array between the elements beginning and end.
    Instead of trying to invent the wheel a third time I'd trust in existing code:
    Arrays.sort(x);
    Assert.assertEquals("last Value is biggest", 6,x[x.length-1]);
    And hopefully this is what your teacher wanted from you...

    bye
    TPD
  • 4. Re: Array - return greatest value in the array
    Kayaman Guru
    Currently Being Moderated
    TPD Opitz-Consulting com wrote:
    And hopefully this is what your teacher wanted from you...
    They're not trying to learn the Java API, but the logic behind it.
  • 5. Re: Array - return greatest value in the array
    EJP Guru
    Currently Being Moderated
    And he explicitly said he doesn't want anybody to write it for him. Please read the question.
  • 6. Re: Array - return greatest value in the array
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    Kayaman wrote:
    They're not trying to learn the Java API, but the logic behind it.
    .
    EJP wrote:
    And he explicitly said he doesn't want anybody to write it for him. Please read the question.
    So the point is how to use the <tt>for</tt> loop and I shoud better have written that the OP should use the method parameters in the appropriate places in the <tt>for</tt> loop declaration?

    Damn, I'm too focussed on short solutions...
    ;o)

    bye
    TPD
  • 7. Re: Array - return greatest value in the array
    968909 Newbie
    Currently Being Moderated
    Thank you for all of your responses but please note - I am a she !

    Someone asked, I should put what the output is that I am expecting - Its in the first line. The greatest value of the array between beginning and end.

    So I have written methods and passed values. And I have done so in an array. The last program I had to search the array and return the 4th index.

    This one we are passing 3 parameters. This program should return the greatest number in the array.

    I did some more reading based on the comments and here is what I came up with:
     
     
    public class Test {
          
           public void printArray(int x[]){
                  for (int i=0; i < x.length; i++)
                         System.out.println(x[i] + "");
                      System.out.println("\n");
           }
     
           public static int greatest(int x[], int beginning, int end){
                 
            int count = 0;
            
            for (int i = 0; i < x.length; i++) {
                 if (x[i] >= beginning && x[i] < end) count++;
            }
            
            return count;
         }
          
           public static void main(String[] args){
                  Test program = new Test();
                  int a[] = {2,4,7,9,1,6};
                  int val = greatest(a,0,a.length-1);
                  program.printArray(a);
       }
    } 
    It runs. No errors. Out put just prints the numbers in the array. I need it to print the greatest value.

    And I am not sure I completely understand this statement:
    int val = greatest(a,0,a.length-1);
    val is the variable
    greatest(a, 0, a.length-1); is calling the method greatest
    a = the array? not sure on '0' and a.length-1?

    Thoughts?

    Edited by: SunshineJava on Dec 6, 2012 5:15 PM
  • 8. Re: Array - return greatest value in the array
    Kayaman Guru
    Currently Being Moderated
    SunshineJava wrote:
    Thank you for all of your responses but please note - I am a she !
    We'll try to keep that in mind.
    It runs. No errors. Out put just prints the numbers in the array. I need it to print the greatest value.
    You need to print out the val variable, that's where the return value will be kept from the method.
    And I am not sure I completely understand this statement:
    int val = greatest(a,0,a.length-1);
    val is the variable
    Yes, val is the variable you introduce to save the return value from the method.
    greatest(a, 0, a.length-1); is calling the method greatest
    a = the array? not sure on '0' and a.length-1?
    Yes, a is the array, 0 is the beginning index of the array and a.length-1 is the last index of the array.
    Thoughts?
    The logic in the method greatest() is wrong:
    if (x[i] >= beginning && x[i] < end) count++;
    That translates to "if the value in the array at the current index is bigger than 0 and smaller than the length of the array, increase count".
    You should instead keep track of the biggest value you have at the moment, and its index. Then you can compare against the biggest value.

    It helps to write out the logic on paper first, then translate that into code.
  • 9. Re: Array - return greatest value in the array
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    SunshineJava wrote:
    Thank you for all of your responses but please note - I am a she !
    Does this matter anyhow?
    You know what gender you are and our answers will not change because of this.
    public class Test {
    int count = 0;
    Your requrement does not mention a count so why are you counting the array elements?
            for (int i = 0; i < x.length; i++) {
         if (x[i] >= beginning && x[i] < end) count++;
    }
    You should use <tt>beginning </tt> and <tt>end</tt> in the <tt>for </tt> statement directly. This would express better what's intended.
    And again: by your requirement you are not supposed to count the elements, so why do you do that?
    Instead you should do some comparing here since you need to compare values to find out which one is the bigger.

    bye
    TPD
  • 10. Re: Array - return greatest value in the array
    968909 Newbie
    Currently Being Moderated
    Kayaman - once again you have been helpful. Thank you.

    So I went to the paper and thought about the logic and rewrote what I had in the method greatest.

    * I am passing to the method, successfully calculating the greatest value, but not returning a varialble so the variable val is still not used. Or if I am returning a value I dont see it. *

    So, I need to return the value and then call the printArray method to print the outcome.

    What line needs to be addressed - remember anyone else reading this, please do not write it for me :-)
         public static int greatest(int x[], int beginning, int end){
              
               int max = x[0];
    
                 for (int i = 0; i < x.length; i++)
                 {
                  if (max < x)
         max = x[i];
         }
         
         System.out.println("The greatest: " + max);
         
         return max;
         }
     
    
    Please and Thank you :-)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  • 11. Re: Array - return greatest value in the array
    968909 Newbie
    Currently Being Moderated
    TPD wrote: 
    Does this matter anyhow?
    You know what gender you are and our answers will not change because of this.
    Yes it does matter. Everyone was saying 'he' this.. 'he' that... I just wanted to mention that I am not a 'HE".
    I do not expect special treatment or a change in the answers.
    I am here trying to learn.
    I was just clarifying. It would do you good to learn some manners :-)
  • 12. Re: Array - return greatest value in the array
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    SunshineJava wrote:
    I was just clarifying. It would do you good to learn some manners :-)
    Didn't notice that remembering someones gender is a "manner" in a morre or less anonymous forum...
    I thought its a manner to write in a way that neither male nor female readers are offended...

    bye
    TPD
  • 13. Re: Array - return greatest value in the array
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    SunshineJava wrote:
    So, I need to return the value and then call the printArray method to print the outcome.
    You don't change the array, so your print method does not show the outcome.

    You <tt>greatest</tt> method does 2 things: it actually finds the greatest value and prints it. (Remember that there is a separation of concerns principle.)

    Move the line that prints the maximum value to the calling method.

    bye
    TPD
  • 14. Re: Array - return greatest value in the array
    morgalr Explorer
    Currently Being Moderated
    SunshineJava wrote:
    I am trying to write a program that for the method 'greatest' it will return the greates value in the array between the elements beginning and end.

    Below is my code. No errors but I am not getting the desired results.

    My output:
    2
    4
    1
    5
    3
    6


    My code:
    public class Test {
         
         public void printArray(int x[]){
              for (int i=0; i < x.length; i++)
                   System.out.println(x[i] + "");
                  System.out.println("\n");
         }
    
         public static int greatest(int x[], int beginning, int end){
              
              int i;
              int index = 0;
              int largest = 0;
         
              
              for(int z = 0; z < x.length; z++)
              {
                 if(largest < x[z])
                  {
                    largest = x[z];
                    index = z;
                   }
              }
              return index;
         }
         
         public static void main(String[] args){
              Test program = new Test();
              int a[] = {2,4,1,5,3,6};
              int val = greatest(a,0,a.length-1);
              program.printArray(a);
    }
    }
    Please, I just would like someone to point out where I need to focus. I do not want anyone to write this. Yes - its for college.
    Please and Thank you
    Change this line:
     program.printArray(a);
    to this:
    System.out.println(a[val]);
    You make the correct calculation, you just never use it.
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points