d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help Summing A Nxn Array Recursively
Add Reply New Topic New Poll
Member
Posts: 29,197
Joined: Feb 5 2007
Gold: 4,000.18
Jun 14 2012 10:23am
Can't quite get the code working...

been trying for an hour now :s

can anyone help me

This post was edited by IsraeliSoldier on Jun 14 2012 10:23am
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jun 14 2012 11:19am
i would of thought you would have been posting here enough to know to post some code and where you are stuck...
Member
Posts: 29,197
Joined: Feb 5 2007
Gold: 4,000.18
Jun 14 2012 11:21am
Quote (AbDuCt @ Jun 14 2012 12:19pm)
i would of thought you would have been posting here enough to know to post some code and where you are stuck...


I actually editted it out to see how you guys would tackle it xD

Code
public static int ArraySum(int[][] A, int a, int b)
 {
  System.out.println(a + "" + b);
  if(a == A.length-1 & b == A.length-1)
  return A[a][b];

  if(a <= b & b != A.length)
  return A[a][b] + ArraySum(A, a, b+1);

  if(b == A.length-1)
  return ArraySum(A, a+1, 0);

  return 1;
  }

 
   }


I can't find the right statement to sum the second colum, third column etc till the end.

Maybe I'm looking at it from a bad angle?

/e still trying to find a way to sum the rest of the array,.

This post was edited by IsraeliSoldier on Jun 14 2012 11:40am
Member
Posts: 29,197
Joined: Feb 5 2007
Gold: 4,000.18
Jun 14 2012 12:00pm
Found the solution after some hardcore testing xD

System.out.println(a + "" + b);

if(a == A.length-1 & b == A.length-1)
return A[a][b];

if(a <= b & b != A.length || a>b)
return A[a][b] + ArraySum(A, a, b+1);

if(b == A.length)
return ArraySum(A, a+1, 0);

return 0;

if anyone is intrested
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Jun 14 2012 12:07pm
sum of an nxn array is just the sum of an n-1 x n-1 array plus the sum of the remaining n-length row/column
Member
Posts: 29,197
Joined: Feb 5 2007
Gold: 4,000.18
Jun 14 2012 12:20pm
Quote (irimi @ Jun 14 2012 01:07pm)
sum of an nxn array is just the sum of an n-1 x n-1 array plus the sum of the remaining n-length row/column


My code works, Don't know if it's the best one, but it's 10000000x better than the TA's solutions online. Her code is just fucking terrible I cringed and cried when I saw it.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll