- #1
uknowwho
- 25
- 0
We just started out C++ and I thought of making a conversion program from binary to gray and vice versa just for fun and to improve my skills
I was successful to make conversions of 4 bits
#include <iostream>
using namespace std;
int main() {
int Choice;
bool g0,g1,g2,g3,b0,b1,b2,b3;
cout<<"For Binary To Gray Code Enter 1." << endl <<"For Gray Code to Binary Enter 2." << endl;;
cin>>Choice;
if(Choice==1){
cout<<"Enter the Binary Bits." << endl;
cin>>b0>>b1>>b2>>b3;
cout<<"Orginal Binary Form: "<<b3 <<b2 <<b1 <<b0 << endl;
g3=b3;
g2=b3^b2;
g1=b2^b1;
g0=b1^b0;
cout<<"Converted Gray Code Form: "<<g3 <<g2 <<g1 <<g0 << endl;
}
else if(Choice==2){
cout<<"Enter The Gray Code Bits." << endl;
cout<<"g0: ";
cin>>g0;
cout<<"g1: ";
cin>>g1;
cout<<"g2: ";
cin>>g2;
cout<<"g3: ";
cin>>g3;
cout<<"Orignal Gray Code Form: "<<g3 <<g2 <<g1 <<g0 << endl;
b3=g3;
b2=b3^g2;
b1=b2^g1;
b0=b1^g0;
cout<<"Converted Binary Form: "<<b3 <<b2 <<b1 <<b0 << endl;
}
return 0;
}
but what if I want to store the number of bits entered by the user in real time is it possible?
I heard using vectors is more useful in c++ than arrays? Also how to store the bits in a string so that the user can enter the bits in one line without spaces.
We haven't studied loops and making functions as of yet so it would be great If you suggest simpler ideas
I was successful to make conversions of 4 bits
#include <iostream>
using namespace std;
int main() {
int Choice;
bool g0,g1,g2,g3,b0,b1,b2,b3;
cout<<"For Binary To Gray Code Enter 1." << endl <<"For Gray Code to Binary Enter 2." << endl;;
cin>>Choice;
if(Choice==1){
cout<<"Enter the Binary Bits." << endl;
cin>>b0>>b1>>b2>>b3;
cout<<"Orginal Binary Form: "<<b3 <<b2 <<b1 <<b0 << endl;
g3=b3;
g2=b3^b2;
g1=b2^b1;
g0=b1^b0;
cout<<"Converted Gray Code Form: "<<g3 <<g2 <<g1 <<g0 << endl;
}
else if(Choice==2){
cout<<"Enter The Gray Code Bits." << endl;
cout<<"g0: ";
cin>>g0;
cout<<"g1: ";
cin>>g1;
cout<<"g2: ";
cin>>g2;
cout<<"g3: ";
cin>>g3;
cout<<"Orignal Gray Code Form: "<<g3 <<g2 <<g1 <<g0 << endl;
b3=g3;
b2=b3^g2;
b1=b2^g1;
b0=b1^g0;
cout<<"Converted Binary Form: "<<b3 <<b2 <<b1 <<b0 << endl;
}
return 0;
}
but what if I want to store the number of bits entered by the user in real time is it possible?
I heard using vectors is more useful in c++ than arrays? Also how to store the bits in a string so that the user can enter the bits in one line without spaces.
We haven't studied loops and making functions as of yet so it would be great If you suggest simpler ideas