Results 1 to 7 of 7
  1. #1
    skj
    skj is offline Member
    Join Date
    Oct 2014
    Posts
    20
    Rep Power
    0

    Default why it is not printing the sum of the first row ?

    Java Code:
    public class Lab12 {
    	public static int SumRow(int [][] a){
    		
    		int row,  column, sum=0;
    		
    		 for (row=0;row<2;row++ )
    		 {
    			 sum=0;
    			 for (column=0;column<2;column++)
    			 {
    				 sum=sum+a[row][column];
    			 }
    			 System.out.println("The sum of row "+row+ " is: "+sum);
    			 
    		 }
    		      return sum;
    		 
    	}
    	public static int SumCol(int [][] b){
    		
    		int total=0;
    		
    		  for (int column=0;column<2;column++)
    		  {
    			  total=0;
    			  for (int row=0;row<2;row++)
    			  {
    				  total=total+b[row][column];
    			  }
    			  
    			  System.out.println("The sum of column "+column+" is: "+total);
    		  }
    		return total;
    	}
    	 public static  int MaxInRow(int[][] c)
    	 {
    		 int max=0;
    		 for (int row=0;row<2;row++)
    		 {
    			 max=0;
    			 for (int column=0; column<2;column++)
    			 {
    				 if (max<c[row][column])
    					 
    					 max=c[row][column];
    			 }
    			 
    			 System.out.println("The max of row "+row+" is: "+max);
    		 }
    		 return max;
    	 }
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		
    	}
    my main class is
    Java Code:
    public class Lab12main {
      public static void main (String[] args){
    	  
    	  int s,t,m;
    	  int [][] a = { { 3,4},{2,1} };
    	  Lab12  ob=new Lab12();
    	 s= ob.SumRow(a);
    	 t=ob.SumCol(a);
    	 m= ob.MaxInRow(a);
    	  
    			  
    	  
    	  
    	  
    	  
    	  
    	  
      }
    }
    my output is
    The sum of row 1 is: 3
    The sum of coloumn 0 is: 5
    The sum of coloumn 1 is: 5
    The max of row 0 is: 4
    The max of row 1 is: 2

    it is supposed to print the sum of row o first ?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: why it is not printing the sum of the first row ?

    What happened when you stepped through this with a debugger? What happened when you added print statements? What happened when you walked through this in your head with a piece of paper and a pencil?

    The idea is to figure out where exactly the program's execution differs from your expectations.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    skj
    skj is offline Member
    Join Date
    Oct 2014
    Posts
    20
    Rep Power
    0

    Default Re: why it is not printing the sum of the first row ?

    for row=0 we supposed to get the sum of a[0][0]+a[0][1] which in this case 3+4=7
    it supposed to be for row 0 the sum is 7
    and then for row 1 the sum is 3 which printed
    I don't understand why it is not printing the sum 7

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: why it is not printing the sum of the first row ?

    You might want to use standard naming conventions: variables and method should start with lower-case letters.

    When I run your code I get this output:

    The sum of row 0 is: 7
    The sum of row 1 is: 3
    The sum of column 0 is: 5
    The sum of column 1 is: 5
    The max of row 0 is: 4
    The max of row 1 is: 2
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  5. #5
    skj
    skj is offline Member
    Join Date
    Oct 2014
    Posts
    20
    Rep Power
    0

    Default Re: why it is not printing the sum of the first row ?

    I just changed the beginning of each method to lower case and still I am getting the same output :(

  6. #6
    skj
    skj is offline Member
    Join Date
    Oct 2014
    Posts
    20
    Rep Power
    0

    Default Re: why it is not printing the sum of the first row ?

    ok thanks I just fixed

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: why it is not printing the sum of the first row ?

    Changing the case of the method names was for our sanity, not for fixing any bugs.

    What output are you getting? Is it different than the output I'm getting?

    If so, make sure you're running the version you think you are. How are you compiling this? How are you running it?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. Replies: 0
    Last Post: 12-26-2011, 03:52 PM
  2. Replies: 9
    Last Post: 08-19-2011, 12:59 AM
  3. printing string backwards and printing every other
    By droidus in forum New To Java
    Replies: 22
    Last Post: 03-10-2011, 09:17 AM
  4. Printing
    By zzpprk in forum AWT / Swing
    Replies: 0
    Last Post: 01-20-2010, 11:25 AM
  5. Printing Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:41 PM

Posting Permissions

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