Click here to Skip to main content
15,902,918 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Why this code is taking infinite input for no reason ? I was trying to solve codeforces problem but don't know why this code is taking infinite input. How to debug this kind of error and when it occurs even if you are not using any infinite loop ?? Please tell what to do.
Input =
3
6
0 3 0 0 1 3
10
0 0 0 1 0 5 0 0 0 2
3
0 0 0

Output =
1 1 0 1 1 1 
0 1 1 1 1 1 0 0 1 1 
0 0 0 


What I have tried:

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	int t=1;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		int a[n];
		string s = "";
		for(int i=0;i<n;i++) cin>>a[i];
		for(int i=n-1;i>=0;i--){
		    if(a[i] >= (i+1)){
		        int k = a[i];
		        while(k--)
		            s+='1';
		        i = i-k+1;
		    }
		    else if(a[i] == 0)  
		        s+='0';
		    else{
		        int k = a[i];
		        while(k--)
		            s+= '1';
		        i = i-k+1;
		    }
		}
		for(int i=n-1;i>=0;i--)
		    cout<<s[i]<<" ";
		cout<<"\n";
	}
	return 0;
}
Posted
Updated 25-Mar-21 0:42am
v3
Comments
Rick York 25-Mar-21 4:33am    
1. what does "taking infinite input" mean?
2. there is ALWAYS a reason your program does something whether you understand it or not.

I would guess it's because you are changing the loop index in the logic and in the loop incrementer :
C++
 for(int i=n-1;i>=0;i--)   <===============
 {
     if(a[i] >= (i+1))
     {
         int k = a[i];
         while(k--)
             s+='1';
         i = i-k+1;    <===============
     }
     else if(a[i] == 0)
         s+='0';
     else
     {
         int k = a[i];
         while(k--)
             s+= '1';
         i = i-k+1;    <================
     }
}
When you do that kind of thing the results can be unpredictable. A debugger can help you see what is going on.

In lieu of a debugger, a few print statements can help show you what's going on. I noticed you have a redundant conditional check in the code so I will remove that. Here is a revised loop with printing statements.
C++
for(int i = n - 1; i >= 0; --i )
{
    int k = a[i];

    cout << "i is " << i << " and k is " << k << std::endln;

    if( k == 0 )
    {
        s += '0';
    }
    else
    {
        while( k-- )
            s += '1';
        i = i - k + 1;
    }

    cout << "i is now " << i << " and s is " << s << std::endln;
}
My apologies if the cout statements aren't quite right. I never use them but I see that you do so I did this time.
 
Share this answer
 
v3
Comments
Priyanka Somani 25-Mar-21 5:06am    
When I comment the incremental line in for loop. Then program runs and gives output but wrong. Thus is means that it is bcz of these incremental or decrement in loops Can you tell a good debugger as I don't know anything about debugger
Rick York 25-Mar-21 13:27pm    
I use Visual Studio and it is quite good. I don't know about any other debuggers. If you can't find one then a few printf statements can help. I will revise my solution to show you what that might look like.
It is a good idea to give url in codeforces so we have a chance to ubderstand what you code is supposed to do.
Quote:
Compiler taking infinite input

Wrong description. The compiler does its job correctly.
Quote:
Why this code is taking infinite input for no reason ?

Be precise:
- 'infinite input' means that yourprogram infinitely asking for user inputs and problem is around the 'cin'
- 'program never ends' means what is said.
- 'program never ends processing data' means what is said.
Quote:
Why this code is taking infinite input for no reason ?

No reason that you see. As far as I see, there is no problem with inputs.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v4
Your code compiles fine in a finite time.
Probably you meant that its execution time is infinite. In such a case you should, at least, tell us what are the requirements, the input values and the expected output ones.
 
Share this answer
 
Comments
Priyanka Somani 25-Mar-21 4:35am    
Updated now
CPallini 25-Mar-21 4:54am    
You did not report the requirements (that is 'what your code is supposed to do?').
The first step should be that you clean up all of the i-loops to another variable names.
At second you MUST avoid doing incremental math with some i in your loops. It is the control element of your loop.
I guess you need to redesign the algorithm or replace
C++
​i = i-k+1;
with another variable or other solution.

So the code isnt not only bad style but full of bugs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900