Java C++, dealing with matrix sum

Programming
Tutor: None Selected Time limit: 1 Day

int i,j;

      for(i=0;i<4;i++)

      {

         for(j=0;j<4;j++)

            System.out.print(matrix [i] [j] + " ");

         System.out.println();

      }

      int sum = 0;

      for (int row=0; row < matrix.length; row++)

    {

        for (int col=0; col < matrix[row].length; col++)

        {

            sum = sum + matrix[row][0];

        }

I keep getting the wrong answers when the rows are added. 

The sum for row 0 is 4 (should be 10)

Oct 3rd, 2015

Thank you for the opportunity to help you with your question!

The answer is : 

int i,j;

      for(i=0;i<4;i++)

      {

         for(j=0;j<4;j++)

            System.out.print(matrix [i] [j] + " ");

         System.out.println(" ");

      }

      int sum = 0;

      for (int row=0; row < 4; row++)

    {

        for (int col=0; col < 4; col++)

        {

            sum = sum + matrix[row][col];     // or you can use  sum += matrix[row][col];

        }

      }

System.out.println(sum);


ask if you don't understand 

Good luck 

Please let me know if you need any clarification. I'm always happy to answer your questions.
Sep 21st, 2015

Did you know? You can earn $20 for every friend you invite to Studypool!
Click here to
Refer a Friend
...
Oct 3rd, 2015
...
Oct 3rd, 2015
Feb 19th, 2017
check_circle
Mark as Final Answer
check_circle
Unmark as Final Answer
check_circle
Final Answer

Secure Information

Content will be erased after question is completed.

check_circle
Final Answer