In reference to the link below which is my same question, but the expert answer
ID: 3690199 • Letter: I
Question
In reference to the link below which is my same question, but the expert answer does not answer the instructions stated. Can you rewrite the code so that the proper amount of arrays are used, and use the void functions being asked for, and get the program to work only using:
#include "stdafx.h"
#include
using namespace std;
http://www.chegg.com/homework-help/questions-and-answers/write-c-program-converts-numbers-decimal-binary-binary-decimal-program-must-contain-follow-q8520003?adobe_reloaded=true
Explanation / Answer
Oh never mind I could edit this , heres the correct code (I have rectified some of the mistakes since im using dev-cpp it might initialize automatically some variables anyways dont worry i dont think u will get anymore errors if u get u can ask on posting comment) :
#include<iostream>
using namespace std;
void decToBin();
int binToDec(int []);
void decToTcBin();
void tcBinToDec();
int pow(int,int);
int main()
{
int n,op;
while(n!=1)
{
cout<<" Welcome to the Number Conversion Program! ---------------------------------------- (1) Convert unsigned binary to unsigned decimal (2) Convert unsigned decimal to unsigned binary (3) Convert two's complement binary to decimal. (4) Convert decimal to two's complement binary (5) Exit the program Your choice?> ";
cin>>op;
switch(op)
{
case 1:
int dec,binNum[8];
cout<<"Enter binary value... ";
for(int i=7;i>=0;i--)
{
cin>>binNum[i];
}
dec=binToDec(binNum);
cout<<dec;
break;
case 2:
decToBin();
break;
case 3:
tcBinToDec();
break;
case 4:
decToTcBin();
break;
case 5:
n=1;
break;
}
}
return 0;
}
int pow(int n,int i)
{
int res=1,j;
if(i==0)
{
return 1;
}
for(j=1;j<=i;j++)
{
res=res*n;
}
return res;
}
int binToDec(int binNum[])
{
int i;
static int dec;
dec=0;
cout<<"The dec value for given binary value is : ";
for (int i=0;i<8;i++)
{
dec=dec+(binNum[i]*pow(2,i));
}
return dec;
}
void decToBin( )
{
int dec;
cout<<"Enter a decimal number: ";
cin>>dec;
int rem,i=1;
int k=7,temp[8],n=0;
static int binNum[8];
binNum[0]=0;
int m=dec;
do
{
rem=dec%2;
temp[k]= rem;
dec=dec/2;
n++;
k--;
}while(dec>0);
int j=k;
k++;
for(i=0;i<=j;i++)
{
binNum[i]=0;
}
for(i=0;i<n;i++)
{
binNum[k]=temp[k];
k++;
}
cout<<"The binary value of "<<m<<" is: ";
for(i=0;i<8;i++)
{
cout<<binNum[i]<<" ";
}
}
void decToTcBin()
{
char str[10];
int k,i;
cout<<"Enter decimal value: ";
cin>>str;
k=0;
int res=0;
for(i=1;str[i]!='';i++)
{
res=res*10+(int(str[i])-48);
}
int dec;
dec=res;
int rem;
i=1;
int temp[8],n=0;
k=7;
static int binNum[8];
binNum[0]=0;
int m=dec;
do
{
rem=dec%2;
temp[k]= rem;
dec=dec/2;
n++;
k--;
}while(dec>0);
int j=k;
k++;
for(i=0;i<=j;i++)
{
binNum[i]=0;
}
for(i=0;i<n;i++)
{
binNum[k]=temp[k];
k++;
}
for(i=0;i<8;i++)
{
if(binNum[i]==1)
{
binNum[i]=0;
}
else
binNum[i]=1;
}
int carry=0;
binNum[7]=binNum[7]+1;
for(i=7;i>=0;i--)
{
if(carry>0)
{
binNum[i]=binNum[i]+carry;
carry=0;
}
if(binNum[i]==2)
{
binNum[i]=0;
carry=1;
}
}
cout<<" ";
cout<<"Decimal to twos complement value: ";
for(i=0;i<8;i++)
{
cout<<binNum[i]<<" ";
}
}
void tcBinToDec()
{
int binNum[8],i,n;
cout<<"Enter binary twos complement value (with spaces): ";
for( i=7;i>=0;i--)
{
cin>>binNum[i];
}
if(binNum[7]==1)
{
n=1;
}
for(i=0;i<8;i++)
{
if(binNum[i]==1)
{
binNum[i]=0;
}
else
binNum[i]=1;
}
int carry=0;
binNum[0]=binNum[0]+1;
for(i=0;i<8;i++)
{
if(carry>0)
{
binNum[i]=binNum[i]+carry;
carry=0;
}
if(binNum[i]==2)
{
binNum[i]=0;
carry=1;
}
}
if(n==1)
cout<<"-"<<binToDec(binNum);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.