Difference between revisions of "Summing arrays"
From CompSciWiki
m |
m (Removed <pre> tags from SolutionCode.) |
||
Line 35: | Line 35: | ||
|SolutionCode= | |SolutionCode= | ||
− | |||
public static int sumArray(int[] myArray) | public static int sumArray(int[] myArray) | ||
{ | { | ||
Line 49: | Line 48: | ||
return sum; | return sum; | ||
} | } | ||
− | |||
− | |||
}} | }} |
Revision as of 11:58, 9 April 2010
Back to the Program-A-Day homepage
ProblemLets start off with a small problem this week. |
More with Arrays
| |
---|---|---|
SolutionFirst we are going to need our method header. We know that it takes an array of integers and returns an integer. public static int sumArray(int[] myArray) { } We'll only need one variable at the top of our method. This is the our running sum. We will initialize this variable to 0. public static int sumArray(int[] myArray) { int sum; sum = 0; return sum; } Now we need to iterate over all of our elements and add them up. | ||
Code |