CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2018
    Location
    Java 8
    Posts
    16

    Adding every 5 numbers in an array

    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:

    Code:
     
    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 main method i have th
    Code:
     
    		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);
    my results come back like this

    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
    does anyone see something that i did wrong?

  2. #2
    Join Date
    Jan 2018
    Location
    Java 8
    Posts
    16

    Re: Adding every 5 numbers in an array

    i found the error!!!

    i needed to initialize the variable j outside of the for loop

  3. #3
    Join Date
    Feb 2017
    Posts
    677

    Re: Adding every 5 numbers in an array

    Quote Originally Posted by rcheeks23 View Post
    i found the error!!!
    It's always a good idea to seek solutions with low complexity. For example you could do,
    Code:
        final int D = 5;
        int[] sum = new int[list.length/D]; //size of sum for every 5 number in the list array
    		
        for (int i = 0; i<sum.length*D; i++) {
            sum[i/D] += list[i];
        }
    Now there are far fewer things that can go wrong. There may still be errors of course but the simpler the code the easier they are to spot.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured