c++ Logic issues, can't get correct output?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: c++ Logic issues, can't get correct output?

  1. #1
    Join Date
    Nov 2006
    Posts
    4

    c++ Logic issues, can't get correct output?

    Hello,

    I am trying to get this program to work correctly. My main issue is with the logic. I can't figure out how to get the desired output. I have it attached becuase it wouldnt line up.

    Here is the file thats being read in.

    4 is the amount of time a process can be in memory. ABCD are process IDEN.
    20,15, 5 are the memory it needs. 6,7,8,2 is how long it needs to run in memory.

    4
    A 20 6
    B 20 7
    C 15 8
    D 5 2

    The attched file explains the read ins a little more.

    Thanks for helping.

    Code:
    #include <string>
    #include <iostream>
    #include <fstream>
    #include <queue>
    
    using namespace std;
    
    #define maxmem 50
    
    struct process
    {
      int time[30];
      int memory[30];
      char process1[30];
      bool done[30];
    };
    
    
    //read the processs in
    void memin(char process, char[], int position, int large);
    
    //read it the process out
    void memout(char [], int memory);
    
    //its done kick out
    void removemem(char memarray[], char memchar);
    
    //put process in biggest free space avail
    int worstfit(char[], int& position);
    
    //get the time left for the process.
    int dispatch(process&, queue<int>, int);
    
    
    
    int main()
    {
      queue<int> jobs;
      int availmem;
      char dot ='.';
      char memarray[50];
      char memchar, reqmem;
      bool done=true;
      bool condition;
      int quantum, totaltime = 0, totalprocess = 0;
      int temp, currentp, time = 0;
      int position = 0;
      int timeleft, l, maxproc = 0, avgmem = 0, usedmem = 0;
      float tp, maxUtil = 0.0;
      string filename;
      
      ifstream inp;
      
      //Uses array of structures to read the elements of the file
      
      
      process myprocess;
      
      for (int xx = 0; xx < maxmem; xx++)
        {
          memarray[xx] = '.';
        }
      
      
      //Opens the file  
      cout << "Enter dat file name: ";
      cin >> filename;
      inp.open(filename.c_str());
      int j = 0, i = 0;
      
      if(!inp)
        {   cout << "Can't open file" << endl;
        exit(1);
        }
      inp >> quantum;
      while(!inp.eof())
        { 
          inp >> myprocess.process1[j];
          inp >> myprocess.memory[j];
          inp >> myprocess.time[j];
          
          j++;
          
        }
      totalprocess = j - 1;
      
      
      inp.close();
      temp = totalprocess;
      
      while (temp >= 0)
        {
          totaltime = myprocess.time[temp] + totaltime;
          temp--;
        }
      
      cout << "    0....5....0....5....0....5....0....5....0....5...9" << endl; 
      memout(memarray, maxmem);
      cout << endl;
      
      availmem = 0;
      int p = 0;
      
      for (int x = 0; x < 29; x++)
        myprocess.done[x] = false;
      
      do
        {   time++; condition = false;
        
        
        // finds largest available memory space and returns it's starting position
        availmem = worstfit(memarray, position);
        
        // returns the process character name
        memchar = myprocess.process1[p];
        // returns the amount of memory required for process
        reqmem = myprocess.memory[p];
        
        // checks to see if enough room in memory and if so then adds to memory if hasn't been already.
        if (availmem >= reqmem && myprocess.done[p] == false)
          {
    	memin(memchar, memarray, position, reqmem);
    	myprocess.done[p] = true;
    	condition = true;
                 jobs.push(p);
          }
        
        
        
        // dispatch removes 1 click from the running time
        
        
        temp = jobs.front();
        timeleft = dispatch(myprocess, jobs, temp);
        
        
        
        // move to end of queue if process hasn't completed
        if(time%quantum == 0 && timeleft != 0)
          {
    	currentp = jobs.front();
    	jobs.pop();
    	jobs.push(currentp);
    	
          }
        
        // if process is complete then removes from memory
        if (timeleft == 0)
          {  
    	memchar = myprocess.process1[temp];
    	removemem(memarray, memchar);
    	condition = true;
    	myprocess.done[temp] = true;
    	jobs.pop();
    	
    	
          }
        
        if (maxproc < jobs.size())
          maxproc = jobs.size();
        
        // gathering stats
        availmem = worstfit(memarray, position);
        usedmem = maxmem - availmem;
       if (usedmem > maxUtil)
         maxUtil = usedmem;
       avgmem = (usedmem + avgmem);
       
       // prints memory if a change occurs
       //if (condition)
       //{
       cout << time << ":  ";
       memout(memarray, maxmem);
       cout << endl;
       //}
       
       
       p++;
       if(p >= totalprocess)
         p = 0;
       //cin >> l;
       
       
        } while(jobs.size() != 0 && availmem != 50);
      
      avgmem = avgmem/time;
      maxUtil = (maxUtil * 100.0)/maxmem ;
      tp = (totalprocess*100.0)/time;
      
      cout << "Totaltime should take: " << totaltime  << "  largest availmem currently: " << availmem << " position: " << position << endl;
      cout << "Max Proc Count: " << maxproc << " Avg Mem Utilization: " << avgmem << endl;
      cout << "Max MemUtilization: " << maxUtil << " Throughput: " << tp << endl;
      
      
      
      
      return 0;
    } // end of main
    
    
    int worstfit(char memarray[], int& position)
    {
      int large = 0;
      int counter = 0;
      
      
      
      for (int i = 0; i < 50; i++)
        {
          
          if(memarray[i] == '.')
    	counter++;
          else counter = 0;
          
          if(memarray[i+1] != '.')
    	{if (counter >= large)
    	  {
    	    large = counter; 
    	    position = i - counter + 1;
    	  }
    	counter = 0;
    	}
        }
      
      return large;
    }
    
    
    void memin(char theprocess, char memarray[50], int position, int reqmem)
    {
      for (int j = position; j <= reqmem + position - 1; j++)
        memarray[j] = theprocess;
    }
    
    void memout(char memarray[50], int memory)
    {
      
      for(int i = 0; i < memory; i++)
        cout << memarray[i];
      //cout << endl;
      
    }
    
    void removemem(char memarray[], char memchar)
    {
      for (int j = 0; j < maxmem; j++)
        if (memarray[j] == memchar)
          memarray[j] = '.';
    }
    
    int dispatch(process &myprocess, queue<int> jobs, int h)
    {
       int timeleft;
    
     myprocess.time[h] = myprocess.time[h] -1 ;
     timeleft = myprocess.time[h];
     
     return timeleft;
    
    }
    Attached Files Attached Files
    Last edited by meatner; 11-02-2006 at 07:46 PM.

  2. #2
    Join Date
    Nov 2006
    Posts
    4
    any one?

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    I dont see a mistake, but on the supposedly correct one you have spaces to line stuff up and you have two line # 11's which does not make a bit of sense to me. It also looks like you are off by one, you do not have a line zero and therefore are off by one timestep relatively (?)

  4. #4
    Join Date
    Nov 2006
    Posts
    4
    yeah, that extra line is a typo...sorry.

    but basically, I am trying to get to that output with what I have.

    I added after this an else:
    if (availmem >= reqmem && myprocess.done[p] == false)

    else
    P++;

    This gets me the desired output(kinda). It reads in way too many B's in the correct spot. and d's are in the correct spot. I am short 2 lines of D's, and 2 extra A's.

Similar Threads

  1. Replies: 1
    Last Post: 04-24-2008, 06:03 AM
  2. Replies: 0
    Last Post: 11-28-2005, 12:56 PM
  3. Capture DOS output after CreateProcess
    By davidsc in forum VB Classic
    Replies: 4
    Last Post: 11-03-2005, 08:21 AM
  4. Consolidation of Business and Database Logic
    By Trac Bannon in forum Enterprise
    Replies: 0
    Last Post: 04-30-2001, 03:57 PM
  5. Replies: 0
    Last Post: 09-07-2000, 09:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center