[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

FAT Simulation

Posted on 1997-05-21
6
Medium Priority
?
1,537 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
[X]
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
  • 3
  • 3
6 Comments
 
LVL 3

Accepted Solution

by:
gaohong earned 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

649 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