Summing arrays
From CompSciWiki
Back to the Program-A-Day homepage
ProblemLet's 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) { } Only one variable is needed at the top of our method. This is the our running sum. This variable will be initialized 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 |