i wrote this code yesterday and i traced it myself but the problem is the array doesn't store any number in it and the output was like this :

//convert from decimal to binary 
#include<iostream>
using namespace std ;
void main(){
int n,c=0,*pointer,temp=0,r,i,y  ;
cout<<"Enter number\n";
cin>>n;
y=n ;
for(;;){
    if(n==0)
    {break ;}
    n=n/2 ;
    c++ ;
}
pointer=new int[c] ;
for(i=0;i<c;i++){
    temp=y ;
    y=y/2 ;
    r=temp%2 ;
    pointer[i]=r ;
}
cout<<"the binary number is :";
for(i=c-1;i<0;i--)
    cout<<pointer[i];
    cout<<endl;
system("pause");
}
Attachments Capture2.PNG 6.75 KB

OK well, ignoring all of the usual niggly things in the code... Like the fact that main should return int and not void. And the fact that it makes programs much easier to understand if you have meaningful names for variables. etc etc. ad nauseum.

The most obvious logical error that I can see, which would be most problematic is the break condition for the for loop at line 23 of your program.

You are iterating backwards through your array of characters from c-1 to 0, therefore the break condition should be c>=0 not c<0. Using c<0 as the break condition would cause the for loop to exit immediately.

Once this is fixed, you'll see output after entering your number. But you will get no output if you enter 0 as the decimal number! I'll leave you to fix that!

Edited 1 Year Ago by JasonHippy

speaking about your code, just correct line 23 => i>=0 not i<0

and for the future:
this is slow:
y=y/2 ;
r=temp%2 ;

temp%2 is the same as temp - temp/2 * 2 so you can do like in the below:

while (temp = y)
        {
            y /= 2;
            *(pointer + i++) = temp - y * 2;
        }




    //this how can the program look like:
#include<iostream>
#define MAX 32
using namespace std;
void main(){
    int temp, i=0, y;
    int pointer[MAX];
    cout << "Enter number\n"; cin >> y;


    while (temp = y){
        y /= 2;
        *(pointer + i++) = temp - y * 2;
    }


    cout << "the binary number is :";
        for (; i--;)
        cout << pointer[i];
    cout << endl;
    system("pause");

Here is a nifty way of doing it:

int* DecToBin(int n, int* output)
{
    if(n == 0) return &(*output = 0);
    if(n == 1) return &(*output = 1);
    output = DecToBin(n / 2, output);
    return &(*(++output) = n % 2);
}

in the output field put a array of int's (int x[10] for example). Of course you have to init your table with another values than 0 and 1 to know the length of the data.

This version doesn't need a array from outside, instead it uses a vector and so you know how long the response is (the length of the array). Also here is the entire program:

#include <string>
#include <vector>
#include <iostream>

std::vector<int> DecToBin(int n)
{
    std::vector<int> output;
    if(n == 0) { output.push_back(0); return output; }
    if(n == 1) { output.push_back(1); return output; }
    output.swap(DecToBin(n / 2));
    output.push_back(n % 2); return output;
}

int main(int argc, char* argv[])
{
    std::vector<int> x = DecToBin(10);
    for(int i = 0; i < x.size(); ++i)
    {
        std::cout << x[i];
    }
    std::cin.get();
    return 0;
}
This article has been dead for over six months. Start a new discussion instead.