Troubleshooting code

This is a discussion on Troubleshooting code within the C++ Programming forums, part of the General Programming Boards category; need help troubleshooting... i want this to copy from the preList into the readyList whenever the requirements are met... when ...

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

    Troubleshooting code

    need help troubleshooting... i want this to copy from the preList into the readyList whenever the requirements are met... when x%1000 = 0 and whenever preList[i][0] = x.... why won't this code work??

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    
    
    using namespace std;
    
    
    
    long int preList[21][2];
    long int readyList[21][2];
    
    
    
    
    int main()
    {
    
    	fstream inFile;
    	inFile.open("times.txt", ios::in);
    
    
    	int arrivalTime;
    	float timeToProcess;
    	int i, z = 0;
    	long int x = 0;
    	int p;
    
    
    	//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<=21; i++)
    		{	
    			
    			inFile>>arrivalTime>>timeToProcess;
    
    			preList[i][0] = arrivalTime * 1000;
    			preList[i][1] = timeToProcess;
    
    		    cout<<preList[i][0]<<"  "<<preList[i][1]<<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[i][0] == x)
    			{
    
    				readyList[z][0] = preList[i][0];
    				readyList[z][1] = preList[i][1];
    				
    				cout<<readyList[z][0]<<"   "<<readyList[z][1]<<endl;
    				z++;
    			}
    			
    		}
    	
    		x++;
    	}
    
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > for(i=0; i<=21; i++)
    This should be:
    for(i=0; i<21; i++)

    > //if 'arrivalTime' field in preList is equal to the current time in 'x', copy over into "readyList"
    > if (preList[i][0] == x)
    This should be:
    if (preList[z][0] == x)

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    first, what exactly is going wrong?

    second, just looking at your comments, think about the first run: what's the remainder of 0/1000? I'm not sure if that's really hurting you in any way, but from your comment it looks like a logic flaw.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    i'm just getting a list of zeros for the output... here is the updated code with changes made... the point is when "arrivalTime" in preList is equal to "x", copy all data from node with "arrivalTime = x" into first node in "readyList", and then when the next node in "preList" is equal to x, add that to next available slot in "readyList''... i hope that all makes sense... here's the updated code...

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    
    
    using namespace std;
    
    
    
    long int preList[21][2];
    long int readyList[21][2];
    
    
    
    
    int main()
    {
    
    	fstream inFile;
    	inFile.open("times.txt", ios::in);
    
    
    	int arrivalTime;
    	float timeToProcess;
    	int i, z = 0;
    	long int x = 0;
    	int p;
    
    
    	//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++)
    		{	
    			
    			inFile>>arrivalTime>>timeToProcess;
    
    			preList[i][0] = arrivalTime * 1000;
    			preList[i][1] = timeToProcess;
    
    		   // cout<<preList[i][0]<<"  "<<preList[i][1]<<endl;
    		}
    	}
    
    	i = 0;
    
    	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)
    			{
    			
    				preList[i][0] = readyList[z][0];
    				preList[i][1] = readyList[z][1];
    
    				cout<<readyList[z][0]<<"   "<<readyList[z][1]<<endl;
    				z++;
    				
    			}
    		}
    	
    		x++;
    	}
    
    
    return 0;
    
    }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > readyList[z][0] = preList[i][0];
    > readyList[z][1] = preList[i][1];
    And z here also.
    readyList[z][0] = preList[z][0];
    readyList[z][1] = preList[z][1];

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > for(i=0; i<22; i++)
    I'm assuming you made this 22 for debugging purposes.

  7. #7
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    there's actually 22 items in my array once read in from the file, so 0-21 will be the "values"

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >there's actually 22 items in my array once read in from the file, so 0-21 will be the "values"

    >long int preList[21][2];
    >long int readyList[21][2];
    And your arrays only have 21 elements?

  9. #9
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    yeah, you're right... good catch...

  10. #10
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    new problem!!! here is the updated version of the code i've been working on. I explain in the comments in the code what needs to be done. If someone could help me out here i would GREATLY appreciate it because I am lost. Here is the code with the comments about what needs to be done...

    Code:
    #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 overhead[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;
    	long int x = 0;
    	int p;
    	int newTime = 0;
    	bool done = 0;
    	int quantumEntered;
    
    
    	cout<<"Please enter time quantum number 1 - 4"<<endl;
    	cin>>quantumEntered;
    
    
    	//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++)
    		{	
    			
    			inFile>>arrivalTime>>timeToProcess;
    
    			preList[i][0] = arrivalTime * 1000;
    			preList[i][1] = timeToProcess;
    
    		    cout<<preList[i][0]<<"  "<<preList[i][1]<<endl;
    		}
    	}
    
    	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<<"Time is: "<<x<<endl;
    				readyList[z][0] = preList[z][0];
    				readyList[z][1] = preList[z][1]; 
    				
    				cout<<"ready list service time: "<<readyList[z][1]<<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++;	
    
    			}
    
    
    			//SUPPOSED TO SEND PROCESS TO BE PROCESSED INTO PROCESSOR.  IF PROCESS ARRIVES AT 30,000, AND HAS A JOB LENGTH OF 1,700, THE JOB WILL BE DONE
    			//AT 31,700... SUPPOSE A NEW PROCESS ARRIVES AT 35,000... THERE IS NO JOB IN THE "PROCESSOR" AT THAT TIME, SO IT IS IMMEDIATELY TAKEN TO BE PROCESSED
    			//THE TIME QUANTUM IS SUPPOSED TO GO AND CHECK THE "READY LIST" TO SEE IF THERE IS A JOB WAITING; IF THERE IS, THE CURRENT JOB IN THE 
    			//PROCESSOR GETS AN ALLOCATED AMOUNT OF TIME (timeQuana) TO PROCESS.  WHEN "QUANTA" REACHES 0, THE JOB SHOULD BE SWITCHED FROM THE CURRENT JOB
    			//TO THE JOB WAITING IN THE READY LIST, THE NEW JOB IN GETS A TIME QUANTA OF 50, AND THE PROCESS IS REPEATED UNTIL THE ORIGINAL PROCESS IS DONE.
    			
    			//HELP!
    			if (readyList[z] != 0)
    			{
    				int quantum = timeQuanta[quantumEntered-1];
    
    					
    					newTime = newTime + 1;
    					//cout<<"new time: "<<newTime<<endl;
    					
    					//cout<<"quantum: "<<quantum<<endl;
    
    					if(quantum == 0)
    					{
    						quantum = timeQuanta[quantumEntered -1];
    					
    					}
    						
    
    					quantum--;
    
    				
    			}
    
    				
    		}
    
    	
    		x++;
    	}
    
    
    
    return 0;
    
    }
    as it says above, i need to have the timeQuantum work as a "pre-emptive" scheduler. it should be "time slices" to switch the processes from a simulated "processor" back into the ready list with the remaining time left to process (readyList[z][1] - however much work just got done)... i'm totally lost here.. please someone help!!!

  11. #11
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    updated code: someone help please!!

    Code:
    #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;
    	long int x = 0;
    	int p;
    	int newTime = 0;
    	int quantumEntered, overheadEntered;
    
    
    	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++)
    		{	
    			
    			inFile>>arrivalTime>>timeToProcess;
    
    			preList[i][0] = arrivalTime * 1000;
    			preList[i][1] = timeToProcess;
    
    		    cout<<preList[i][0]<<"  "<<preList[i][1]<<endl;
    		}
    	}
    
    	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<<"Time is: "<<x<<endl;
    				readyList[z][0] = preList[z][0];
    				readyList[z][1] = preList[z][1]; 
    
    				
    				cout<<"ready list service time: "<<readyList[z][1]<<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++;	
    
    			}
    
    			//SUPPOSED TO SEND PROCESS TO BE PROCESSED INTO PROCESSOR.  IF PROCESS ARRIVES AT 30,000, AND HAS A JOB LENGTH OF 1,700, THE JOB WILL BE DONE
    			//AT 31,700... SUPPOSE A NEW PROCESS ARRIVES AT 35,000... THERE IS NO JOB IN THE "PROCESSOR" AT THAT TIME, SO IT IS IMMEDIATELY TAKEN TO BE PROCESSED
    			//THE TIME QUANTUM IS SUPPOSED TO GO AND CHECK THE "READY LIST" TO SEE IF THERE IS A JOB WAITING; IF THERE IS, THE CURRENT JOB IN THE 
    			//PROCESSOR GETS AN ALLOCATED AMOUNT OF TIME (timeQuanta) TO PROCESS.  WHEN "QUANTA" REACHES 0, THE JOB SHOULD BE SWITCHED FROM THE CURRENT JOB
    			//TO THE JOB WAITING IN THE READY LIST, THE NEW JOB IN GETS A TIME QUANTA OF 50, AND THE PROCESS IS REPEATED UNTIL THE ORIGINAL PROCESS IS DONE.
    
    
    			if (readyList[z] != 0)
    			{
    				
    
    					newTime = newTime + 1;
    	
    					if(quantum == 0)
    					{
    						//go check readyList, if there is something in there
    						//switch processes in "cpu"
    						quantum = timeQuanta[quantumEntered-1] + overhead;
    
    					}
    						
    					cout<<"Quantum is:  "<<quantum<<endl;
    					quantum--;
    
    				
    			}
    
    				
    		}
    
    	
    		x++;
    	}
    
    
    
    return 0;
    
    }

  12. #12
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    updated code: anyone out there??????

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    
    
    using namespace std;
    
    
    
    long int preList[22][2];
    long int readyList[22][3];
    
    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;
    	long int x = 0;
    	int p;
    	int newTime = 0;
    	int quantumEntered, overheadEntered, timeArrived;
    	int averageWaitTime, averageTurnAroundTime = 0;
    
    
    	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++)
    		{	
    			
    			inFile>>arrivalTime>>timeToProcess;
    
    			preList[i][0] = arrivalTime * 1000;
    			preList[i][1] = timeToProcess;
    
    			timeArrived = x;
    
    		    cout<<preList[i][0]<<"  "<<preList[i][1]<<endl;
    		}
    	}
    
    	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<<"Time is: "<<x<<endl;
    				readyList[z][0] = preList[z][0];
    				readyList[z][1] = preList[z][1]; 
    				readyList[z][2] = 1;
    
    				if (readyList[z][2] != 0)
    				{
    				
    					newTime = newTime + 1;
    	
    					if(quantum == 0)
    					{
    						//go check readyList, if there is something in there
    						//switch processes in "cpu"
    						
    						
    
    
    						//reset quantum to alloted quantum chosen
    						quantum = timeQuanta[quantumEntered-1] + overhead;
    
    					}
    						
    					cout<<"Quantum is:  "<<quantum<<endl;
    					quantum--;
    
    				}
    
    				
    				cout<<"ready list service time: "<<readyList[z][1]<<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++;	
    			}
    			
    		}
    
    	    
    		x++;
    	}
    
    	//averageWaitTime = (time process enters CPU - timeArrived)/22;
    	//cout<<"average wait time: "<<averageWaitTime<<endl;
    
    	averageTurnAroundTime = (newTime - timeArrived)/22;
    	cout<<"average turnaround time: "<<averageTurnAroundTime<<endl;
    
    
    return 0;
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

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