please check this script

//program to add to 8 bit binory numbers(2)
#include<iostream>
using namespace std;
int binory(int ,int *p);
int addbinory(int *,int *, int *);
//this is c++ program so if u want convert it c just change your cout to printf and cin to scanf and do nessassory changes
//this is a function to convert decimal to binary equavalent
int binory(int num, int *p)
{
int i=7;
cout<<"the number is : "<<num<<"\n";
while(num!=0)
{
int val=num%2;
cout<<"the remender is : "<<val<<"\n";
num=num/2;
p[i]=val;
i--;
cout<<"the p[i] value is : "<<p[i]<<"\n";
}
cout<<"\n the array values \n";
for(int k=0;k<8;k++)
{
//cout<<"\n the array values \n";
cout<<p[k]<<" ";
}
}
//This block will add two converted binory numbers
int addbinory(int *p,int *q,int *l)
{
int cf=0,i;
for(i=7;i>=0;i--)
{
l[i]=p[i]+q[i]+cf;
cf=0;
if(l[i]==2)
{
l[i]=0;
cf=1;
}
if(l[i]==3)
{
l[i]=1;
cf=1;
}
}
l[i]=cf;
cout<<"\n\n AFTER ADDING TWO BINORY NUMBERS IS \n\n";
for(i=0;i<8;i++)
{
cout<<l[i]<<" ";
}
cout<<"\n COmpleted\n";
}

int main()
{
int num,a[]={0,0,0,0,0,0,0,0},b[]={0,0,0,0,0,0,0,0},c[]={0,0,0,0,0,0,0,0};
cout<<"\nentre the number\n";
cin>>num;
binory(num,&a[0]);
cout<<"\nEnter second number\n";
cin>>num;
binory(num,&b[0]);
addbinory(&a[0],&b[0],&c[0]);
return 0;
}

Edited 6 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

Hi,

I checked this code because it was offered, but mainly because I am still a beginner at C programming (either I am in the wrong forum or you are).

I have only just signed up with DaniWeb, so I still don't know how to exactly post, another reason for the reply. Your code ran fine on my machine (CodeBlocks under Ubuntu). I did re-write some of the formatting of the code, I found it hard to read/follow without indentation (4 spaces and not using the tab).

I will post the re-written code, for my own practise of posting code and to see if it comes out with the indentations.

On my first pass through I did change all the \n to << endl because I thought that was the C++ thing to do, I am not a C++ programmer, but thought to google what the difference was between these. It seems that using endl flushes the output, but using \n doesn't. So I used endl at the end of a string and \n at the beginning. Maybe somebody with more experience could comment on this, but that might need a new thread.

I assume you must have compiled the code and knew it ran okay, but was asking for feedback on the code structure/style.

Again, I am a newbie myself, so please feel free to take or leave my comments.

Regards

Martin.

//program to add to 8 bit binary numbers(2)

#include<iostream>

using namespace std;

int binary(int , int *);
int addbinary(int *, int *, int *);

//this is c++ program so if u want convert it c just change your cout to printf and cin to scanf and do nessassory changes
//this is a function to convert decimal to binary equavalent

int binary(int num, int *p)
{
    int i = 7;
    cout << "The number is : " << num << endl;
    while(num != 0)
    {
        int val = num % 2;
        cout << "The remainder is : " << val << endl;
        num = num / 2;
        p[i] = val;
        i--;
        cout << "The p[i] value is : " << p[i] << endl;
    }
    cout << "\nThe array values" << endl;
    for(int k = 0; k < 8; k++)
    {
//cout<<"\n the array values \n";
    cout << p[k] << " ";
    }
}

//This block will add two converted binary numbers
    int addbinary(int *p, int *q, int *l)
    {
        int cf = 0, i;
        for(i = 7; i >= 0; i--)
        {
            l[i] = p[i] + q[i] + cf;
            cf = 0;
            if(l[i] == 2)
            {
                l[i] = 0;
                cf = 1;
            }
        if(l[i] == 3)
        {
            l[i] = 1;
            cf = 1;
        }
        }
    l[i] = cf;

    cout << "\n\nAFTER ADDING TWO binary NUMBERS IS\n" << endl;

    for(i = 0; i < 8; i++)
    {
        cout << l[i] << " ";
    }
    cout << "\nCompleted" << endl;
    }

    int main()
    {
        int num , a[] = {0,0,0,0,0,0,0,0}, b[] = {0,0,0,0,0,0,0,0}, c[] = {0,0,0,0,0,0,0,0};
        cout << "\nEnter the first number" << endl;
        cin >> num;
        binary(num, &a[0]);
        cout << "\nEnter second number" << endl;
        cin >> num;
        binary(num, &b[0]);
        addbinary(&a[0], &b[0], &c[0]);
        return 0;
    }
This article has been dead for over six months. Start a new discussion instead.