aditya
aditya

Reputation: 103

How to fix the "~ no response on stdout ~" error in HackerRank?

I am trying to solve this problem on HackerRank and once I submit my solution, the compiler shows wrong answer and it shows ~ no response on stdout ~ in the output window. How do I fix this?

Question:

Given a 6X6 2D Array A,

1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

We define an hourglass in A to be a subset of values with indices falling in this pattern in A's graphical representation:

a b c
  d
e f g

There are 16 hourglasses in A, and an hourglass sum is the sum of an hourglass' values.

Task

Calculate the hourglass sum for every hourglass in A, then print the maximum hourglass sum.

I have already tried solving the problem. I just can't get rid of this error message. The logic and the solution seem to be correct.

#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *readline();
char **split_string(char *);

void func(int arr[][100]) {
    int hgs[4][4];
    int i = 0, j = 0;
    for (i = 0; i < 4; i++) {
       for (j = 0; j < 4; j++) {
           hgs[i][j] = arr[i][j] + arr[i][j+1] + arr[i][j+2] +
                       arr[i+1][j+1] +
                       arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2];
        }
    }
    int max = hgs[0][0];
    for (i = 0; i < 4; i++) {
        for (j = 0; j < 4; j++) {
            if (hgs[i][j] > max)
                max = hgs[i][j];
        }
    }
    printf("%d", max);
}

int main() {
    int **arr = malloc(6 * sizeof(int*));

    for (int i = 0; i < 6; i++) {
        *(arr + i) = malloc(6 * (sizeof(int)));

        char **arr_item_temp = split_string(readline());

        for (int j = 0; j < 6; j++) {
            char* arr_item_endptr;
            char* arr_item_str = *(arr_item_temp + j);
            int arr_item = strtol(arr_item_str, &arr_item_endptr, 10);

            if (arr_item_endptr == arr_item_str || *arr_item_endptr != 
'\0') {
                exit(EXIT_FAILURE);
            }
            *(*(arr + i) + j) = arr_item;
       }
    }
    func(**arr);
    return 0;
}

char *readline() {
    size_t alloc_length = 1024;
    size_t data_length = 0;
    char* data = malloc(alloc_length);

    while (true) {
        char *cursor = data + data_length;
        char *line = fgets(cursor, alloc_length - data_length, stdin);

        if (!line) {
            break;
        }
        data_length += strlen(cursor);
        if (data_length < alloc_length - 1 || data[data_length - 1] == 
'\n') {
            break;
        }

        size_t new_length = alloc_length << 1;
        data = realloc(data, new_length);
        if (!data) {
            break;
        }

        alloc_length = new_length;
    }

    if (data[data_length - 1] == '\n') {
        data[data_length - 1] = '\0';
    }

    data = realloc(data, data_length);

    return data;
}

char **split_string(char *str) {
    char **splits = NULL;
    char *token = strtok(str, " ");

    int spaces = 0;

    while (token) {
        splits = realloc(splits, sizeof(char*) * ++spaces);
        if (!splits) {
            return splits;
        }

        splits[spaces - 1] = token;

        token = strtok(NULL, " ");
    }
    return splits;
}

Expected output is 19 for the sample input, but I am not getting any output.

Upvotes: 2

Views: 2501

Answers (1)

chqrlie
chqrlie

Reputation: 144969

Your code is very complicated for this simple program, but the problem might be very simple:

  • The prototype for func should be:

    void func(int **arr)
    
  • You should end your output with a newline:

        printf("%d\n", max);
    
  • You should pass the array to func this way:

        func(arr);
    

Here is a simplified version of the func code:

void func(int **arr) {
    int i, j, cur, max = INT_MIN;
    for (i = 0; i < 4; i++) {
        for (j = 0; j < 4; j++) {
            cur = arr[i][j] + arr[i][j+1] + arr[i][j+2] +
                  arr[i+1][j+1] +
                  arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2];
            if (max < cur)
                max = cur;
        }
    }
    printf("%d\n", max);
}

Here is a much simplified version of the whole program:

#include <limits.h>
#include <stdio.h>

int main() {
    int grid[6][6];
    int i, j, cur, max = INT_MIN;

    for (i = 0; i < 6; i++) {
        for (j = 0; j < 6; j++) {
            if (scanf("%d", &grid[i][j]) != 1)
                return 1;
        }
    }
    for (i = 0; i < 4; i++) {
        for (j = 0; j < 4; j++) {
            cur = arr[i][j] + arr[i][j+1] + arr[i][j+2] +
                  arr[i+1][j+1] +
                  arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2];
            if (max < cur)
                max = cur;
        }
    }
    printf("%d\n", max);
    return 0;
}

Upvotes: 1

Related Questions