Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Array Problem

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Array Problem

    The exercise:
    Given an array of scores, return true if each score is equal or greater than the one before. The array will be length 2 or more.

    scoresIncreasing({1, 3, 4}) → true
    scoresIncreasing({1, 3, 2}) → false
    scoresIncreasing({1, 1, 4}) → true

    public boolean scoresIncreasing(int[] scores) 
    {
      for (int i = 0; i< scores.length-1; i++)
      {
      if (scores[i] < scores[i+1] || scores[i] == scores [i+1])
      {
      return true;
      }
      else 
      {
      return false;
      }
      }
      return false;
    }

    Unsure what the problem is, it will always return True for some reason...


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Array Problem

    What do you get when you try this:

    scoresIncreasing({3, 2})

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Array Problem

    --- Update ---

    [/COLOR]NVM, I am a retard. I tested {3,2} and got False.

    class Test
    {
    	void scoresIncreasing(int[] scores) 
    	{
    	  for (int i = 0; i< scores.length-1; i++)
    	  {
    	  if (scores[i] < scores[i+1] || scores[i] == scores [i+1])
    	  {
    	  System.out.println("true");
    	  }
    	  else 
    	  {
    	  System.out.println("false");
    	  }
    	  }
    	}
    }
     
     
    public class App {
     
    	public static void main(String[] args) {
    		Test test1 = new Test();
    		int [] scores = {3,2};
    		test1.scoresIncreasing(scores);
    	}
     
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Array Problem

    Fixed then?

  5. #5
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Array Problem

    Quote Originally Posted by hkfrenchtoast View Post
    --- Update ---
    NVM, I am a retard. I tested {3,2} and got False.
    So why does {3, 2} return false while {1, 3, 2} return true? What happens when the loop runs the first time?

  6. #6
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Array Problem

    I ran the code again with {1,3,2} and got

    Output:
    true
    false
    I am not sure what the problem is. I forgot to mention that the assignment is from CodingBat AP-1 question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    U get true and false because it compares 3 with 1 and 2 with 3. Try remove the code after or and see what happens.

  8. #8
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Array Problem

    Removing "|| scores[i] == scores [i+1]" ?

    That does not work. The output is still: true false. Besides, I need it to complete the assignment (return true if each score is equal).

    I know what seems to be the problem, however. The for loop. It compares 3 with 1 and 2 with 3, like you said. The question is...how can I make it so it only checks the numbers and not put the "true or false" until the last two numbers of the array has been checked.

  9. #9
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Array Problem

    Sorry to bump this, but I really want to know how to make the code work. :/

  10. #10
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Array Problem

    use boolean return type instead void it's better.. now do this: one inside for loop start i by 1 i = 1, remove lenght - 1..
    in if statement remove "|| scores[i] == scores [i+1]" you don't need this.. in if compare scores[i] < socres[i - 1] return false, and return true end of the method.

  11. The Following User Says Thank You to haski For This Useful Post:

    hkfrenchtoast (March 10th, 2014)

  12. #11
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Array Problem

    You must iterate through the entire array to compare the elements. When you call a "return" you are returning a value to the method which called the method. In this instance as haski has mentioned there is no need for all the to return true unless it has iterated through the array and met the conditions.

    The way you have setup your loop is good. When you check the conditions you must meet you can see something. (a > b and a == b) = true, but if you return something here then you do not compare the rest of the values. There is a condition you can setup in the if statement that would justify the return of a boolean inside the loop and return the correct answer.

    Conditions:
    1. (a > b) = true;
    2. (a == b) = true;
    3. (???) = false

    Also make sure to remove the else portion in the if statement. As mentioned previously as soon as a return is called it returns a value to the method that called it and doesn't run the rest of the loop.

  13. The Following User Says Thank You to Ubiquitous For This Useful Post:

    hkfrenchtoast (March 10th, 2014)

  14. #12
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Array Problem

    Quote Originally Posted by hkfrenchtoast View Post
    Sorry to bump this, but I really want to know how to make the code work. :/
    Basically there is a logical problem in the flow of execution. Let me break it down with pseudocode.

    for each element in the array
        if element is less than or equal to the next element
             return true
        else
             return false

    Can you see the problem? It isn't checking that this 'less than or equal to the next element' is true for EVERY element. Only the first one. The return statement bails out the loop and returns either true or false.

    What you need is a temporary variable, something to hold the truthfulness of the statement and give it a chance to check that the entire array is in increasing order.

    temp = false
    for each element in the array
        if element is less than or equal to the next element
             temp = true
        else
             return false
    end loop
    return temp

    Note that if any any element is greater than the next the entire function stops and returns false. There is no need to continue iterating through the array because it can no longer be entirely increasing. Abusing this bit of logic we can further simplify the pseudocode.

    for each element in the array
       if element is greater than the next element
           return false
    end loop
    return true

    This is the most elegant solution (haski mentioned it) because you need to completely prove the truthfulness whilst a single false disproves.

  15. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    hkfrenchtoast (March 10th, 2014)

  16. #13
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Array Problem

    Quote Originally Posted by ChristopherLowe View Post
    Basically there is a logical problem in the flow of execution. Let me break it down with pseudocode.

    for each element in the array
        if element is less than or equal to the next element
             return true
        else
             return false

    Can you see the problem? It isn't checking that this 'less than or equal to the next element' is true for EVERY element. Only the first one. The return statement bails out the loop and returns either true or false.

    What you need is a temporary variable, something to hold the truthfulness of the statement and give it a chance to check that the entire array is in increasing order.

    temp = false
    for each element in the array
        if element is less than or equal to the next element
             temp = true
        else
             return false
    end loop
    return temp

    Note that if any any element is greater than the next the entire function stops and returns false. There is no need to continue iterating through the array because it can no longer be entirely increasing. Abusing this bit of logic we can further simplify the pseudocode.

    for each element in the array
       if element is greater than the next element
           return false
    end loop
    return true

    This is the most elegant solution (haski mentioned it) because you need to completely prove the truthfulness whilst a single false disproves.
    Okay, I did what you said:

    class Test
    {
    	private String temp = "false";
    	void scoresIncreasing(int[] scores) 
    	{
    	  for (int i = 0; i< scores.length-1; i++)
    	  {
    	  if (scores[i] < scores[i+1] || scores[i] == scores [i+1])
    	  {
    	  temp="true";
     
    	  }
    	  else
    	  {
    		  System.out.println(temp);
    	  }
    	  }
     
    	  }
    	}
     
     
     
    public class App {
     
    	public static void main(String[] args) {
    		Test test1 = new Test();
    		int [] scores = {1,3,2};
    		test1.scoresIncreasing(scores);
    	}
     
    }

    Thank you for all the help everyone! I am aware of the other solution, but I wanted to learn how to also do it the hard way, haha.

    EDIT: Okay, I have another problem now...The code doesn't seem to work

    public boolean scoresIncreasing(int[] scores) {
      boolean temp=false;
      for (int i=0; i < scores.length-1; i++)
      {
      if (scores[i]<scores[i+1] || scores[i]==scores[i+1])
      {
      temp=true;
      }
      else
      {
      temp=false;
      }
     
    }
    return temp;
    }

    for int [] scores = {1, 1, 2, 4, 3, 7};
    Last edited by hkfrenchtoast; March 10th, 2014 at 06:23 PM.

  17. #14
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Array Problem

    Your result (temp) keeps getting overwritten with each loop, so what will happen is temp will really only be set by the last comparison (3 versus 7 in this case). You might want to initialize temp with true and assign it as temp=temp&true and temp=temp&false (the latter of course can just be temp=false).

  18. The Following User Says Thank You to jpharte For This Useful Post:

    hkfrenchtoast (March 11th, 2014)

  19. #15
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Array Problem

    Quote Originally Posted by jpharte View Post
    Your result (temp) keeps getting overwritten with each loop, so what will happen is temp will really only be set by the last comparison (3 versus 7 in this case). You might want to initialize temp with true and assign it as temp=temp&true and temp=temp&false (the latter of course can just be temp=false).
    Okay, I understand the reasoning behind it, but I do not understand the &true thing. What is that? I haven't seen that before.

    I only seen temp=true or temp=false

    but not temp=temp&true.

Similar Threads

  1. Replies: 2
    Last Post: February 24th, 2014, 10:48 PM
  2. A 2d array problem
    By IAmHere in forum Object Oriented Programming
    Replies: 10
    Last Post: January 24th, 2012, 03:35 PM
  3. 2D ARRAY PROBLEM
    By av8 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 27th, 2011, 07:20 PM
  4. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM