Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum of elements in an array

I'm working on a simple assignment for a summer java course and was just hoping you guys could take a look at my code and see if the way I did it is the best way. The purpose is to create a simple int array with at least 25 elements and use a loop to traverse it and add up all the elements. I had some issues but looks like I got it to work. After I work it out I did a little research and saw some similar stuff where people were using a For Each loop (enhanced loop). Would that be a better option? I'm kinda confused on the best ways to use that opposed to a regular for loop.

Anyway, any comments or criticism in helping me be a better programmer!

public class Traversals {

    public static void main(String[] args) {

        int absenceTotal = 0;
        // initialize array with 30 days of absences.
        int absencesArr[] = { 1, 3, 0, 9, 8, 23, 1, 
                11, 23, 5, 6, 7, 10, 1, 5,
                14, 2, 4, 0, 0, 1, 3, 2, 1, 
                1, 0, 0, 1, 3, 7, 2 };

        for (int i = 0; i < absencesArr.length; i++) {
            absencesArr[i] += absenceTotal;
            absenceTotal = absencesArr[i];
        }
        System.out.println("There were " + absenceTotal + " absences that day.");
    }
}
like image 692
NoobCoderChick Avatar asked Jul 03 '15 01:07

NoobCoderChick


3 Answers

Don't modify the array. I would prefer the for-each loop. And you should consider that there may be a very large number of students, so I would probably use a long for sum. And formatted output. Putting that together into something like

long sum = 0;
for(int i : absencesArr) {       
    sum += i;
}   
// System.out.println("There were " + sum + " absences that day.");   
System.out.printf("There were %d absences that day.%n", sum);
like image 98
Elliott Frisch Avatar answered Oct 07 '22 02:10

Elliott Frisch


public class Traversals {

    public static void main(String[] args) {

        int absenceTotal = 0;
        // initialize array with 30 days of absences.
        int absencesArr[] = { 1, 3, 0, 9, 8, 23, 1, 
                11, 23, 5, 6, 7, 10, 1, 5,
                14, 2, 4, 0, 0, 1, 3, 2, 1, 
                1, 0, 0, 1, 3, 7, 2 };

        for (int i = 0; i < absencesArr.length; i++) {
            // remove this
            //absencesArr[i] += absenceTotal;
            absenceTotal += absencesArr[i]; //add this
        }
        System.out.println("There were " + absenceTotal + " absences that day.");
    }
}
like image 29
Josua Marcel C Avatar answered Oct 07 '22 01:10

Josua Marcel C


In Java 8 you can use stream api:

public class Traversals {
    public static void main(String[] args) {

        int absenceTotal = 0;
        // initialize array with 30 days of absences.

        int absencesArr[] = { 1, 3, 0, 9, 8, 23, 1, 
                11, 23, 5, 6, 7, 10, 1, 5,
                14, 2, 4, 0, 0, 1, 3, 2, 1, 
                1, 0, 0, 1, 3, 7, 2 };

        absenceTotal = IntStream.of(array).sum();

        System.out.println("There were " + absenceTotal + " absences that day.");
    }
}
like image 40
Jan Siekierski Avatar answered Oct 07 '22 00:10

Jan Siekierski