Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

array sum

Posted on 2015-02-12
3
Medium Priority
?
111 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
[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
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 1600 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 400 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

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!

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article will show, step by step, how to integrate R code into a R Sweave document
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

636 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