-2

I am trying to solve Hackerrank Find Digits problem and receiving this runtime error for questions involving arrays. I have tried freeing the array memory space but it's still the same. What changes do I make?

I tried declaring the arrays conventionally as well as by using malloc function.

#include <bits/stdc++.h>
using namespace std;
int main() {
int testCase,rem,count[]={0},n;
scanf("%d", &testCase);
int *numbers = (int*) malloc(testCase*sizeof(int));
for(int i=0; i<testCase; i++) 
    scanf("%d",numbers + i);
for(int i=0; i<testCase; i++) {
    n = *(numbers + i);
    while(n!=0) {
        int q = n % 10;      //get the units place
        rem = *(numbers + i) % q;         //find remainder
        if(rem == 0)
            count[i]++;
        n/=10;
    }
}
free(numbers);
for(int i=0; i<testCase; i++)
    printf("%d", count[i]);
free(count);
 return 0;
}
  • 1
    The array size of `count` will always be 1. Also that's mor c code than c++. – πάντα ῥεῖ May 11 '19 at 08:17
  • 1
    The first line of your code is (strictly speaking) neither C++ nor C. The second line of your code is C++, but is considered [bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice?rq=1). The rest of your code is purely C instead of C++. – L. F. May 11 '19 at 08:17
  • My earlier code was C++ but Hackerrank compiler showed some issues with cin and cout (which I fixed now), anyway, the count size is indeed 1, I will rectify that too. Thanks! –  May 11 '19 at 08:31
  • [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Some programmer dude May 11 '19 at 08:36
  • And almost *never* do you need to use `malloc` and `free` in C++ code. Or `scanf` or `printf` for that matter. And instead of e.g. `*(numbers + i)` use the more natural `numbers[i]`. – Some programmer dude May 11 '19 at 08:37
  • 1
    As a possible hint about your problem (which you don't clearly spell out), how many elements do you think there is in the `count` array? Perhaps [a good book or two](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) would be useful? – Some programmer dude May 11 '19 at 08:39
  • Thanks a lot peeps. I made the necessary changes like numbers[i], the header files were by default included in the Hackerrank compiler, but I'll keep that in mind now on. My new code is purely C++. –  May 11 '19 at 10:28

2 Answers2

1

As you could notice from the comments, your count array was implicitly set to contain only 1 element from the start. Furthermore, this is not the only problem that caused you a run-time error.

If you use operator modulus(%) you have to be sure that you do not divide with zero, otherwise you will get a floating point exception - rem = *(numbers + i) % q;

I would also recommend you to use either C++ or C, because you combine both of them in your code and it is considered to be unpractical.

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {
    int testCase,rem,n;
    scanf("%d", &testCase);
    int *numbers = (int*) malloc(testCase*sizeof(int));
    int *count= (int*) malloc(testCase*sizeof(int));
    for(int i=0; i<testCase; i++) {
        scanf("%d",numbers + i);
        count[i] = 0;
    }
    for(int i=0; i<testCase; i++) {
        n = *(numbers + i);
        while(n!=0) {
            int q = n % 10;      //get the units place
            if (q == 0) {
                n /= 10;
                continue;
            }

            rem = *(numbers + i) % q;         //find remainder
            if(rem == 0)
                count[i]++;
            n/=10;
        }
    }
    free(numbers);
    for(int i=0; i<testCase; i++)
        printf("%d", count[i]);
    free(count);

    return 0;
}
Croppi
  • 172
  • 11
0

// i have not corrected code to print appropriate count. i have just suggested why you are getting runtime error

free should be used when memory is allocated using malloc/realloc

memory has not been allocated to count dynamically so do not use free(count).

Also no need to that header in first line. PFB code below

#include <stdio.h>
int main() {
int testCase,rem,count[]={0},n;
scanf("%d", &testCase);
int *numbers = (int*) malloc(testCase*sizeof(int));

for(int i=0; i<testCase; i++) 
    scanf("%d",(numbers+i));
for(int i=0; i<testCase; i++) 
    printf("%d\n",numbers[i]);

for(int i=0; i<testCase; i++) {
    n = *(numbers + i);
    while(n!=0) {
        int q = n % 10;      //get the units place
        rem = *(numbers + i) % q;         //find remainder
        if(rem == 0)
            count[i]++;
        n/=10;
    }
}
free(numbers);
for(int i=0; i<testCase; i++)
    printf("%d", count[i]);
 return 0;
}
user265906
  • 120
  • 1
  • 8