-1
#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 input
  • a is the matrix size
  • l is the smoothing factor

for all the values entered, print the sum of surrounding elements from i - l to i + l in both the directions if possible.

enter image description here

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Have you tried stepping through the code with a debugger? Why are you posting text as images? – Quimby Mar 25 '23 at 11:48
  • 1
    Have you tried running your code line-by-line in a debugger while monitoring the control flow and the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Mar 25 '23 at 11:49
  • @Sahil Gautam It is unclear where is scanf not stopping? – Vlad from Moscow Mar 25 '23 at 11:52
  • 1
    after looping for a * a, i should get output. it does not show any. like an infinite loop – Sahil Gautam Mar 25 '23 at 11:56
  • 1
    The problem has nothing to do with `scanf`, but rather with an infinite loop in the function `value`. This is clearly visible when running your program line by line in a [debugger](https://stackoverflow.com/q/25385173/12149471). I strongly suggest that you learn to use that tool. – Andreas Wenzel Mar 25 '23 at 12:05
  • @SahilGautam The first time `for (int j = b - l; b < b + l + 1; b++) {` is executed, what is the value of `b`? – chux - Reinstate Monica Mar 25 '23 at 12:07

1 Answers1

1

There is a bug the the value function: the inner loop for (int j = b - l; b < b + l + 1; b++) should be written:

    for (int j = b - l; j < b + l + 1; j++)

As coded, the loop never ends: it actually has undefined behavior when computing b + l + 1 eventually causes an arithmetic overflow.

Your program does not stop. You can type extra input that the terminal echoes, but the program is not reading it.

You should also check the return values of scanf() to detect invalid or missing input.

chqrlie
  • 131,814
  • 10
  • 121
  • 189