Solved

# scoresAverage challenge

Posted on 2016-08-26
74 Views
Hi,

I am working on below challenge

http://codingbat.com/prob/p123837
I wrote my code as below
``````public int scoresAverage(int[] scores) {
int result=0;
int len=scores.length;
int lenHalf=len/2;
int sumEven=0;
int sumOdd=0;
for(int i=0;i<lenHalf;i++){
sumEven=(scores[i]+scores[i+1])/2;
//result=sumeEven;
//return result;
}

for(int i=lenHalf;i<len;i++){
sumOdd=(scores[i]+scores[i+1])/2;
// result=sumeOdd;
//return result;
}
if(sumOdd>sumEven){
return sumOdd;
}
else{
return sumEven;
}

}
return result;

}

}
``````

I am not passing all tests with below error

Compile problems:

Error:      }
^
Syntax error on token "}", { expected after this token

0
Question by:gudii9
• 4
• 2
• 2

LVL 8

Accepted Solution

Subrat (C++ windows/Linux) earned 500 total points
move last return statement two line up and delete last two }} Nd check
0

LVL 27

Expert Comment

0

LVL 8

Expert Comment

``````public int scoresAverage(int[] scores) {
int result=0;
int len=scores.length;
int lenHalf=len/2;
int sumEven=0;
int sumOdd=0;
for(int i=0;i<lenHalf;i++){
sumEven=(scores[i]+scores[i+1])/2;
//result=sumeEven;
//return result;
}

for(int i=lenHalf;i<len;i++){
sumOdd=(scores[i]+scores[i+1])/2;
// result=sumeOdd;
//return result;
}
if(sumOdd>sumEven){
return sumOdd;
}
else{
return sumEven;
}

return result;

}

``````
0

LVL 27

Expert Comment

Not appropriate to post complete solutions to homework-type problems before the Asker has even responded to your hint.
0

LVL 7

Author Comment

``````public int scoresAverage(int[] scores) {
int result=0;
int len=scores.length;
int lenHalf=len/2;
int sumEven=0;
int sumOdd=0;
if(len>2){
for(int i=0;i<lenHalf-1;i++){
sumEven=(scores[i]+scores[i+1])/2;
//result=sumeEven;
//return result;
}

for(int i=lenHalf;i<len-1;i++){
sumOdd=(scores[i]+scores[i+1])/2;
// result=sumeOdd;
//return result;
}
if(sumOdd>sumEven){
result=sumOdd;
return result;
}
else{
result=sumEven;
return result;
}
}
else{
if(scores[0]>scores[1]){
result=scores[0];
return result;
}
else{
result=scores[1];
return result;
}
}

}

``````

above passes all tests.
Any improvement, refinement, refactoring the code?

Psuedo Code:
1. loop through array
2. for arrays more than 2 elements find the average of left part and right part and return whichever greater
3. for array of 3 elements return the max out f both elements.
0

LVL 8

Expert Comment

Keep only one return statement at end. it is bad practice to keep multiple return path from a function. Assign the result as per the requirement and finally return.
0

LVL 8

Expert Comment

>>3. for array of 3 elements return the max out f both >>elements.

test case input 10, 20, 10
is the op that you r getting correct? what is expected?
0

LVL 7

Author Comment

>>3. for array of 3 elements return the max out f both >>elements.

oops, i mean

>>3. for array of 2 elements return the max out f both >>elements.
0

## Featured Post

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.