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]);
}