Help with Round robin code in c++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Help with Round robin code in c++

  1. #1
    Join Date
    Jul 2014
    Posts
    1

    Help with Round robin code in c++

    After lots of research online and studying various source codes. I thought of trying myself to come up with a way to write the round robin code.
    I tried but i get errors in certain part of my output. I am unable to generate the Gaint chart and also please can anyone explain me where i made mistake in calculating my waiting time algorithm.

    Can anyone explain me specially how can i get the waiting time of each process and kindly correct my algorithm .Please i have a sincere request. I am a self learner and there is basically no one other than online articles or books to help me understand a code. So, if you correct my mistake, please can you explain me along with the correction. I really want to get the concept because i don't want to do the same mistake again. Thanks.


    ------- Here is my code --------------

    #include<iostream>

    using namespace std;

    int main()
    {
    int k,j,q,i,n,ts,temp;
    int aw; float awt;
    int bt[10],wt[10],te[10],rt[10],at[10];j=0; //te array stores the number of times a process comes to CPU before completion

    //bt is my burst time store array
    //wt is my waiting time array
    //te keeps a count of the number of times a process enters the CPU before completion
    //at is the arrival time


    cout<<"Enter number of processes"<<endl;
    cin>>n;



    for(i=0;i<n;i++)
    {

    cout<<"Enter burst time"<<endl;

    cin>>bt[i];

    cout<<"Enter arrival times"<<endl;
    cin>>at[i];

    te[i] = 0; wt[i] = 0;
    }

    for(i=0;i<n;i++)
    {
    for(j = i+1;j<n;j++)
    {
    if(at[j]<at[i])
    {
    temp = at[i];
    at[i] = at[j];
    at[j] = temp;
    if(at[j] ==at[i])
    temp = bt[i];
    bt[i] = bt[j];
    bt[j] = temp;
    }
    }
    }


    cout<<"Enter time slice"<<endl;
    cin>>ts;

    cout<<"process:"<<endl;


    for(i=0;i<n;i++)
    {
    cout<<"\t"<<i+1<<endl;
    }



    cout<<"Burst time:"<<endl;
    for(i=0;i<n;i++)
    {
    cout<<" "<<bt[i]<<endl;
    rt[i] = bt[i];
    }

    cout<<"arrival time:"<<endl;
    for(i=0;i<n;i++)
    {
    cout<<" "<<at[i]<<endl;
    }

    cout<<"Gaint chart"<<endl;

    while (j<=n)
    {

    j++;

    for(i = 0;i<n;i++)
    {
    if(rt[i] ==0) continue;
    if(rt[i]>=ts)
    {
    cout<<"\t"<<q<<i+1<<endl;
    q = q + ts;
    rt[i] = rt[i] - ts;
    te[i] = te[i] + 1;
    }

    else
    {
    cout<<" "<<q<<i+1<<endl;
    wt[i] = q-te[i]*ts;
    q = q +rt[i];
    rt[i] = rt[i] - rt[i];
    }
    }
    }

    awt = 0;


    cout<<"Process Waiting Time"<<endl;
    for(i =0;i<n;i++)
    {
    wt[i] = wt[i] - at[i];
    cout<<" "<<i+1<<endl;
    cout<<wt[i]<<endl;
    awt = awt + wt[i];
    }
    aw = awt;
    cout<<"Total waiting time"<<aw<<endl;
    cout<<"Average waiting time "<<awt/n<<endl;

    return 0;


    }

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,558

    Re: Help with Round robin code in c++

    1. the question has nothing to do with the Visual C++, so it is now moved to the more appropriate forum.
    2. Your code is absolutely unreadable. Please, use Code tags around code snippets. See Announcement: Before you post....
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    4,121

    Re: Help with Round robin code in c++

    I've formatted the code to make it more readable

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    int	k,
    	j,
    	q,
    	i,
    	n,
    	ts,
    	temp;
    
    int	aw;
    
    float awt;
    
    int     bt[10],
    	wt[10],
    	te[10],		//te array stores the number of times a process comes to CPU before completion
    	rt[10],
    	at[10];
    
    	j = 0; 
    
    //bt is my burst time store array
    //wt is my waiting time array
    //te keeps a count of the number of times a process enters the CPU before completion
    //at is the arrival time
    
    	cout << "Enter number of processes" << endl;
    	cin >> n;
    
    	for (i = 0; i < n; i++)
    	{
    		cout << "Enter burst time" << endl;
    		cin >> bt[i];
    
    		cout << "Enter arrival times" << endl;
    		cin >> at[i];
    
    		te[i] = 0;
    		wt[i] = 0;
    	}
    
    	for (i = 0; i < n; i++)
    	{
    		for (j = i + 1; j < n; j++)
    		{
    			if (at[j] < at[i])
    			{
    				temp = at[i];
    				at[i] = at[j];
    				at[j] = temp;
    				if (at[j] == at[i])
    					temp = bt[i];
    
    				bt[i] = bt[j];
    				bt[j] = temp;
    			}
    		}
    	}
    
    	cout << "Enter time slice" << endl;
    	cin >> ts;
    
    	cout << "process:" << endl;
    
    	for (i = 0; i < n; i++)
    	{
    		cout << "\t" << i + 1 << endl;
    	}
    
    	cout << "Burst time:" << endl;
    	for (i = 0; i < n; i++)
    	{
    		cout << " " << bt[i] << endl;
    		rt[i] = bt[i];
    	}
    
    	cout << "arrival time:" << endl;
    	for (i = 0; i < n; i++)
    	{
    		cout << " " << at[i] << endl;
    	}
    
    	cout << "Gaint chart" << endl;
    
    	while (j <= n)
    	{
    		j++;
    		for (i = 0; i < n; i++)
    		{
    			if (rt[i] == 0) 
    				continue;
    
    			if (rt[i] >= ts)
    			{
    				cout << "\t" << q << i + 1 << endl;
    				q = q + ts;
    				rt[i] -= ts;
    				te[i]++;
    			}
    			else
    			{
    				cout << " " << q << i + 1 << endl;
    				wt[i] = q - te[i] * ts;
    				q += rt[i];
    				rt[i] = rt[i] - rt[i];	//ARE YOU SURE?????
    			}
    		}
    	}
    
    	awt = 0;
    
    	cout << "Process Waiting Time" << endl;
    	for (i = 0; i < n; i++)
    	{
    		wt[i] -= at[i];
    		cout << " " << i + 1 << endl;
    		cout << wt[i] << endl;
    		awt += wt[i];
    	}
    
    	aw = awt;
    	cout << "Total waiting time" << aw << endl;
    	cout << "Average waiting time " << awt / n << endl;
    
    	return 0;
    }
    Are you sure you want this line
    Code:
    rt[i] = rt[i] - rt[i];	//ARE YOU SURE?????
    What debugging of this code have you done to determine where it starts to deviate rom what you expected?
    Last edited by 2kaud; July 27th, 2014 at 04:13 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
    C, C++ Compiler: Microsoft VS2015

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center