9 Replies - 700 Views - Last Post: 05 April 2013 - 09:42 PM Rate Topic: -----

#1 sayham  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 73
  • Joined: 21-March 13

Summing both rows and print the greatest value...

Posted 05 April 2013 - 11:47 AM

Hi friends. I am in trouble with some programs including this. Any help will be appreciated.

/*Complete the method, named bigSum, in the class named ArrayOps.java. The only parameter to this method is a 
two-dimensional array of integers with only 2 rows. This method should sum each row of integers separately, 
and then decide which of the two sums is larger. This larger sum is the return value for this method. 
For example, suppose the array contains the following data: 
[ [ 25, 16, 14 ]
  [ 13, 27, 22 ]  ]
The sum of the first row is (25 + 16 + 14) = 55, while the sum of the second row is (13 + 27 + 22) = 62. Comparing, 
we see that the sum of the second row is the larger sum; hence, the method returns the integer 62. 
You might think of this as comparing the test grades of two students. 
Complete the following code:*/
   import java.util.Scanner;  
   public class ArrayOps5
   {
   /**
      This method sums up both rows of a two-dimensional array
      (the only parameter to the method) and returns the greater sum.
      @param theArray, a 2-D array of integers
      @return, the greater row sum
   */
      public static int bigSum(int[][] theArray)
      {
      
         int sum1 = 0;
         int sum2 = 0;
      
         int sum = 0;
         for (int i = 0; i < theArray.length; i++)// loop though theArray, summing first row 
         {
         
            for(int j = 0; j < theArray[0].length; j++)
            {
               sum1 = sum1 + theArray[i][j];
            }
            return sum1;
         }
         for (int i = 0; i < theArray.length; i++)// loop though theArray, summing second row
         {
         
            for(int j = 0; j < theArray[0].length; j++)
            {
               sum2 = sum2 + theArray[i][j];
            }
            return sum2;
         }
         if (sum1>sum2)      // your work here
         {
            return sum1;}
         else 
         {
            return sum2;}

      
      }
      public static void main(String[] args)
      {
         Scanner in = new Scanner(System.in);
         int[][] theArray = {{ 25, 16, 14 },
               				{ 13, 27, 22}};
         System.out.print(bigSum(theArray));
      }
   }//No.48



Is This A Good Question/Topic? 1
  • +

Replies To: Summing both rows and print the greatest value...

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,693
  • Joined: 13-March 10

Re: Summing both rows and print the greatest value...

Posted 05 April 2013 - 11:56 AM

You have return statements in wrong places. The IDE should warn you that other code is not reacheable.
Was This Post Helpful? 0
  • +
  • -

#3 sayham  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 73
  • Joined: 21-March 13

Re: Summing both rows and print the greatest value...

Posted 05 April 2013 - 08:05 PM

Thanks Darek, so, what should I do now?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10821
  • View blog
  • Posts: 40,340
  • Joined: 27-December 08

Re: Summing both rows and print the greatest value...

Posted 05 April 2013 - 08:45 PM

You only return one value at the end. Don't use return statements before your comparisons. Look at your logic and read your code.
Was This Post Helpful? 0
  • +
  • -

#5 sayham  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 73
  • Joined: 21-March 13

Re: Summing both rows and print the greatest value...

Posted 05 April 2013 - 08:53 PM

Thanks Mac, Please help me, what should I do now.
for (int i = 0; i < theArray.length; i++)// loop though theArray, summing first row 
         {
         
            for(int j = 0; j < theArray[0].length; j++)
            {
               sum1 = sum1 + theArray[i][j];
            }
         }
         for (int i = 0; i < theArray.length; i++)// loop though theArray, summing second row
         {
         
            for(int j = 0; j < theArray[0].length; j++)
            {
               sum2 = sum2 + theArray[i][j];
            }
         }
         if (sum1>sum2)      // your work here
         {
            return sum1;
         }
         else 
         {
            return sum2;
         }

Was This Post Helpful? 1
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10821
  • View blog
  • Posts: 40,340
  • Joined: 27-December 08

Re: Summing both rows and print the greatest value...

Posted 05 April 2013 - 08:55 PM

What specific problems or errors are you encountering? Are you actually trying to debug your code? It feels like you're debugging by proxy here.
Was This Post Helpful? 0
  • +
  • -

#7 sayham  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 73
  • Joined: 21-March 13

Re: Summing both rows and print the greatest value...

Posted 05 April 2013 - 09:11 PM

Sorry Mac, I did debug but forgot to tell the result. While debugging, I found sum1=117, sum2=117 and the program returning 117. Actually, it counting everything twice.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10821
  • View blog
  • Posts: 40,340
  • Joined: 27-December 08

Re: Summing both rows and print the greatest value...

Posted 05 April 2013 - 09:27 PM

If you want to compare rows, you only need a single for loop.
for(int j = 0; j < theArray[0].length; j++){
   //add up theArray[0][j] for sum1
   //add up theArray[1][j] for sum2
}


Was This Post Helpful? 0
  • +
  • -

#9 sayham  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 73
  • Joined: 21-March 13

Re: Summing both rows and print the greatest value...

Posted 05 April 2013 - 09:40 PM

Thanks Mac, I appreciate your help. My code looks good. Thank you very much.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10821
  • View blog
  • Posts: 40,340
  • Joined: 27-December 08

Re: Summing both rows and print the greatest value...

Posted 05 April 2013 - 09:42 PM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1