0

I was solving questions on hacker rank, and I came across this question titled "Dynamic Array In C" (here is the link to the question), and I wrote a code for it. The code works fine in online compilers, but it is not working in local IDE(I tried VS code and Code blocks). I will really appreciate if you can help me out. Given below is the code.

NOTE: I think that there is some problem regarding 'scanf taking up new line character'. Because, the local IDEs were taking only few inputs and then exiting the program. But I don't know how to solve it.

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

void insertbook(int, int, int *, int *);
void printpages(int, int, int *);
void printbooks(int, int *);

int main()
{

    int query, num_shelf;
    int querytype;
    int x, y;

    scanf("%d\n", &num_shelf);

    int *shelf = (int *)malloc(num_shelf * 100000 * sizeof(int));
    int *count = (int *)malloc(num_shelf * sizeof(int));

    scanf("%d\n", &query);

    for (int i = 0; i < query; i++)
    {
        scanf("%d", &querytype);

        if (querytype == 1)
        {
            scanf("%d\n", &x);
            scanf("%d\n", &y);
            insertbook(x, y, shelf, count);
        }
        else if (querytype == 2)
        {
            scanf("%d\n", &x);
            scanf("%d\n", &y);
            printpages(x, y, shelf);
        }
        else
        {
            scanf("%d\n", &x);
            printbooks(x, count);
        }
    }

    return 0;
}

void insertbook(int x, int y, int *shelf, int *count)
{
    shelf[(x * 100000) + count[x]] = y;
    count[x] = count[x] + 1;
}

void printpages(int x, int y, int *shelf)
{
    printf("%d\n", shelf[(x * 100000) + y]);
}

void printbooks(int x, int *count)
{
    printf("%d\n", count[x]);
}
  • 2
    This assignment shelf[(x * 100000) + count[x]] = y; invokes undefined behavior because the array pointed to by the pointer count was not initialized. – Vlad from Moscow May 04 '21 at 15:41
  • Very vulnerable code. You never check that user input is valid. If a user inputs 10 for `num_shelf` and later inputs 100 for `x` you access arrays out of bounds. Anything can happen – Support Ukraine May 04 '21 at 15:41
  • 1
    And... `scanf("%d\n", &x);` --> `scanf("%d", &x);` or better `if (scanf("%d", &x); != 1) exit(1);` – Support Ukraine May 04 '21 at 15:43
  • Nitpick: Instead of `x` and `y` you should use names that describes the purpose of the variables – Support Ukraine May 04 '21 at 15:46
  • @4386427 Usually these problems guarantee valid input within the specified constraints. – Ian Abbott May 04 '21 at 15:47
  • 1
    @IanAbbott hmm... really... so it teach sloppy programming. Great – Support Ukraine May 04 '21 at 15:48
  • @4386427 They are mainly concerned with solving algorithmic problems rather than input validation problems! – Ian Abbott May 04 '21 at 15:52
  • 1
    Hmmm... just read the problem description. It says that `num_shelfs` can be 100000. So in that case you `malloc` 100000*100000 integers. That's a lot... especially when there can't be more than 1100 books per shelf. – Support Ukraine May 04 '21 at 15:54
  • BTW: " The code works fine in online compilers, but it is not working in local IDE" is a good problem description. What isn't working in local IDE? Please explain... – Support Ukraine May 04 '21 at 15:58
  • Maybe the local system gets "sad" when you malloc 40G memory – Support Ukraine May 04 '21 at 16:00
  • 1
    Further, you don't really follow the problem description. It requires `shelf` to be a `int**` – Support Ukraine May 04 '21 at 16:01
  • The trailing newline in statements such as `scanf("%d\n", &x);` is a UX disaster. You have to type something other than white space (not a blank, tab or newline) to terminate the input. That usually does not work well when humans are providing the input (it requires prescience on their part to know what to type for the next input). It can sorta work OK if the input comes from a file, but it isn't necessary. Either do line-based input (`fgets()` and then `sscanf()` the result) or omit the trailing white space in the format. Only `%c`, `%[…]` (scan sets) and `%n` do not skip white space anyway. – Jonathan Leffler May 04 '21 at 16:16

0 Answers0