Welcome to the iTechForums.
If this is your first visit, be sure to check out the
FAQ by clicking the link above.
You may have to
register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
To register now click here.
|
Latest Threads
Advertisements
Forum Statistics
Threads:
Posts: 3
Members:
Number of Users Online:
Welcome to our newest member, |
|
 |

05-15-2008, 03:50 PM
|
|
Junior Member
|
|
Join Date: May 2008
Posts: 2
|
|
Write a Java Method?
Write a Java method call SumArray which take in an integer array arr and sum up all the values in the array by either a for loop or a while loop and return the sum.
|

05-15-2008, 08:02 PM
|
|
Junior Member
|
|
Join Date: May 2008
Posts: 1
|
|
the parameter for the method should be of type int [], and the return value should be an int.
Declare an int variable that will take on the sum.
The for loop will loop according to the condition i < arr.length, in which i is a variable you declare(to be 0) in the for declaration. You then will increase i by one at the end of each iteration of the loop.
In the loop, you will add the sum variable you declared to itself and the current value of arr[i] - that is:
sum = sum + arr[i] ;
after the loop, you need to return sum.
|

05-15-2008, 08:54 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 37
|
|
private int SumArray(int[] arr)
{
int total = 0;
for(int i = 0; i < arr.lenght; i++)
{
total += arr[i];
}
return total;
}
this is C# code.... but you should be able to figure it out...
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
All times are GMT. The time now is 05:32 AM.