FAT Simulation

I am trying to write a simple FAT simulator using C++... I am a newbie to the language so forgive any obvious stupid approaches to my algorithm.... but I just can't get this to function.. I am using Microsoft Visual C++ Version 4.0

This assumes a 10 element FAT in an array  FAT[1]-FAT[10]...  I want to simulate assigning clusters to a file...

So I input the # of clusters...  The function should return the starting index of the first cluster IF there was room.. and say a -1 if there wasn't... I don't want any partial assignments.. so it all goes or nothing goes..  Sounds rough eh??  That is what I though!!  Here is what I have done so far....

#include <iostream.h>

#define max_fat 10

int fat[max_fat];
void print_it(void);
int assign_fat(int nc);
void initialize_fat(void);
void user(void);
void automatic(void);

void main(void)
{
      char choice;

      initialize_fat();

      cout<< "\n Do you want to enter your own values? Y" << flush;
      cin >> choice;
      if (choice=='y' || choice == 'Y')
      {
            user();
      }
      else
      {
            cout<< "\n using automatic values" <<endl;
            automatic();
      }

      print_it();
}


void print_it(void)
{
      int i, pause;
      for (i =1; i<=max_fat; i++)
      {
            cout<<"fat["<<i<<"] = "<< fat[i] << "\n" <<flush;
      }

      cin>> pause;

}

int assign_fat(int nc)
{
      int next, i, count, return_value;
      next = count= 0;
      i=1;

      //check if enough clusters are available
      for(i=1; i<= max_fat;i++)
      {
            if(fat[i]==0)
            {
                  count++;
            }
            if(count ==nc)
            {
                  break;
            }
      }
      //not enough clusters
      if(count!=nc)
      {
            cout<< "\ncan't assign_fat of " << nc << endl;
            return -1;
      }

      // ok there are enough clusters assign cluster file
      i=1;
      count =0;
      do
      {
            if(fat[i]==0)//start
            {
                  if(count==0)// this is the index value
                  {
                        return_value = i;
                  }

                  next=i;
                  do
                  {
                        next++;// next value that is '0'
                  }while (fat[next] !=0 );
                  count++;//assigned another cluster
                  fat[i] = next;

            }
            //assign nc worth of clusters, so stop assigning
            if(count==nc)
            {
                  break;
            }
            i++;
      }while(i<=max_fat);// not nescessary since we know that it will break out


      fat[i]=-1;// last cluster is end of file
      return return_value;


}

void initialize_fat(void)
{
      int i;
      for(i=1; i<= max_fat; i++)
      {
            fat[i]=0;
      }

      fat[1]= 2;
      fat[2]=-1;
      fat[5]=8;
      fat[8]=9;
      fat[9]=-1;
      
}

void user(void)
{
      int how_many, assigned;

      cout<<"\nenter -1 to quit";
      do
      {
            cout<< "\nhow many clusters to assign?"<< flush;
            cin>>how_many;
            if( how_many==-1)
            {
                  break;
            }
            assigned = assign_fat(how_many);

      }while(assigned != -1);

}

void automatic(void)
{
      int assign =0;
      assign = assign_fat(4);
      cout<<"\ncalled assign_fat(4) and got " <<assign << " returned" <<endl;

      assign =assign_fat(2);
      cout<<"\ncalled assign_fat(2) and got s" <<assign << " returned"
<<endl;



}
stuntmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gaohongCommented:
To get it function, just comment out the "break out" line in your posted code in function assign_fat(int) and it
is functioning.  This function contains bug as the return_value
is not initialized and several conditions will break execution
and falls to the return statement. Check your code throughly,
and consider each possible case, you are on your way.

hope this helps
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stuntmanAuthor Commented:
Hmm...  Where Exactly???  I tried removing the last 3 lines, but that didn't seem to be right....  :))

Thanks

Robert

0
gaohongCommented:
Hi, Look for //***** for explainations

int assign_fat(int nc)
                 {
                 int next, i, count, return_value;
                 next = count= 0;
                 i=1;

//*****   it should be for(i = 0; i < max_fax; i++)
//else you overflow array fat[]

                 //check if enough clusters are available
                 for(i=1; i<= max_fat;i++)
                 {
                 if(fat[i]==0)
                 {
                 count++;
                 }
                 if(count ==nc)
                 {
                 break;
                 }
                 }
                 //not enough clusters
                 if(count!=nc)
                 {
                 cout<< "\ncan't assign_fat of " << nc << endl;
                 return -1;
                 }

                 // ok there are enough clusters assign cluster file
               
//*****   it should be i = 0;
//else you overflow array fat[]
                 i=1;
                 count =0;
                 do
                 {
                 if(fat[i]==0)//start
                 {
                 if(count==0)// this is the index value
                 {
                 return_value = i;
                 }

                 next=i;
                 do
                 {
                 next++;// next value that is '0'
                 }while (fat[next] !=0 );
                 count++;//assigned another cluster
                 fat[i] = next;

                 }
                 //assign nc worth of clusters, so stop assigning
                 if(count==nc)
                 {
                 break;
                 }
                 i++;
                 }while(i<=max_fat);// not nescessary since we know that it will
                 break out
//*****   it should be while(i < max_fat)
//else you overflow array fat[]
//initially, when I pasted your code, there are compiler errors
//due to "break out" from comment line, so I said break out



                 fat[i]=-1;// last cluster is end of file
                 return return_value;


                 }
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

stuntmanAuthor Commented:
Now we are getting there!!!   However, the results are still questionalble!

Here is the data I am testing it with... let me know if you get the same results...

Initial values...

fat1=2   (next cluster in chain is #2)
fat2=-1  (end of chain)
fat3=0   (empty)
fat4=0
fat5=8
fat6=0
fat7=0
fat8=9
fat9=-1
fat10=0

This initializes fine... but let us assign say (4) to the fat...  it SHOULD return a 3 (first cluster assigned) and leave the fat like this...
fat1=2
fat2=-1
fat3=4
fat4=6
fat5=8
fat6=7
fat7=-1
fat8=9
fat9=-1
fat10=0

Right??  On paper anyhow :))

Then if I try to assign say a (2) it should return a -1 and NOT change the fat since only cluster fat[10] is available...

Let me know if this test data gives you the correct response..

Thanks again for your patience!!  You have already been a HUGE help!


0
gaohongCommented:
Hi, it does not make lots sense to verify the correctness of
your approach. It maybe better that you redesign the thing
like following and it will help you much more to learn C++.

class FatTable
    {
     unsigned int *fat;
     unsigned int tableLength;
     unsigned int available, first;
public:
     FatTable()  
        { available = tableLength = 10; first = 0;
          fat = new unsigned int [tableLength];
          for(int i = 0; i < tableLength; i++) fat[i] = 0;      
        }
     FatTable(int tl)
        { available = tableLength = tl; first = 0;
          fat = new unsigned int [tableLength];
          for(int i = 0; i < tableLength; i++) fat[i] = 0;    
        }
     ~FatTable() {delete [] fat;}
     int assignFat(unsigned int nc);
     int printFat();
     int userAssign(unsigned int);
     };  
         
int FatTable::assignFat(unsigned int nc)
     {
      if(available < nc)
         return -1;
      else
        {
         available -= nc;
         int i, n = 0;
         for(int i = first; i < tableLength && n < nc; i++)
              {
               if(fat[i] == 0) { n++; fat[i] = 1; }
              }
        }  
      return 1;
     }
//do other function yourself

hope this helpful
0
stuntmanAuthor Commented:
You are right... it is just so hard to start over... when you have invested so much time into what you already have...

Thanks for your Help!!!!

Robert

:)

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.