• Welcome to Valhalla Legends Archive.
 

Help needed.

Started by oasis, April 15, 2004, 11:47 PM

Previous topic - Next topic

oasis

I have to write a program for one of my classes and I'm kinda confused on what to do.

Here's the prog assignment:
==

A Gray Code is a sequence of binary numbers such that the numbers differ in exactly one bit. For example, a 2 bit Gray Code is 00, 01, 11, 10.

You are to write a program that:

1.   has two global constants representing the number of rows and columns of a two-dimensional array
2.   within main( ), creates an array using the global constants
3.   reads an integer length (which represents the length of the requested Gray Code) from a file c:\gray.dat
4.   within another function, fills the array with the Gray Code using array indexing notation, e.g., a. Each entry of the array will be either a 0 or a 1.
5.   within another function, writes your Gray Code to the file c:\gray.out using pointer notation, e.g., *aPtr, without using indices

The maximum number of columns for the array is 8; you need to determine the appropriate maximum number of rows.

There are a number of algorithms for calculating a Gray Code of a specific length.  You are to use the following one:
1.   a Gray Code of length 1 contains two numbers 0 and 1
2.   a Gray Code of length n + 1 can be constructed from one of length n  (call it GCn) by taking GCn and attaching a 0 at the end of each term in the sequence, then reversing the GCn and appending a 1 to each of the terms.

==

Any help would be greatly appreciated.

Adron

The assignment is crystal clear to me. What are your questions about it?

oasis

Hello Adron,

I'm pretty sure I got the first 2 parts done.  But I'm confused with parts 3,4, and 5.

Adron

More specifically, what about them confuses you?

oasis

I can't get it to work right, is there a way I can send you the .cpp file or attach it to a post so you can see it.

Adron

This should be short enough to paste in code tags in a post.

oasis


#include <iostream.h>
#include <fstream.h>
#include <math.h>


const int row=256; const int column=8;

void graycode(int[row][column], int);
void displayout(int[row][column], int);

int main()
{  
   int length, invalid=0;
   int array[row][column];

   //obtaining length from user through dat file
   ifstream inFile("Gray.dat", ios::in);
   inFile >> length;

   cout << length << endl << endl;
   if (!inFile)
   {
       cout << "Unable to open the file\n";
       return 1;
   }


   //takes user input and checks to see if data is a legal value.
   if ((length >= 0) && (length <= 8))
   {  
       graycode(array, length);

   }
   else
   {
       cout << "Data in Gray.dat is invalid!" << endl << "Enter a value "
       << "desired from 1 to 8 bits long and please try again. \n";
       invalid=1;
   }
   

   if(invalid == 0)
   {
       displayout(array, length);

   }


   return 0;
}



void graycode(int array[row][column], int length)
{  
   //array and initial setup for gray code
   array[0][0] = 0;
   array[1][0] = 1;

   if(length > 1)
   {
       int lengthtemp =2;
       while(lengthtemp <= length)
       {
           int count =0;

           for(int x=0; x < pow(2,(lengthtemp-1)); x++)
           {
               array[x][lengthtemp-1]=0;
           }

            for(int k= (int)pow(2,(lengthtemp-1)); k < (pow(2,lengthtemp));k++)
            {
                array[k][(lengthtemp-1)]=1;
            }
 
             
 
            for(int p= (int)(pow(2,(lengthtemp))-1); p>=pow(2,(lengthtemp-1)); p--)
            {
                for(int r=0; r<(lengthtemp-1);r++)
                {
                    array[p][r]=array[count][r];
                }
                    count++;
            }

           lengthtemp++;
       }
   }
}

void displayout(int array[row][column], int length)
{
    ofstream outFile("Gray.dat", ios::out);
    int *aptr;
    for(int h=0; h<(pow(2,length)); h++)
    {
          aptr = array[h];
          cout << *aptr;
         
        for(int v=0; v<length; v++)
        {
            outFile << *aptr;
            aptr++;
        }
         
        outFile << endl;
    }    
}

oasis

I think my problem is with the last function, but I tried everything I can think of right now and can't get it to display the gray code properly.  So if you can look it over and help me figure out this problem that would be great.

Adron

Perhaps this would work?


void displayout(int array[row][column], int length)
{
   ofstream outFile("Gray.dat", ios::out);
   int *aptr = &array[0][0];
   for(int h=0; h<(pow(2,length)); h++)
   {
       for(int v=0; v<length; v++)
       {
           outFile << (*aptr?'1':'0');
           cout << (*aptr?'1':'0');
           aptr++;
       }
         
       outFile << endl;
       cout << endl;
   }    
}

oasis

#9
Yes that worked!

Thanks Adron!

Now I guess there is a problem calculating the graycode...:(

Adron

Actually, my code is flawed. For one, it needs to increase aptr by column - length after each line.

oasis

Well I got it working.  Here is my final code:

// GrayCode.cpp : Defines the entry point for the console application.
//

#include <iostream.h>
#include <fstream.h>
#include <math.h>

const int row=256; const int column=8;

void graycode(int[row][column], int);
void displayout(int[row][column], int);

int main()
{  
   int length, invalid=0;
   int array[row][column];

   //obtaining length from user through dat file
   ifstream inFile("C:\\gray.dat", ios::in);
   inFile >> length;

   cout << length << endl << endl;
   if (!inFile)
   {
       cout << "Unable to open the file\n";
       return 1;
   }


   //takes user input and checks to see if data is a legal value.
   if ((length >= 0) && (length <= 8))
   {  
       graycode(array, length);

   }
   else
   {
       cout << "Data in Gray.dat is invalid!" << endl << "Enter a value "
       << "desired from 1 to 8 bits long and please try again. \n";
       invalid=1;
   }
   

   if(invalid == 0)
   {
       displayout(array, length);

   }


   return 0;
}

void graycode(int array[row][column], int length)
{  
   //array and initial setup for gray code
   array[0][0] = 0;
   array[1][0] = 1;

   int i, j, t[10], g[10];

   //initialize g to all 0s and then t to keep track of the bit #s
   for (j=0; j <= length+1; j++)
   {
       g[j] = 0;
       t[j] = j+1;
   }

   //set the first index to be all 0s
   for( i = 0; i<1; i++ )
      for( j=0; j<length; j++ )
         array[i][j] = 0;

   j = 1;
   int l, k = 0;

   while (i < length+1)
   {
      //change the g array by 1 bit and keep track in the t array
      
      if( k > 0 )
         for (l=0, j=length; j; j--, l++)
            array[k][l] = g[j];
         i = t[0];
       g[i] = !g[i];
       t[0] = 1;
       t[i-1] = t[i];
       t[i] = i+1;
      k++;
   }
   
}

void displayout(int array[row][column], int length)
{
    ofstream outFile("C:\\gray.out", ios::out);
    int *aptr;
    for(int h=0; h<(pow(2,length)); h++)
    {
          aptr = array[h];
         
        for(int v=0; v<length; v++)
        {
            outFile << *aptr;
            aptr++;
        }
         
        outFile << endl;
    }    
}

j0k3r

Some notes... (damn, the one of the things I thought of was correct, other one I cant' find).

Anyways, it's very useful for those reading your code when you use meaningful variable names, not "k", "g", etc. Also, it's probably not very good form to name variables after keywords (array).

Good use of comments too (well, I thought so), but try adding a reason to your comments instead of just saying what it does.
QuoteAnyone attempting to generate random numbers by deterministic means is, of course, living in a state of sin
John Vo

oasis

I added more comments to it before I turned it in. 8)