Milks
Milks

Reputation: 47

Getting a loop to add all the columns of an array

I'm trying to take a hardcoded array and print out all the sums of each column. Everytime I try it just prints the first column sum over and over before ending. I do not know what else to add to keep the loop going throughout the entire array.

Heres the code:

public class ClimateChange {
    public static void main(String[] args) {
        final int ROWS = 9;
        final int COLUMNS = 12;

        int[][] displaced = {
                {106, 107, 111, 133, 221, 767, 866, 1001, 172, 307, 392, 395},
                {20, 73, 26, 82, 502, 615, 209, 947, 116, 214, 278, 445},
                {163, 203, 276, 308, 172, 246, 354, 118, 123, 310, 146, 152},
                {121, 260, 234, 108, 149, 202, 216, 58, 567, 229, 628, 765},
                {1203, 1274, 1226, 1882, 1072, 1007, 1192, 1395, 123, 310, 146, 152},
                {116, 324, 438, 714, 167, 521, 209, 904, 76, 29, 31, 99},
                {76, 29, 31, 99, 187, 201, 278, 306, 183, 122, 99, 246},
                {109, 104, 121, 13, 121, 69, 246, 100, 123, 161, 69, 246},
                {402, 415, 209, 547, 106, 234, 178, 145, 103, 121, 39, 246}};

        String[] animals = {
                "Cheetah",
                "Tigers",
                "Asian elephant",
                "Vaquita porpoise",
                "Mountain gorilla",
                "Red tuna",
                "Orangutan",
                "Black Rhinos",
                "Dolphins"};

        System.out.println("              Temp        0C      1C      3C      " +
                "5C      7C      9C     28C     32C     36C     38C     42C     45C");

        System.out.println();

        for (int i = 0; i < ROWS; i++) {
            System.out.printf("%20s", animals[i]);
            for (int j = 0; j < COLUMNS; j++) {
                System.out.printf("%8d", displaced[i][j]);
            }
            System.out.println(); // A new line begins at the end of the row.
        }

        int row = 0;
        int col;
        int colSum = 0;

        col = 0;
        for (row = 0; row < displaced.length; row++)
            colSum = colSum + displaced[row][col];
        for (col = 0; col < displaced.length; col++) {
            System.out.println("Animals:  " + colSum);
        }
        System.out.println("               Save our animals, climate change is real!");
    }
}

Everytime I try to use this it just prints the first column sum over and over underneath the printed array.

Upvotes: 3

Views: 117

Answers (1)

Aalexander
Aalexander

Reputation: 5004

Solution

You were close, you have to build the sum inside the innerLoop and then print out the sum in the outer loop. Also note that you have to reset it to zero again in the outerloop

for (row = 0; row < displaced.length; row++) {
    colSum = 0;

    for (col = 0; col < displaced.length; col++) {
        colSum = colSum + displaced[row][col];

    }
    System.out.println("Animals:  " + colSum);
    colSum = 0;
}

Code

public class Q {

    public static void main(String[] args) {
        final int ROWS = 9;
        final int COLUMNS = 12;

        int[][] displaced = { { 106, 107, 111, 133, 221, 767, 866, 1001, 172, 307, 392, 395 },
                { 20, 73, 26, 82, 502, 615, 209, 947, 116, 214, 278, 445 },
                { 163, 203, 276, 308, 172, 246, 354, 118, 123, 310, 146, 152 },
                { 121, 260, 234, 108, 149, 202, 216, 58, 567, 229, 628, 765 },
                { 1203, 1274, 1226, 1882, 1072, 1007, 1192, 1395, 123, 310, 146, 152 },
                { 116, 324, 438, 714, 167, 521, 209, 904, 76, 29, 31, 99 },
                { 76, 29, 31, 99, 187, 201, 278, 306, 183, 122, 99, 246 },
                { 109, 104, 121, 13, 121, 69, 246, 100, 123, 161, 69, 246 },
                { 402, 415, 209, 547, 106, 234, 178, 145, 103, 121, 39, 246 } };

        String[] animals = { "Cheetah", "Tigers", "Asian elephant", "Vaquita porpoise", "Mountain gorilla", "Red tuna",
                "Orangutan", "Black Rhinos", "Dolphins" };

        System.out.println(
                "              Temp        0C      1C      3C      5C      7C       9C    2  32C     36C      38C     42C     45C");

        System.out.println();

        for (int i = 0; i < ROWS; i++) {

            System.out.printf("%20s", animals[i]);
            for (int j = 0; j < COLUMNS; j++) {
                System.out.printf("%8d", displaced[i][j]);
            }

            System.out.println(); // A new line begins at the end of the row.
        }

        int row = 0;
        int col = 0;
        ;
        int colSum = 0;

        for (row = 0; row < displaced.length; row++) {

            for (col = 0; col < displaced.length; col++) {
                colSum += displaced[row][col];
            }

            System.out.println("Animals:  " + colSum);
            colSum = 0;
        }
        System.out.println("               Save our animals, climate change is real!");

    }
}

Outputs

              Temp        0C      1C      3C      5C      7C       9C    2  32C     36C      38C     42C     45C

             Cheetah     106     107     111     133     221     767     866    1001     172     307     392     395
              Tigers      20      73      26      82     502     615     209     947     116     214     278     445
      Asian elephant     163     203     276     308     172     246     354     118     123     310     146     152
    Vaquita porpoise     121     260     234     108     149     202     216      58     567     229     628     765
    Mountain gorilla    1203    1274    1226    1882    1072    1007    1192    1395     123     310     146     152
            Red tuna     116     324     438     714     167     521     209     904      76      29      31      99
           Orangutan      76      29      31      99     187     201     278     306     183     122      99     246
        Black Rhinos     109     104     121      13     121      69     246     100     123     161      69     246
            Dolphins     402     415     209     547     106     234     178     145     103     121      39     246
Animals:  3484
Animals:  2590
Animals:  1963
Animals:  1915
Animals:  10374
Animals:  3469
Animals:  1390
Animals:  1006
Animals:  2339
               Save our animals, climate change is real!

Upvotes: 1

Related Questions