开发者

Pseudo code needed if possible?

开发者 https://www.devze.com 2022-12-11 17:20 出处:网络
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 passe

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 a开发者_运维技巧dded 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


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


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)


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号