Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

FAT Simulation

Posted on 1997-05-21
6
1,503 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
convert char array to number in c 5 86
No module found pypyodbc, 3 33
How to properly read C++ console input into several variables with scanf 2 20
Outlook 13 41
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

828 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