Solved

array sum

Posted on 2015-02-12
3
87 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
Comment Utility
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
Comment Utility
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
Comment Utility
correction - that should say what HE provided.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Issues installing SSL certificate into Apache Tomcat 3 70
stringclean challenge 26 53
HashMap Vs TreeMap 12 47
Java and GPO 11 45
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.
This video teaches viewers about errors in exception handling.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

8 Experts available now in Live!

Get 1:1 Help Now