Solved

array sum

Posted on 2015-02-12
3
90 Views
Last Modified: 2015-02-12
Hi,

I am workjing on below challenge

http://codingbat.com/prob/p175763

i wrote as below and passed all tests.

public int sum3(int[] nums) {
int num1=nums[0];
int num2=nums[1];
int num3=nums[2];

return num1+num2+num3;
  
}

Open in new window


i wonder how to improve my code. please advise
0
Comment
Question by:gudii9
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 400 total points
ID: 40606565
Not an improvement in this case, but if you wanted it to work regardless of the length, you might use something like

  int sum=0;
  for( int n : nums ){
    sum += n;
  }
  return sum;

Or in the length 3 case, you could do it without the variables
  return nums[0]+nums[1]+nums[2];
0
 
LVL 14

Assisted Solution

by:Geisrud
Geisrud earned 100 total points
ID: 40606607
In support of Ozo's comment, when writing a program (even a simple one such as this) always consider how you could re-use your code and how does it scale to other/larger projects.

Your code is great for only this situation, adding arrays of three integers.  If there were four, then you have to rewrite.  Next week there will be five, and another rewrite.  Maybe there's a thousand integers in the array, or a million!  Do you want to hardcode all of those integers?  No.

With Ozo's simple and elegant solution, you can reuse the same snippet of code for any size array.

Points should go to Ozo, doesn't get better than what we provided.
0
 
LVL 14

Expert Comment

by:Geisrud
ID: 40606641
correction - that should say what HE provided.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now