After,My previous post on trying to come up with a code for Round robin algorithm in c++ which can be found here
[
^]
I thought of taking Andy's suggestion and come up with a new way of writing the algorithm. Thanks to Andy and the people of code project for their help.
So, here is my new code and i have added comments almost every where. I am stuck at some part of my program . Please can anyone review my code and correct my mistake. The areas where i am stuck, i shall mention below. Here is my code--
#include <iostream>
#include <stdio.h>
static int n;
int i,j,total_time = 0;
float avgtat=0, avgwt = 0;
int queue[];
int time = 0, front = 0,rear = 0,q_count = 0;
using namespace std;
struct data {
int at,st,ct,tat,wt; char pname[20];
float ntat;
}temp;
void push(int q) {
queue[rear++] = q;
}
int pop() {
int x;
x = queue[front++];
return x;
}
void check(struct data a[]) {
while(a[j].at<time&&j> {
q_count++; push(j++); }
}
void finding(struct data a[])
{
int temp_st[n] ,flag =0, count = 0, p_process; j = 0; int tq;
cout<<"Enter time quantum"<<endl;
cin>>tq;
for(i=0;i<n;i++)>
{
temp_st[i] = a[i].st; }
time = a[0].at; q_count = 1; push(j++); while(time<=total_time) {
if(flag==1 ||q_count!=0) {
if(flag==0 &&count==0) {
p_process = pop(); count =0; flag=1; }
temp_st[p_process]--;
if(temp_st[p_process]==0) {
time++; count =0;
a[p_process].ct = time; flag = 0; q_count--; check(a); continue; }
count++;
if(count==tq) {
count = 0; time++; check(a); push(p_process); flag =0; }
else {
time++; check(a); }
}
else
{
time++;
check(a);
}
}
}
void display(struct data a[])
{
}
int main()
{
cout<<"Enter the number of processes"<<endl;
cin>>n;
struct data aa[n];
for (i = 0;i<n;i++)>
{
cout<<"Enter the name of the processes"<<endl;
cin>>aa[i].pname;
cin>>aa[i].at>>aa[i].st;
}
for (i=0;i<n;i++)>
{
for(j=i+1; j<n;j++)>
{
if(aa[j].at<aa[i].at)>
{
temp = aa[i];
aa[i] = aa[j];
aa[j] = temp;
}
else if(aa[i].at==aa[j].at)
{
if(aa[j].st<aa[i].st)>
{
temp = aa[i];
aa[i] = aa[j];
aa[j] = temp;
}
}
}
total_time+=aa[0].at+aa[0].st;
for(i = 0;i<n;i++)>
{
if(aa[i].at>total_time)
total_time += aa[i].st;
}
finding(aa);
return 0;
}
Here are the few problems i am having--
1. The part where i call the structure RR in my main
struct RR a[n];
//here it gives me error by saying that static n must have a constant value
2.Also in my void finding function where i declare an array called
int temp_st[n] to store the service time/burst time of each process. I get the same error which says "Static n must have a constant value"
3. Finally, i am new to data structures and i dont have much experience with it. So, i am confused what should i declare in my display function
void display(struct data a[]) { ------Please help me here -----}
Lastly please suggest if there is any more corrections which i should make. Thanks