Last try!!

This is a discussion on Last try!! within the C++ Programming forums, part of the General Programming Boards category; Alright guys, thanks to everyone out there who has helped me with this project. Much props to ya'll... here is ...

  1. #1
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69

    Last try!!

    Alright guys, thanks to everyone out there who has helped me with this project. Much props to ya'll... here is my final attempt at this, hopefully you'll be able to tell what I'm trying to accomplish in the code by reading through it. This is the last time i'll post, so please please please someone take the time to read through it and make some suggestions for me!!! THanks everyone!!

    Code:
    /*
    
    Program copies processes into the ready list at correct times and runs in
    proper time and obtains all data required.  (May not be correct though?? :)  )
    
    Problem in code is putting process from "readyList" into "simulated CPU" at correct expirations of
    time quanta.
    
    CPU should be "simulated" the same as the (x < 800000) simulates 800 seconds of CPU processing.  
    
    
    */
    
    
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    
    
    using namespace std;
    
    
    
    long int preList[22][2];
    long int readyList[22][2];
    
    
    int timeQuanta[4] = {50, 100, 250, 500};
    int overheadArray[6] = {0, 5, 10, 15, 20, 25};
    
    
    int main()
    {
    
    	fstream inFile;
    	inFile.open("times.txt", ios::in);
    
    
    	int arrivalTime;
    	float timeToProcess;
    	int i, z= 0;
    	int h = 1;
    	long int x = 0;
    	int p, temp;
    	int newTime = 0;
    	int wait;
    
    	int quantumEntered, overheadEntered, timeArrived;
    	int averageWaitTime, averageTurnAroundTime = 0;
    	bool done = false;
    
    
    
    	cout<<"Please enter time quantum number 1 - 4"<<endl;
    	cin>>quantumEntered;
    	int quantum = timeQuanta[quantumEntered-1];
    
    
    	cout<<"Please enter overhead numer 1 - 6"<<endl;
    	cin>>overheadEntered;
    	int overhead = overheadArray[overheadEntered-1];
    
    	quantum = quantum + overhead;
    
    
    	//make sure file opens
    
    	if (!inFile)
    	{
    		cout<<"Error opening file."<<endl;
    		exit(1);
    	}
    
    		
    	//open file, get stuff in there
    	
    	while(!inFile.eof())
    	{	
    
    		
    		for(i=0; i<22; i++)
    		{	
    			//read from file into variables
    			inFile>>arrivalTime>>timeToProcess;
    
    			//put data into proper "preList" array
    			preList[i][0] = arrivalTime * 1000;
    			preList[i][1] = timeToProcess;
    			
    			//save current time in timeArrived
    			timeArrived = timeArrived + x;
    
    		}
    	}
    
    	cout<<endl<<endl;
    
    	
    
    	while (x < 800000)
    	{
    		
    		//set x%1000 into p
    		p = x%1000;
    
    		//if p == 0, it means x is at a number ending in 3 zeros..., so go check preList to see if process is ready to come in or not
    		if (p == 0)
    		{
    			
    			//if 'arrivalTime' field in preList is equal to the current time in 'x', copy over into "readyList"
    			if (preList[z][0] == x)
    			{
    			
    				//copy into readyList at arrival time
    				cout<<"Arrival time is: "<<x<<" m.s. for process #: "<<h<<endl;
    				readyList[z][0] = preList[z][0];
    				readyList[z][1] = preList[z][1]; 
    				h++;
    				
    
    				
    				cout<<"Time in CPU is: "<<readyList[z][1]<<" m.s."<<endl;
    				
    				//add processing time to arrival time to obtain and see new time
    				newTime = x + readyList[z][1];
    				cout<<"Time after run is: "<<newTime<<endl;
    			
    				cout<<endl;
    				z++;
    				
    			}
    
    			
    		}
    
    		
    				while (readyList[z] != 0 && quantum!=0)
    				{
    					newTime = newTime + 1;
    					
    	
    					if(quantum == 0)
    					{	
    
    
    						//reset quantum to alloted quantum chosen
    						quantum = timeQuanta[quantumEntered-1] + overhead;
    
    						cout<<"Quanta reset"<<endl;
    					}
    
    					wait = x + timeArrived;
    					
    					quantum--;
    					
    					
    
    				}
    
    		//cout<<"quantum is: "<<quantum<<endl;
    
    
    	    //increment counter +1
    		x++;
    		
    	}
    
    	
    
    	averageWaitTime = (timeArrived + wait)/22;
    	cout<<"average wait time: "<<averageWaitTime<<endl;
    
    	temp = newTime - timeArrived;
    	averageTurnAroundTime = temp/22;
    	cout<<"average turnaround time: "<<averageTurnAroundTime<<endl;
    
    
    return 0;
    
    }
    Last edited by bcianfrocca; 11-17-2005 at 07:43 PM.

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    I didn't really try to analyize the code, my only two suggestions are:

    Less blank lines
    More spaces

    It's easier to read when you can see more lines of code in one page, having 3 blank lines between lines of code does no good for readability.

    Having space between operators and identifiers makes the code easier to read as well, at a quick glance.

    Code:
    cout<<"Time in CPU is: "<<readyList[z][1]<<" m.s."<<endl;
    
    is much easier to read quickly as
    
    cout << "Time in CPU is: " << readyList[z][1] << " m.s." << endl;

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21