Solved

array sum

Posted on 2015-02-12
3
95 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 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

734 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