Storing 1 as 01 in array

Hey guys, so as the title suggests I am looking for a way to store 1 as 01 or any number of 0 as prefix.
Is this possible for an int array?

Thanks.
You seem to have a misconception about how numbers are stored. 1, 01 (and 000001 etc.) are just different representations of the same number.
You should explain what you're really trying to do.
I was basically trying to write gray code for binary digits. I realized that there is a certain pattern this follows where the permutations for binary length 1(i.e. 0 and 1) will be first reversed(becomes 1 and 0). All 4(0,1,1,0) of these are stored in an array, and the first half of the elements i.e.( 0 and 1) have to added 0(becomes 00 and 01) and second half has to be added 1(becomes 11 and 10).
This same recursive process has to be followed for any binary length.

I changed my approach and used a string array instead, however I seem to be getting a segmentation fault.
Please suggest changes in the code.

//from 2 reverse it, add it, add 0 to 1st half and 1 to 2nd half. do the same for 4, do this till n!

#include <iostream>
#include <string>
#include <math.h>
using namespace std;

string a,b;
string c="0";
string d="1";

int base( int n, string prefix, int& pos,string answer[],int& count ){ //function to get permutation for length 2 binary
if( n==1 )
{
if(pos==-1){

a=prefix + c;
answer[count]=a;
count++;


b=prefix + d;
answer[count]=b;
count++;
pos=0;
}
else{
a=prefix + d;
answer[count]=a;
count++;


b=prefix + c;
answer[count]=b;
count++;
pos=0;;
}
}
else{

string prefix1 = prefix + '0';
base( n-1, prefix1,pos,answer,count);

string prefix2 = prefix + '1';
base( n-1, prefix2,pos,answer,count);
}
}

int ansrev(string dig[],int& count,int& position,int length){
if(position>=0)
{
dig[count]=dig[position];
count++; position--;
ansrev(dig,count,position,length);
}
else{
int i=0;
for(i;i<((count+1)/2)-1;i++)//change 1 to 01 in array, and change pos to the last element value!
{ dig[i].append("0",0,1);}
for(int j=i;j<length;j++)
{ dig[j].append("1",0,1);}

position=count;
}
}

int main(){
int n;
cin >> n;//length n binary gray code required
int pos=-1; // acts as a flag
int count=0; //counts the number of elements that are in array
int length=2*pow(2,n); //length of array would twice the total permutations according to the algorithm
string answer[length];

base( 2, "",pos,answer,count ); //function to get permutation for length 2 binary

int position=1;//the array elements when n=2 end at this position in array

while(position<length)
{
ansrev(answer,count,position,length); //reversing and adding 0 and 1 to the elements in array
}

for(int i=0;i<length;i++)
{
cout<<answer[i]<<endl; //printing the final output required.
}

return 0;
}


Would it work to store 1 as a prefix and leave off leading zeros? For example: 11b, 10b, 1b, 0b?

Have you considered 11b as 3 and 10b as 2? 0, 1, 2, and 3 all fit nicely in an int. :)
I fixed the problem, used a string array and used the insert() function to add 0 and 1 to beginning of every string.
Thanks for the suggestion though :)
Topic archived. No new replies allowed.