Solved

scoresAverage challenge

Posted on 2016-08-26
8
131 Views
Last Modified: 2016-08-29
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;
  

}

}

Open in new window





I am not passing all tests with below error

Compile problems:


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


How to improve my design, approach, code? please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 9

Accepted Solution

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

Expert Comment

by:d-glitch
ID: 41772918
Where is your pseudo code??
0
 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 41772937
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;

}

  

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:d-glitch
ID: 41772943
Not appropriate to post complete solutions to homework-type problems before the Asker has even responded to your hint.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41773397
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;
  }
}

}

  

Open in new window



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 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 41773420
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 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 41773422
>>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

by:gudii9
ID: 41773429
>>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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jboss 7.1 start up error 1 85
How to share My.Settings in multiple application instances ? 7 79
Netbeans and org.apache.commons.lang3 issue 3 55
MCAT Verbal Reasoning 2 22
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question