Sum of the contour of an 2d array in C with pointers

Viewed 73

Hello I'm still new to programming and I still have difficulty moving around in a 2d array with pointers, and so I was wondering if someone could help me understand the concept better with this program I have to write in which I have to calculate the sum of the contours of an 2d array, I already wrote the code with indicators(i, j) but I can only use pointers so I was wondering if someone could explain to me on how I could transform the last big block of for loops and move around in the 2d array with only pointers, so if someone could help me on how I could obtain the same result by using pointers that could be quite useful, thank you in advance and sorry if I wasn't clear in which case please ask so I may be able to make my question more clear ^^.

here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int
main()
{
    int i,
     j,
     contour,
     sum,
     max,
     array[20][20],
    *p,
     n,
     contourmax;

    srand(time(NULL));

    do {
        printf("How many rows and columns? ");
        scanf("%d", &n);
    } while (n <= 0 || n > 20);

    for (i = 0, p = &array[0][0]; i < n; i++, p = p + 20 - n) {
        for (j = 0; j < n; j++, p++) {
            *p = rand() % 2;
        }
    }

    printf("\nHere is your array:\n");

    for (i = 0, p = &array[0][0]; i < n; i++, p = p + 20 - n) {
        for (j = 0; j < n; j++, p++) {
            printf("%d\t", *p);
        }
        printf("\n");
    }

    for (contour = 0, max = 0; contour < n / 2; contour++) {
        for (i = contour, sum = 0; i < n - contour; i++) {
            for (j = contour; j < n - contour; j++) {
                if (i == contour || i == n - 1 - contour || j == contour || j == n - 1 - contour) {
                    sum += array[i][j];
                }
            }
        }

        printf("\nSum of the contour %d = %d", contour, sum);

        if (max < sum) {
            max = sum;
            contourmax = contour;
        }
    }

    printf("\nThe maximum contour is %d with the sum %d\n", contourmax, max);

    return 0;
}

so by the sum of the contour here is what i mean, each color is a different border/contour and I have to basically calculate the sum of each contour while using only pointers, such as my pointer p or creating a new pointer but can't use the array directly like array[i][j] or something

image contour

1 Answers

When you input an n that is less than 20, you have an incorrect stride.

A matrix stride is the number of elements between rows of a matrix. Normally, the stride is the same as the matrix width.

But, if n is 7 (the matrix width), the stride is [still] 20. The stride can also be thought of as:

stride = &array[1][0] - &array[0][0];

Your code has a bug. We can't just add 20 to p. We need a second pointer that points to the start of the row (e.g. p0).

To clean things up a bit, we can do (e.g.): #define AMAX 20 and replace 20 with AMAX.

Here is the corrected code for the first loop:

int *p0;

p0 = &array[0][0];
for (i = 0; i < n; i++, p0 += AMAX) {
    p = p0;
    for (j = 0; j < n; j++, p++)
        *p = rand() % 2;
}

UPDATE:

Thank you, at college they taught I had to move around using the method I was using so this clears up that a bit however I'm still confused on how I can obtain the the same result for the sum of the contours in the last for loops where I use [] instead of my pointer p and such to move around in my array, but thank you for this explication, I will modify my code – LinksBurner

Applying the p0 trick is somewhat similar:

max = 0;
for (contour = 0; contour < n / 2; contour++) {
    int edge = n - 1 - contour;

    sum = 0;

    p0 = &array[0][0];
    for (i = 0;  i < n;  ++i, p0 += AMAX) {
        // top/bottom row of the contour
        if ((i == contour) || (i == edge)) {
            // do all elements in the row segment
            p = p0 + contour;
            for (j = contour;  j <= edge;  ++j, ++p)
                sum += *p;
        }

        // _not_ the top/bottom row -- just do first/last element of row
        else {
            sum += p0[contour];
            sum += p0[edge];
        }
    }

    printf("\nSum of the contour %d = %d", contour, sum);

    if (max < sum) {
        max = sum;
        contourmax = contour;
    }
}

I know how to do the loops for a "border" or "window frame", but I'm not totally sure about a "contour" as you're doing it, It looks like a border/frame and the "radius" changes, so I just followed your code.

Note that putting the if for top/bottom row inside the inner loop is wasteful/slow. So, I modified the code a bit.

I think it's correct but I could be wrong.

Related