-2

Here's my Implementation i take the number and put it into the stack by doing mathematical calculation ... but it's not producing correct output even my logic regarding the problem is correct ... Please detect what is wrong in my implementation. It's competitive programming Question asked by two companies in my College . So, please help me to correct the implementation.... I only want to know what is wrong with my implementation .....

Actual Question: Input: The first line of input contains an integer T denoting the number of test cases. There will be a single line for each testcase which contains N. Output: Print all binary numbers with decimal values from 1 to N in a single line. Constraints: 1 ≤ T ≤ 106 1 ≤ N ≤ 106

Example: Input: 2 2 5

Output: 1 10 1 10 11 100 101

Explanation: Testcase 1: Binary numbers from 1 to 2 are 1 and 10.

This is the Required Output Input: 2 2 5

Output: 1 10 1 10 11 100 101

And my output is : For Input: 1 3 Your Output is:

Can any one here tell me the reason of incorrect output ..

#include<iostream>
#include <stack> 
#include <queue> 
using namespace std;
int main()
{
    int T ;
    cin>>T;
    while(T--)
    {
        int  n;
        cin>>n;
        for(int i=1;i<=n;i++)
        {   
            stack <int > s;
            int num=i;
            while(num>0)
            {  
                s.push(num%2);
                num=num/2 ;
            }
            for(int j=0;j<s.size();j++)
            {
                cout<<s.top();
                s.pop();
            }
            cout<<" ";
        }
        cout<<endl;
    }
return 0 

This is the Required Output

Input:
2
2
5

Output:
1 10
1 10 11 100 101

And my output is :

For Input:
1
3
Your Output is:

Can any one here tell me the reason of incorrect output ..

sanjjeev dutt
  • 21
  • 1
  • 8
  • 1
    If it were a function, it would be a lot easier for you to write the tests. Looking. – Kenny Ostrom Jun 26 '19 at 13:36
  • Same happen in case of Function .... the problem is with output don't know i am right or Wrong .. but i also tried with function got the same error in output – sanjjeev dutt Jun 26 '19 at 13:38
  • 3
    `for(int j=0;i `for(int j=0;j – Max Langhof Jun 26 '19 at 13:38
  • This doesn't produce the output you claim. – Kenny Ostrom Jun 26 '19 at 13:40
  • For Input: 2 2 5 Your Output is: 1 1 1 1 1 10 10 output is still incorrect ... – sanjjeev dutt Jun 26 '19 at 13:41
  • Please edit your comments instead of adding new ones when you want to amend them. (You can do that for 5 minutes after posting them). – Max Langhof Jun 26 '19 at 13:42
  • 2
    For clarification you should [edit] your question and not post tons of comments – Jabberwocky Jun 26 '19 at 13:42
  • @ Kenny Ostrom ..its giving output but the output is incorrect and i tried my best to find the mistake in my implementation .. if you are able to detect then please correct the above Implementation – sanjjeev dutt Jun 26 '19 at 13:43
  • 3
    I feel urge to flag this as duplicate of [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Yksisarvinen Jun 26 '19 at 13:45
  • @Jabberwocky . I already edited my Post i only want to know why my implementation is not producing correct output.. Even my Logic is correct .. Help me about their is something wrong with my logic or with y output – sanjjeev dutt Jun 26 '19 at 13:52
  • @Yksisarvinen ... I already edited my Post i only want to know why my implementation is not producing correct output.. Even my Logic is correct .. Help me about their is something wrong with my logic or with y output – sanjjeev dutt Jun 26 '19 at 13:52
  • 2
    It would help a lot if you could describe what the program is supposed to do instead of making people guess from an example. – molbdnilo Jun 26 '19 at 13:56
  • 2
  • @molbdnilo hey i edited my question. yes but both loops work separately regarding j – sanjjeev dutt Jun 26 '19 at 14:00
  • Reread molbdnilo's last comment. – Kenny Ostrom Jun 26 '19 at 14:01
  • @Kenny Ostrom if it's creating the problem then then in output i supposed to segmentation Fault but it's Working Fine and See again my implementation popping loop is different and pushing loop is different .. i think it's the only advantage of Vector – sanjjeev dutt Jun 26 '19 at 14:04
  • 2
    @sanjjeevdutt There are no situations where you are *supposed* to get a segmentation fault. – molbdnilo Jun 26 '19 at 14:12
  • (1) Please post code that compiles.  The code you posted is missing a `;` and a `}`. (2) Please tell us what output you are getting.  Are you getting ***no** output?*  You seem to be saying that your output is `Your Output is:` — is that what you’re saying? (3) The biggest problem in your code has been pointed out in a comment, above. – G-Man Says 'Reinstate Monica' Jun 26 '19 at 14:22

1 Answers1

1

The problem is here:

for(int j=0;j<s.size();j++)
{
    cout<<s.top();
    s.pop();
}

Suppose s has four elements.
On the first iteration, j is 0 and s.size() is 4.
On the second, j is 1 and s.size() is 3.
On the third, j is 2 and s.size() is 2, so the loop terminates.
That's two iterations instead of four.

Instead of counting the times you're going to pop, just pop until the stack is empty:

while (!s.empty())
{
    cout << s.top();
    s.pop();
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82