Thread: Help - Bidimensional array

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    105

    Help - Bidimensional array

    Hello everyone! I'm trying to solve an exercise.
    I have a bidimensional array (N*M) where each element represents how many people live in a square. I have to print which element has more people living in the squares that surround it.

    Sorry if you don't understand what I wrote. I will put some pictures, maybe you would understand they better

    Help - Bidimensional array-sumamanzanas-png

    In the first bidimensional array, the user write how much people live in each square.
    In the second bidimensional array, the program "walk" through the bidimensional array and, in this case, it prints that the element which has the higher population in the squares that surrounds the one he lives in is [2][1].

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int CompleteBidimArray(int N, int M, int matriz[N][M])
    {
        int i, j;
        for(i=0; i<N; i++)
        {
            for(j=0; j<M; j++)
            {
                printf("\n[%d][%d]: ", i, j);
                scanf("%d", &matriz[i][j]);
            }
        }
    }
    
    int PrintOriginalMatrix(int N, int M, int matriz[N][M])
    {
        int i, j;
        for(i=0; i<N; i++)
        {
            for (j=0; j<M; j++)
            {
                printf("%4d", matriz[i][j]);
            }
            printf("\n");
        }
    }
    
    int HighLeftSide (int N, int M, int matriz[N][M])
    {
        //N=0, M=0
        int sum=0;
        printf("\n\nHIGH LEFT SIDE\n");
        sum = (matriz[1][0]+matriz[1][1]+matriz[0][1]);
        return sum;
    }
    
    int HighRightSide (int N, int M, int matriz[N][M])
    {
        //N=0, M=M-1
        int sum=0;
        printf("\n\nHIGH RIGHT SIDE\n");
        sum = (matriz[0][M-2]+matriz[1][M-2]+matriz[1][M-1]);
        return sum;
    }
    
    int LowerLeftSide (int N, int M, int matriz[N][M])
    {
        //N=N-1, M=0
        int sum=0;
        printf("\n\nLOWER LEFT SIDE\n");
        sum = (matriz[N-2][0]+matriz[N-2][1]+matriz[N-1][1]);
        return sum;
    }
    
    int LowerRightSide (int N, int M, int matriz[N][M])
    {
        //N=N-1, M=M-1
        int sum=0;
        printf("\n\nLOWER RIGHT SIDE\n");
        sum = (matriz[N-1][M-2]+matriz[N-2][M-2]+matriz[N-2][M-1]);
        return sum;
    }
    
    int MiddleHigh (int N, int M, int matriz[N][M]) //OK
    {
        int i=0, j, sum1=0, sum2=0, sumtotal;
        printf("\n\nMIDDLE HIGH\n");
        for (j=1; j<N; j++)
        {
            sum1 = matriz[0][j-1] + matriz[0][j] + matriz [0][j+1];
            sum2 = matriz[1][j-1] + matriz[1][j] + matriz [1][j+1];
            sumtotal = sum1 + sum2 - matriz[0][j];
            printf("\nElement: [%d][%d]\nSquare sum: %d", i, j, sumtotal);
        }
    }
    
    int MiddleLow (int N, int M, int matriz[N][M]) //OK
    {
        int i=N-1, j, sum1=0, sum2=0, sumtotal;
        printf("\n\nMIDDLE LOW\n");
        for (j=1; j<N; j++)
        {
            sum1 = matriz[i][j-1] + matriz[i][j] + matriz[i][j+1];
            sum2 = matriz[i-1][j-1] + matriz[i-1][j] + matriz[i-1][j+1];
            sumtotal = sum1 + sum2 - matriz[i][j];
            printf("\nElement: [%d][%d]\nSquare sum: %d", i, j, sumatotal);
        }
    }
    
    int RightSide (int N, int M, int matriz[N][M])
    {
        int i, j=N, sum1=0, sum2=0, sumtotal;
        printf("\n\nRIGHT SIDE\n");
        for (i=1; i<N-1; i++)
        {
            sum1 = matriz[i-1][j] + matriz[i][j] + matriz[i+1][j];
            sum2 = matriz[i-1][j-1] + matriz[i][j-1] + matriz[i+1][j-1];
            sumtotal = sum1 + sum2 - matriz[i][j];
            printf("\nElement: [%d][%d]\nSquare sum: %d", i, j, sumtotal);
        }
    }
    
    int LeftSide (int N, int M, int matriz[N][M])
    {
        int i, j=0, sum1=0, sum2=0, sumtotal;
        printf("\n\nLEFT SIDE\n");
        for (i=1; i<N-1; i++)
        {
            sum1 = matriz[i-1][j] + matriz[i][j] + matriz[i+1][j];
            sum2 = matriz[i-1][j+1] + matriz[i][j+1] + matriz[i+1][j+1];
            sumtotal = sum1 + sum2 - matriz[i][j];
            printf("\nElement: [%d][%d]\nSquare sum: %d", i, j, sumtotal);
        }
    }
    
    int Middle (int N, int M, int matriz[N][M])
    {
        int i, j, sum1=0, sum2=0, sum3=0, sumtotal;
        printf("\n\nMIDDLE\n");
        for (i=1; i<N-1; i++)
        {
            for (j=1; j<N; j++)
            {
                sum1 = matriz[i-1][j-1] + matriz[i-1][j] + matriz[i-1][j+1];
                sum2 = matriz[i][j-1] + matriz[i][j] + matriz[i][j+1];
                sum3 = matriz[i+1][j-1] + matriz[i+1][j] + matriz[i+1][j+1];
                sumtotal = sum1 + sum2 + sum3 - matriz[i][j];
                printf("\nElement: [%d][%d]\nSquare sum: %d", i, j, sumtotal);
            }
        }
    
    }
    
    
    int main()
    {
        int N, M;
        printf("\nNumber of rows: ");
        scanf("%d", &N);
        printf("\nNumber of columns: ");
        scanf("%d", &M);
    
        int matriz[N][M];
        CompleteBidimArray(N, M, matriz);
        PrintOriginalMatrix(N, M, matriz);
    
    
        int suma;
        int MaxSuma = HighLeftSide(N, M, matriz);
        if ((suma = HighRightSide(N, M, matriz))>MaxSuma)
            MaxSuma = suma;
        if ((suma = LowerRightSide(N, M, matriz))>MaxSuma)
            MaxSuma = suma;
        if ((suma = LowerLeftSide(N, M, matriz))>MaxSuma)
            MaxSuma = suma;
    
        printf("%d", MaxSuma);
    
        MiddleHigh(N, M, matriz);
        MiddleLow(N, M, matriz);
        RightSide(N, M, matriz);
        LeftSide(N, M, matriz);
        Middle(N, M, matriz);
    }
    Help - Bidimensional array-sumamanzanas2-png

    As you can see, the program prints a lot of thing that are not required. That's just to see if it's working correctly.

    - MiddleHigh is OK
    - LeftSide is OK


    I don't know why, but the program works like if it adds a column in the right side and prints things I didn't write. You can see this in "MiddleLow", "RightSide" and "Middle"


    I hope you understand what I wrote!

    Thanks,
    Juan

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    None of those functions can work until you can compile without errors. Again, start with compiling at maximum warning level:
    Code:
    $ make foo
    gcc -g -Wall -std=c99 -o foo foo.c  
    foo.c: In function ‘MiddleLow’:
    foo.c:88:61: error: ‘sumatotal’ undeclared (first use in this function)
    foo.c:88:61: note: each undeclared identifier is reported only once for each function it appears in
    foo.c:81:35: warning: variable ‘sumtotal’ set but not used [-Wunused-but-set-variable]
    foo.c: In function ‘Middle’:
    foo.c:134:1: warning: control reaches end of non-void function [-Wreturn-type]
    foo.c: In function ‘LeftSide’:
    foo.c:116:1: warning: control reaches end of non-void function [-Wreturn-type]
    foo.c: In function ‘RightSide’:
    foo.c:103:1: warning: control reaches end of non-void function [-Wreturn-type]
    foo.c: In function ‘MiddleLow’:
    foo.c:90:1: warning: control reaches end of non-void function [-Wreturn-type]
    foo.c: In function ‘MiddleHigh’:
    foo.c:77:1: warning: control reaches end of non-void function [-Wreturn-type]
    foo.c: In function ‘PrintOriginalMatrix’:
    foo.c:28:1: warning: control reaches end of non-void function [-Wreturn-type]
    foo.c: In function ‘CompleteBidimArray’:
    foo.c:15:1: warning: control reaches end of non-void function [-Wreturn-type]
    make: *** [foo] Error 1
    You have a variable that isn't declared. Also, you declared all those functions to return an int. If you intend to return something, do so; otherwise declare them to return void.

    You need to make sure, when you're totaling numbers, that you start from zero. Uninitialized automatic (i.e. local) variables have garbage values if not initialized. Also, I think you're over-complicating some of these based on the diagram. The corners should just return a single element it seems. The sides should be one simple for loop with just one sum variable. The middle is the only "tricky" part, but even that's not too hard.

    As for the first problem, work a few examples by hand, ensure you know the process. Pay special attention to how you sum up the neighbors for edge and corner elements.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    First, there is a typo in your code:

    Code:
    ||=== scrap_c, Debug ===|
    main.c||In function 'MiddleLow':|
    main.c|88|error: 'sumatotal' undeclared (first use in this function)|
    main.c|88|error: (Each undeclared identifier is reported only once|
    main.c|88|error: for each function it appears in.)|
    ||=== Build finished: 3 errors, 0 warnings ===|
    I'm assuming this was accidentally introduced while you were in the process of transferring your code to the forum. With that fixed, I received several warnings:

    Code:
    main.c||In function 'Middle':|
    main.c|134|warning: control reaches end of non-void function|
    main.c||In function 'LeftSide':|
    main.c|116|warning: control reaches end of non-void function|
    main.c||In function 'RightSide':|
    main.c|103|warning: control reaches end of non-void function|
    main.c||In function 'MiddleLow':|
    main.c|90|warning: control reaches end of non-void function|
    main.c||In function 'MiddleHigh':|
    main.c|77|warning: control reaches end of non-void function|
    main.c||In function 'PrintOriginalMatrix':|
    main.c|28|warning: control reaches end of non-void function|
    main.c||In function 'CompleteBidimArray':|
    main.c|15|warning: control reaches end of non-void function|
    ||=== Build finished: 0 errors, 7 warnings ===|
    These are all based on the same issue. You declare several functions to return integers, but are not returning anything from them. If you do not plan to return a value, they should be declared as "void" instead of "int". If you do plan on eventually returning a value, but haven't gotten around to that yet, you should put a temporary return value in there (e.g. "return 0;"). The best approach to programming is to write a few lines of code, compile, clear up any warnings/errors, and verify that it works correctly, before moving on to the next few lines of code. Putting dummy "return" statements in functions that will eventually return values (after further development) will reduce the number of warnings you get, so you can focus on real issues the compiler sees. If you didn't get these warnings, you need to increase the warning level of your compiler.



    It seems that you're doing a lot of extra work to deal with the edge cases. You could do all of this work in a single function if you're clever - it would be similar to the "Middle()" function you wrote, with a few additional checks for verification that the elements to be summed are within the bounds of the array. Having all your sum-checking in a single function would greatly simplify this logic, as you would only have to check for the max value in one function, return the corresponding element to "main()", and print. Presto.

    In fact, your current method is fatally flawed. For instance, "Middle()" checks the neighbor-sum of six different elements. You would have to check for the max value (1) inside of the "Middle()" function, (2) return that value to "main()", and (3) check that value (again) against the "max" contained in "main()". You would have to do this for each function that check the neighbor-sum for more than one element.

    However, you can only return a single value from a function. Since you'd be returning the max sum from each of those functions, to be checked in "main()", you would not be able to indicate which element that maximum neighbor-sum came from. (You could do it with structures, but that would be further complicating the logic.)

    So I advise that you re-think your logic, based on my advice.



    One more note - you should use more descriptive variable names. To check your array declaration, I had to look in three places - (1) To find out that 'N' corresponded to the "rows" prompt, (2) To find out that 'M' corresponded to the "columns" prompt, and (3) To find out which dimension each of these were associated with. If instead of 'N' and 'M', you used variable names "rows" and "cols", we would only have to look in one place to clearly understand your array declaration.

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Thanks anduril and Matticus!

    First of all, it says 'sumatotal undeclared' because I forgot to change it (I originally did the program in Spanish)
    Then, all the functions are 'int' because, when it works, I would put, for example: return element[i][j] and, in 'main', MaxElement = Middle (...)

    Maybe I can do all the program in one function, but I don't know how.
    I thought it would be perfect to do this: I receive a bidimensional array of 3x4, so I add 2 columns and 2 rows. Now, the bidimensional array is 4x5. In the column and row I added, I write all 0.

    Something like this:
    Help - Bidimensional array-sumamanzanas3-png

    I ask the user how much rows and columns he wants. I add 2 columns and 2 rows. I put all 0 in the rows and columns I added.

    Now, I can work like everything is in the Middle.

    The problems are:
    - I don't know how to add those columns and rows in that position (I know how to add a column or row in the end, but not where I want)
    - I think that the problem of 1 added column with bargage would continue

    Thanks!

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I must say, I like your diagrams very much. They clearly illustrate the points you're making.

    I thought it would be perfect to do this: I receive a bidimensional array of 3x4, so I add 2 columns and 2 rows. Now, the bidimensional array is 4x5...
    This is a very good idea (note that by adding two, it would actually be 5x6). I've used this approach myself in several programs that use two-dimensional arrays. It makes the bounds-checking I mentioned in my earlier post much easier.

    You should work out on paper what needs to be done before writing the code. Figure out how to accomplish this, step by step, then takes those steps and use it to build your logic.

    I don't know how to add those columns and rows in that position (I know how to add a column or row in the end, but not where I want)
    You're probably over-thinking this. It seems like you're trying to (1) create exactly as many rows and columns as are needed for the input, (2) filling them in with values, then (3) adding additional rows and columns at the "beginnings" and "ends".

    Simplify your logic, and initially create the array with the expanded size. Then you fill them in with the supplied values. It's just a matter of determining which indices to place the data you receive.

    Let's assume the array you just proposed - 4 columns by 3 rows.

    1. User enters 4 for columns (stored in "cols") and 3 for rows (stored in "rows")
    2. Declare you array with ("cols" + 2) columns and ("rows" + 2) rows
    3. Initialize the array to all zeroes (simple as: char matrix[rows][cols] = {0}; )
    4. Read in the values like you normally do (12 values, 4x3)
    5. Instead of running your loops from 0 to n-1, run them from 1 to n-2

    That, in a nutshell, is how you can implement your idea. I gave a little more direct advise than I normally do, because it seems like you're really putting effort in to think about and understand your problem. Before implementing anything in code, I'd strongly suggest you draw it out on paper and understand how the array indexing will work.

  6. #6
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Thanks for you answer!

    Here I have the code for the bidimensional array, but it has a problem.. it only works for 1x1, 2x2, 1x2, 2x1... when I put 3 cols and 3 rows, it doesn't work!!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void CompleteBidimArray(int rows, int cols, int mat[rows][cols])
    {
        int i, j;
    
        for (i=0; i<rows; i++)
        {
            mat[i][0]=0;
            mat[i][cols-1]=0;
        }
        for (j=0; j<cols; j++)
        {
            mat[0][j]=0;
            mat[rows-1][j]=0;
        }
        for(i=1; i<rows-1; i++)
        {
            for(j=1; j<cols-1; j++)
            {
                printf("\n[%d][%d]: ", i, j);
                scanf("%d", &mat[i][j]);
            }
        }
    
    }
    
    
    void PrintBidimArray(int rows, int cols, int mat[rows][cols])
    {
        int i, j;
        for(i=0; i<rows; i++)
        {
            for (j=0; j<cols; j++)
            {
                printf("%4d", mat[i][j]);
            }
            printf("\n");
        }
    }
    
    int main()
    {
    
        int rows, cols;
        printf("\nRows: ");
        scanf("%d", &rows);
        printf("\nColumns: ");
        scanf("%d", &cols);
    
        int mat[rows][cols];
    
        CompleteBidimArray(rows+2, cols+2, mat);
        PrintBidimArray(rows+2, cols+2, mat);
        return 0;
    }
    I don't know why...

    Thankssss

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Code:
    int mat[rows][cols];
    Take a look at that, then read Matticus' post #5, item 2 carefully. How many rows and columns does your array have? How many do you try to access?

    The fact that it "worked" for 1x2, 2x2, etc was sheer, dumb luck. You were accessing the array out of bounds, which results in undefined behavior meaning anything, or nothing, could happen.

  8. #8
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Yesssss! That was the problem!

  9. #9
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Here I have the program working perfectly!!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void CompleteBidimArray(int rows, int cols, int mat[rows][cols])
    {
        int i, j;
    
        for (i=0; i<rows; i++)
        {
            mat[i][0]=0;
            mat[i][cols-1]=0;
        }
        for (j=0; j<cols; j++)
        {
            mat[0][j]=0;
            mat[rows-1][j]=0;
        }
        for(i=1; i<rows-1; i++)
        {
            for(j=1; j<cols-1; j++)
            {
                printf("\n[%d][%d]: ", i-1, j-1);
                scanf("%d", &mat[i][j]);
            }
        }
    }
    
    void PrintBidimArray(int rows, int cols, int mat[rows][cols])
    {
        int i, j;
        for(i=1; i<rows-1; i++)
        {
            for (j=1; j<cols-1; j++)
            {
                printf("%4d", mat[i][j]);
            }
            printf("\n");
        }
    }
    
    void Sum (int rows, int cols, int mat[rows][cols])
    {
        int i, j, MaxSum, FirstSum, sum1, sum2, sum3, TotalSum, ElementMax, iMax, jMax;
    
        FirstSum=mat[1][2]+mat[2][2]+mat[2][1];
        MaxSum=FirstSum;
        iMax=1;
        jMax=1;
        for(i=1; i<rows-1; i++)
        {
            for(j=1; j<cols-1; j++)
            {
                sum1=mat[i-1][j-1]+mat[i-1][j]+mat[i-1][j+1];
                sum2=mat[i][j-1]+mat[i][j+1];
                sum3=mat[i+1][j-1]+mat[i+1][j]+mat[i+1][j+1];
                TotalSum=sum1+sum2+sum3;
    
                if (TotalSum>MaxSum)
                {
                    MaxSum=TotalSum;
                    iMax=i;
                    jMax=j;
                }
            }
        }
        printf("\nMaximum Sum: %d", MaxSum);
        printf("\nMaximum Element: [%d][%d]", iMax-1, jMax-1);
    }
    
    
    
    int main()
    {
    
        int rows, cols;
        printf("\nRows: ");
        scanf("%d", &rows);
        printf("\nColumns: ");
        scanf("%d", &cols);
    
        int mat[rows+2][cols+2];
    
        CompleteBidimArray(rows+2, cols+2, mat);
        PrintBidimArray(rows+2, cols+2, mat);
        Sum(rows+2, cols+2, mat);
        return 0;
    }
    Maybe you have a much more intelligent way to make the sum!

    Thanksssssss




    Edit: I initialized iMax and jMax in 1 because the program admits negative numbers, so, if I put that the MaximumSum was 0 and all the others sums are negative, it would be wrong
    Last edited by juanjuanjuan; 02-14-2014 at 03:44 PM.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by juanjuanjuan View Post
    Maybe you have a much more intelligent way to make the sum!
    Nope, that is more or less how I would write this program. Can't get too much better.
    Quote Originally Posted by juanjuanjuan View Post
    Edit: I initialized iMax and jMax in 1 because the program admits negative numbers, so, if I put that the MaximumSum was 0 and all the others sums are negative, it would be wrong
    The code looks right, but I find your explanation confusing. Still, I think you understand correctly. Good job!

  11. #11
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Haha.. maybe my explanation is confusing because I can't write everything I want in English haha

    What I wanted to say is, for example, if I have 5 numbers and I want to print the higher, I would do:
    Higher=FirstNumber
    for(i=SecondNumber;i<=FifthNumber;i++)
    if (i>Higher)
    Higher=i

    and not this:
    Higher=0
    because 0 is higher than negative numbers!

    I'm now trying to solve another problem...

    Don't you think it would be better to use a for or something like that to do this:
    sum1=mat[i-1][j-1]+mat[i-1][j]+mat[i-1][j+1];
    sum2=mat[i][j-1]+mat[i][j+1];
    sum3=mat[i+1][j-1]+mat[i+1][j]+mat[i+1][j+1];
    TotalSum=sum1+sum2+sum3;
    ?

    Thanks! You all are the best!

  12. #12
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I'd probably have left the original array size and just added a conditional for when to add (e.g. if i >=0 && j >=0 then add to sum) but I guess your solution is just as valid and possibly more efficient (if that even matters!) I don't understand why your loops start at 1 either. Well, I do, now that I look at the code but it's a bit non-obvious.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by juanjuanjuan View Post
    Haha.. maybe my explanation is confusing because I can't write everything I want in English haha

    What I wanted to say is, for example, if I have 5 numbers and I want to print the higher, I would do:
    Higher=FirstNumber
    for(i=SecondNumber;i<=FifthNumber;i++)
    if (i>Higher)
    Higher=i

    and not this:
    Higher=0
    because 0 is higher than negative numbers!
    As I suspected, you understand perfectly, just a difficulty translating. That is exactly correct.

    Quote Originally Posted by juanjuanjuan View Post
    I'm now trying to solve another problem...

    Don't you think it would be better to use a for or something like that to do this:
    sum1=mat[i-1][j-1]+mat[i-1][j]+mat[i-1][j+1];
    sum2=mat[i][j-1]+mat[i][j+1];
    sum3=mat[i+1][j-1]+mat[i+1][j]+mat[i+1][j+1];
    TotalSum=sum1+sum2+sum3;
    ?

    Thanks! You all are the best!
    I don't think this would lend well to a loop. It would be something like the following:
    Code:
    TotalSum = 0;
    for (y = i-1; y <= i+1; y++) {
        for (x = j-1; x <= j+1; x++) {
            if (y != i || x != j) {
                TotalSum += mat[y][x];
            }
        }
    }
    
    // Or, you could initialize TotalSum to the opposite (negative) value of mat[i][j], then ditch the if inside the loop
    // so you add in mat[i][j], which cancels out the initial value
    TotalSum -= mat[i][j];
    That is hardly any clearer, possibly less so. If for some crazy reason, you had a three-, four- or higher-dimension matrix, and had to sum all the neighbors of an element, in all those dimensions, then maybe a iterative (loop) or recursive solution would make more sense. But your way seems easiest and cleanest to me, especially for a 2-d array.

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by Hodor View Post
    I'd probably have left the original array size and just added a conditional for when to add (e.g. if i >=0 && j >=0 then add to sum) but I guess your solution is just as valid and possibly more efficient (if that even matters!) I don't understand why your loops start at 1 either. Well, I do, now that I look at the code but it's a bit non-obvious.
    You end up needing either awkwardly nested if statements, or separate bounds checking, covering all 8 neighbor positions, which I find makes the code more "natural" in a sense, but somewhat awkward to read. A quick comment near the declaration of mat with "add zeros around the outside of the matrix to simplify neighbor summation" should be sufficient to clear up the non-obvious (IMO at least).

    More often than not, this probably boils down to a style issue, but you do raise a valid point. Plus, if space is an issue, then your method is definitely preferable as well.
    Last edited by anduril462; 02-14-2014 at 06:56 PM.

  15. #15
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Quote Originally Posted by Hodor View Post
    I'd probably have left the original array size and just added a conditional for when to add (e.g. if i >=0 && j >=0 then add to sum) but I guess your solution is just as valid and possibly more efficient (if that even matters!) I don't understand why your loops start at 1 either. Well, I do, now that I look at the code but it's a bit non-obvious.
    Because all the column for i=0 and i=rows and all the row for j=0 and j=cols are fulled by 0's. If I make there a sum of neighbours, it would sum any bargage that is in the position (i-1), for example.

    --------

    Thanks for all your answers!
    Last edited by juanjuanjuan; 02-14-2014 at 07:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with pointers and bidimensional arrays
    By Gustaff in forum C Programming
    Replies: 4
    Last Post: 02-07-2013, 12:42 AM
  2. Replies: 16
    Last Post: 06-15-2012, 03:05 PM
  3. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  4. 4 questions about bidimensional table
    By Dan. in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2011, 11:18 AM
  5. create and populate create bidimensional array
    By darkducke in forum C Programming
    Replies: 0
    Last Post: 12-03-2010, 07:06 AM