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.

Thread: Array Problem

1. 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. Re: Array Problem

What do you get when you try this:

scoresIncreasing({3, 2})

3. 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);
}

}```

Fixed then?

5. Re: Array Problem

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?

6. 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. 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. 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. Re: Array Problem

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

10. 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. 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. Re: Array Problem

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.

```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. Re: Array Problem

Originally Posted by ChristopherLowe
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};

17. 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. Re: Array Problem

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.