2

Here's my code. Whenever I run the program, I expect that the cout statement in the for loop in the function Binary() to execute. But whenever I input something, it gives me 0

#include<iostream>
#include<math.h>
using namespace std;
long int Binary(int x){
    int temp=0,res=0;
    for(int i=x;i<0;i--){
        temp++;
        res=res+ pow(10,temp)*(i%2);
        i=i/2;
        cout<<i<<"  "<<res<<endl;
    }
    return res;
} 
int main(){
    int x; cin>>x;
    cout<<endl<<Binary(x);
    return 0;
}
  • 2
    Look at `for(int i=x;i<0;i--)` very carefully. Is `i < 0` going to be true when you first enter the function? – NathanOliver Jun 29 '20 at 17:57
  • 1
    Try input `-42`, and you will get output other than 0. – MikeCAT Jun 29 '20 at 17:57
  • 1
    You shouldn't use `pow`, as it is a floating point function that may give inaccurate results. Just use a (static) array of powers of 10. Also, why would you want to constantly repeat calculating `pow`, for example, `pow(10,2)`, when you know it is 100 and can't be anything else? That's why a table should be used, and second entry in the table would be 100 (the entry number is the exponent value). – PaulMcKenzie Jun 29 '20 at 18:03
  • https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice#1452738 – Jesper Juhl Jun 29 '20 at 18:06
  • 1
    Rather than using a table of powers of 10, you could have a variable that gets multiplied by 10 in each iteration. – Thomas Matthews Jun 29 '20 at 18:28
  • @PaulMcKenzie That is a nice suggestion, but it's hardly essential. All integers can be represented by `double` up to `2**53`, which likely to be as large as OP wants and, depending on the platform, may even be larger than `int` can hold anyway. For all possible values that would give a result in range, the difference in computation time will be trillions of times smaller than the time for you to type your comment. – Arthur Tacca Jun 29 '20 at 18:38

2 Answers2

3

I think the condition in your for loop is the opposite way round than your expect it. You seem to want it to continue looping until i<0. But it will actually loop while i<0. The condition is already not true when you enter the loop, so it will finish immediately without doing any iterations.

Arthur Tacca
  • 8,833
  • 2
  • 31
  • 49
2

You just have to change the loop i check.

#include<iostream>
#include<math.h>
using namespace std;
long int Binary(int x){
    int temp=0,res=0;
    for(int i=x;i>0;i--){
        temp++;
        res=res+ pow(10,temp)*(i%2);
        i=i/2;
        cout<<i<<"  "<<res<<endl;
    }
    return res;
} 
int main(){
    int x; cin>>x;
    cout<<endl<<Binary(x);
    return 0;
}

Check the live version here http://cpp.sh/95z74

CodingSomething
  • 449
  • 3
  • 10
  • Nevertheless the program has no syntactic error and seems to be working fine without getting weird, but has logical errors in binary conversion and produces incorrect output. – Rohan Bari Jun 29 '20 at 18:16
  • 1
    @LegendrarySaiyan If you are happy with an answer then please show that by accepting the answer you found most helpful. That is likely to help stop discussion. – Arthur Tacca Jun 29 '20 at 18:38