1

I wrote the code for a simple problem on HackerRank where you have to count the number of digits of a number which divide the number with 0 remainder.

I used a basic while loop for testing each digit. Number of test cases are t and the number is n.

The code that I wrote is:

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

int main()
{
    int t;
    cin >> t;
    int count[t];
    for (int t_itr = 0; t_itr < t; t_itr++) {
        int n;
        cin >> n;
        int dig,temp=n;
        count[t_itr]=0;
        while(n>0){
            dig=n%10;
            if(temp%dig==0){
                count[t_itr]++;
            }
            n=n/10;
        }
    }
    for(int x=0;x<t;x++){
        cout<<count[x]<<endl;
    }
    return 0;
}

Input:

2
12
1012

Expected Output:

2
3

My Output:

~ no response on stdout ~
Drashti
  • 21
  • 8
  • 3
    You should never `#include `. It is not proper C++. It ruins portability and fosters terrible habits. See [Why should I not `#include `](https://stackoverflow.com/q/31816095). – L. F. Jul 23 '19 at 00:29
  • 2
    Also, please avoid `using namespace std;`. It is considered bad practice. See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721) – L. F. Jul 23 '19 at 00:29
  • @L.F. Thanks a lot for sharing this information, – Drashti Jul 23 '19 at 00:32
  • 2
    The declaration `int count[t];` is invalid C++ only as an extension to some compilers. Please use `std::vector`. Arrays must have capacities defined at compile time. – Thomas Matthews Jul 23 '19 at 00:34
  • 2
    `if (temp % dig == 0)` isn't valid when `dig` is 0. Your program crashes. – Retired Ninja Jul 23 '19 at 00:35
  • @ThomasMatthews Thanks for the information. But is this why I'm not getting any output? – Drashti Jul 23 '19 at 00:36
  • 1
    What are the results when you used a debugger? Which statement is causing the issue? What are the values of variables used in the statement? – Thomas Matthews Jul 23 '19 at 00:38
  • @RetiredNinja Yeah, that was the issue. Thanks for helping. – Drashti Jul 23 '19 at 00:41
  • @Drashti If you used a real compiler, you would have seen the error right away, as [demonstrated here](http://coliru.stacked-crooked.com/a/6ec774bd0af37e1e). Also note the data -- it is hard-coded into the program, which is what you should have done in your code if you're going to post a [mcve]. – PaulMcKenzie Jul 23 '19 at 00:44

1 Answers1

1

The problem, as pointed out by @RetiredNinja, is that you are invoking undefined behavior:

The C++ Standard(2003) says in §5.6/4,

[...] If the second operand of / or % is zero the behavior is undefined; [...]

That is, following expressions invoke undefined-behavior(UB):

X / 0; //UB X % 0; //UB

Undefined Behavior, if you're not aware:

undefined behavior - there are no restrictions on the behavior of the program.

In other words, if your program has Undefined Behavior in it, C++ puts no restrictions on what happens in the program. Your program can literally do anything, including--but not limited to--not putting anything in std::cout.