tags:

views:

157

answers:

3

Hi, I was wondering if I could have some pseudo code for working out the following

i need to loop through a 2d array(the method i am working on takes an int). It starts at the position of the value passed in and then goes down until it hits the same value on the left hand side. As its doing this every int in the 2d array is added to a local variable. Now we are at position (x,x) i guess? Then from here i need to loop across to the right adding all variables to the same previous local var and then return that number

The 2d array for illustration purposed looks something like this for example

1 2 3 4 
2 3 4 5 
1 2 3 4 
3 2 1 4

So if i were to pass in 2 we would start at the number 3 top line i guess, we would loop down until position 3,3 ( 3 + 4 + 3) and then loop to the right until the end (+ 4)

those numbers would be added and returned.

I hope pseudo code is possible and I haven't just given it already myself lol (thus proving i cant actually code it lol :D )

if not any examples you could provide me with that my help me out :D

thanks guys

A: 

Not sure what you are trying to achieve, I assume this is just an assignment. If you are looping to the right, shouldn't 1 be included if not the 2 as well?

i.e. then loop to the right until the end (+1 + 4)

Peter Lawrey
He loops downwards, not to the right.
Tamás Mezei
yes this is an assignmnet, but this is kind of a side issue that i need to solve in order to achieve the assignment, i need to determine if each node in a graph has even edges. So if i am checking node 3. Node 3 has edges with (3,0)(3,1)(3,2)(3,3(not sure about this one))(4,3)dont no if that clears it up at all?
simon
A: 

The answer depends on whether you store the columns or the rows of matrix. Assuming you have a n * n size matrix and you store the rows, so

A = [[1,2,3,4], [2,3,4,5], [1,2,3,4], [3,2,1,4]]

and the starting point is i, you should go from the array no. m = i div n (the integer part of the division, rounding down), and inside the array, the staring element should be the no. p = i mod n (the modulus). And from that point, you can select every array from m to n, and in every array, the pth element, until the most recent element is the same as your original.

In Java-like code:

int n = 4;
int[][] A = new int[n][n];
... // initialize A

int sumValues(int i) { 
    int original = A[i/n][i%n];   
    int sum = original;
    int p = i % n;

    for (m = i/n + 1, m<n, ++m) {
        if (A[m][p] != orginal) sum += A[m][p];
        else break;
    }

    return sum;
}

If you are storing the columns, so

A = [[1,2,1,3], [2,3,2,2], [3,4,3,1], [4,5,4,4]]

then m and p are inversed, meaning that from A you should select array no. m = i mod n and inside that array, element no. p = i div n. After this, you stay in your selected array, and just increase p until A[m][p] equals to the originally selected value.

In Java-like code:

int n = 4;
int[][] A = new int[n][n];
... // initialize A

int sumValues(int i) { 
    int original = A[i%n][i/n];   
    int sum = original;
    int p = i / n;

    for (p = i/n +1, p<n, ++p) {
        if (A[m][p] != orginal) sum += A[m][p];
        else break;
    }

    return sum;
}

But correct me, if I'm wrong :)

Tamás Mezei
A: 

I think this pseudocode should do what you're looking for:

array[][] := ...
position := ...
sum := 0

//add the contents of the outer array
for i := 0 to array.length do
 sum := sum + array[i][position]

 //if we're at (pos, pos) then start looping over the inner array
 if i = position then

  //note we start at pos + 1 so as not to count array[i][position] twice
  for j := position + 1 to array[j].length do
   sum := sum + array[i][j]
  end

     break from loop  

 end
end
Jared Russell
thank you so much
simon
No problem. You'll probably want to add some extra error handling code when you implement it to ensure that the position the method takes is within the bounds of the array (e.g. less that array.length).
Jared Russell