#include <stdio.h>
#include <stdlib.h>
int value(int **, int, int, int, int);
int main(void) {
int a;
scanf("%i", &a);
int l;
scanf("%i", &l);
// declaring space for storage
// error checking
int **arr = malloc(sizeof(int *) * a);
if (arr == NULL) {
return 1;
}
for (int i = 0; i < a; i++) {
arr[i] = malloc(sizeof(int) * a);
if (arr[i] == NULL) {
return 1;
}
}
for (int i = 0; i < a; i++) {
for (int j = 0; j < a; j++) {
scanf("%i", &arr[i][j]);
}
}
for (int i = 0; i < a; i++) {
for (int j = 0; j < a; j++) {
int t = value(arr, i, j, a, l);
printf("%i ", t);
}
printf("\n");
}
return 0;
}
int value(int **arr, int a, int b, int n, int l) {
int val = 0;
for (int i = a - l; i < a + l + 1; i++) {
for (int j = b - l; b < b + l + 1; b++) {
if (i >= 0 && i < n && j >= 0 && j < n) {
val += arr[i][j];
}
}
}
return val;
}
Why is it not giving me output and still wants more input
This is the assignment:
- take
a
input- take
l
inputa
is the matrix sizel
is the smoothing factorfor all the values entered, print the sum of surrounding elements from
i - l
toi + l
in both the directions if possible.