Array Problem

• March 8th, 2014, 11:19 PM
hkfrenchtoast
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

Code :

```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...
• March 9th, 2014, 01:36 AM
ChristopherLowe
Re: Array Problem
What do you get when you try this:

scoresIncreasing({3, 2})
• March 9th, 2014, 03:43 AM
hkfrenchtoast
Re: Array Problem
--- Update ---

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

Code :

```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); }   }```
• March 9th, 2014, 04:48 AM
GregBrannon
Re: Array Problem
Fixed then?
• March 9th, 2014, 06:07 AM
ChristopherLowe
Re: Array Problem
Quote:

Originally Posted by hkfrenchtoast
--- 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?
• March 9th, 2014, 01:04 PM
hkfrenchtoast
Re: Array Problem
I ran the code again with {1,3,2} and got

Output:
Quote:

true
false
I am not sure what the problem is. I forgot to mention that the assignment is from CodingBat AP-1 question.
• March 9th, 2014, 01:44 PM
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.
• March 9th, 2014, 02:13 PM
hkfrenchtoast
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.
• March 10th, 2014, 12:35 AM
hkfrenchtoast
Re: Array Problem
Sorry to bump this, but I really want to know how to make the code work. :/
• March 10th, 2014, 02:57 AM
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.
• March 10th, 2014, 05:42 AM
Ubiquitous
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.
• March 10th, 2014, 06:44 AM
ChristopherLowe
Re: Array Problem
Quote:

Originally Posted by hkfrenchtoast
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.

Code :

```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.

Code :

```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.

Code :

```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.
• March 10th, 2014, 06:17 PM
hkfrenchtoast
Re: Array Problem
Quote:

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

Code :

```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.

Code :

```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.

Code :

```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:

Code :

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

Code java:

```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};
• March 11th, 2014, 01:08 AM
jpharte
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).
• March 11th, 2014, 09:10 PM
hkfrenchtoast
Re: Array Problem
Quote:

Originally Posted by jpharte
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.