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
