Welcome to Dream.In.Code
Become a Java Expert!

Join 150,363 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,811 people online right now. Registration is fast and FREE... Join Now!




Array

 
Reply to this topicStart new topic

Array, Given an array of ints, return the sum of the first 2 elements in the

ragaf
4 Feb, 2008 - 03:30 PM
Post #1

New D.I.C Head
*

Joined: 22 Nov, 2007
Posts: 5


My Contributions
Given an array of ints, return the sum of the first 2 elements in the array. If the array length is less than 2, just sum up the elements that exist, returning 0 if the array is length 0.

Example: sum2({1, 2, 3}) → 3
sum2({1, 1}) → 2
sum2({1, 1, 1, 1}) → 2

public int sum2(int[] nums) {


return nums[0] + nums[nums.length-1]
int value = 0;
for (int x = 0; x<nums.length-1; x++]{
value = value + nums[x]
}
return value;
}

}


i need help
plz help me
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Array
4 Feb, 2008 - 03:33 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
I really don't see a problem here, just return nums[0] + nums[1];

And maybe you should check if the array contains at least two elements before that.

Is that what you were looking for?
User is offlineProfile CardPM
+Quote Post

ragaf
RE: Array
4 Feb, 2008 - 03:36 PM
Post #3

New D.I.C Head
*

Joined: 22 Nov, 2007
Posts: 5


My Contributions
QUOTE(PennyBoki @ 4 Feb, 2008 - 04:33 PM) *

I really don't see a problem here, just return nums[0] + nums[1];

And maybe you should check if the array contains at least two elements before that.

Is that what you were looking for?

i tried that but still having trouble
can you plz provide me another way
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Array
4 Feb, 2008 - 04:12 PM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Well the since you tried that, please show us the code so that we can test it.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:50PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month