Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to make binary cnvert calculator. But I can't make it. Where it the bugs? Please help me to fix it.

What I have tried:

#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{
    char binar[65];
    int decimal;
    int i,n,power,temp;

    while(1) {

    printf("Enter the decimal number : ");
    scanf("%d",&decimal);

    for(n = 0;pow(2,n) < decimal;n++){
        temp = pow(2,n);
    }
    
    n -= 1;
    if(temp < decimal) {
        binar[0] = 1;
    }
    int j;

    for(i = n - 1 ,j = 1;j <= 7;i--,j++) {
        
        if(temp + pow(2,i) < decimal) {
            binar[j] = 1;
            temp = temp + pow(2,i);
        }

        else{
            binar[j] = 0;
        }


    }

   
    for(i = 0;i < n;i++) {
        printf("%d",binar[i]);
    }
    printf("\n");

}

    return 0;
   
}
Posted
Updated 13-Jul-22 23:02pm
Comments
0x01AA 13-Jul-22 15:02pm    
Describe what do you expect from the code and what you get...
Btw. do you know what you are doing? This e.g. what should this part of the code
for(n = 0;pow(2,n) < decimal;n++){
temp = pow(2,n);
}
why here a for is needed?
Lara Thomas 13-Jul-22 15:16pm    
95 --> 101111 c/a 1011111
10 --> 100 c/a 1010
I used this to get the value of n and temp.
for(n = 0;pow(2,n) < decimal;n++){
temp = pow(2,n);
}
0x01AA 13-Jul-22 15:28pm    
But for that no foris needed, think about it or explain me where I'm wrong.
Lara Thomas 13-Jul-22 15:42pm    
so how will you define the value of n?
jeron1 13-Jul-22 15:53pm    
'temp' gets overwritten every iteration through the for loop.

You can do the conversion without using the pow function, instead shift the number right each interation.

See C program to convert decimal to binary number system using bitwise operator[^]
 
Share this answer
 
C
for (n = 0; pow(2, n) < decimal; n++) {
	temp = (int)pow(2, n);
}
n -= 1;

If the number of bits needed should be calculated here, then subtracting one here would not be correct. With negative numbers, n=0 would not work.Since negative numbers are binary based on the two's complement, this approach would not lead to a solution. The number of bits can be calculated without repeatedly calculating pow in a loop, or is the function log not allowed?

C
char binar[65];

Why is the array so big? Are there computers where an int has so many bits?
If you don't know, I would recommend allocating the memory in the necessary size.
C
if (temp < decimal) {
	binar[0] = 1;
}

The query is always fulfilled with some certainty if decimal is not 0. In the most significant position binary[0] there is then (of course) a 1.

After that it just goes wrong, as you would certainly notice in the debugger.
C
for (i = n - 1, j = 1; j <= 7; i--, j++) 
   {
   if (temp + pow(2, i) < decimal) {
      binar[j] = 1;
      temp = temp + (int)pow(2, i);
   }
   else {
      binar[j] = 0;
   }
}

The loop continues even with i=-1 ...

If you simply shifted the decimal value and masked out the lowest digit, you could save yourself all that. And the result would be easier and also more correct.
 
Share this answer
 
v5
Quote:
Where it the bugs? Please help me to fix it.

There about 1 toll to help you discover what is going on by yourself, the debugger.
this simple code get input digits 1 by 1:
C++
int Num= 100;
while (Num > 0) {
    int Digit= Num % 10; // get unit digit here
    Num= Num/ 10;
    printf("%i", Digit);
}
printf("\n");

Guess what happen if you replace 10 by 2.

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
 
v2

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