Solved

FAT Simulation

Posted on 1997-05-21
6
1,494 Views
Last Modified: 2008-03-03
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;



}
0
Comment
Question by:stuntman
  • 3
  • 3
6 Comments
 
LVL 3

Accepted Solution

by:
gaohong earned 100 total points
ID: 1163604
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
 

Author Comment

by:stuntman
ID: 1163605
Hmm...  Where Exactly???  I tried removing the last 3 lines, but that didn't seem to be right....  :))

Thanks

Robert

0
 
LVL 3

Expert Comment

by:gaohong
ID: 1163606
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:stuntman
ID: 1163607
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
 
LVL 3

Expert Comment

by:gaohong
ID: 1163608
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
 

Author Comment

by:stuntman
ID: 1163609
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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now