Currently I am stuck on a problem, not sure what I'm over looking. The problem is:
The method named group will find the sum of each group of 5 elements beginning from the start of the array. If the last group does not have five elements it will not be part of the calculation. The sum of each group will be put into a separate array named sum.
The code i have so far is:
my main method i have thCode:public void group(int[] list){ int count = 0; //keeps count of how many numbers added int sum5 = 0; //keeps the sum of the 5 numbers int[] sum = new int[list.length/5]; //size of sum for every 5 number in the list array for (int i = 0; i<list.length; i++){ int j = 0; sum5 += list[i]; count++; if(count == 5){ //when count reaches 5 the first index of the sum array will hold the first sum of 5 numbers sum[j] = sum5; count = 0; //count resets sum5 = 0; //sum5 resets j++; //next index for sum } } for (int i = 0; i<sum.length; i++) System.out.print(sum[i] + " "); //prints out the array sum }
my results come back like thisCode:System.out.println("\n\nmoreDigits array: "); for (int i=0; i<moreDigits.length; i++) System.out.print(moreDigits[i] + " "); System.out.println("\nSum of each group of 5 elements: "); am.group(moreDigits);
does anyone see something that i did wrong?Code:moreDigits array: 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 Sum of each group of 5 elements: 260 0 0 0 0 0 0 0 0 0




Reply With Quote
