Hi guys this is a gannt chart for our activity in programming 2. I am just a beginner . There seems to be an error in my code. It is not quite evident. But I think there is still an error. Sometimes the output of the Sorting(function) turnaroundtime(Function) and waiting(function) becomes a garbage value. I wonder why is that so . Can anyone help me? You can run the code to find out too.
#include<iostream>
#include<cstdlib>
#include<cctype>
#include<iomanip>
using namespace std;
#include<windows.h>
int numberofp(int p[],int);// for passing the number of process and burst time manipulation
int sorting(int[],int); //sorting the burst time
int turnaround(int [],int);//for the turnaround time computation
int waiting(int[],int[],int);//for the waiting time computation
void ask(char ans);//prompt the user to try again another process
void main()
{
int p[] = {0};
char ans;
int pnum;
int GC = 0;
int gantarray[100];
cout<<"****************Shortest Job First Scheduling****************"<<endl<<endl;
cout<<"Enter the number of process: ";
cin>>pnum;
numberofp(p,pnum);//call the numberofp function
sorting(p,pnum);//callthe sorting function to sort
while(pnum<1 || pnum >10) //to get the number of process
{
cout<<"1-10 process only!"<<endl;
cout<<"Enter the number of process: ";
if(pnum >=1 || pnum <=10)//to check if the user entered 1-10 values only
cin>>pnum;
}//end for while
system("cls");
cout<<" Process \t BurstTime "<<endl;//burst time manipulation header
ctr = 1;//ctr = 1 because p[0]=0 is will be use for the initial value of GC
while(ctr<=pnum)
{
cout<<"P"<<ctr <<" \t \t \t";
cin>>p[ctr];
if (p[ctr]<1 || p[ctr]>20)//this is to check if the user entered 1-20 as expected by the program
{
cout<<"*Error! You can only Enter 1-20 Burst Time!"<<endl;
ctr= ctr - 1;//to go back to the last array that the program used because of the inputerror.
}
ctr++;
}//endwhile
return 0;
}
//end for function
///////////////////////////////////////////////////////////////////////////////
int sorting(int A[],int pnum)
{
int r, temp;
for(r=0;r<=pnum;r++) //outer loop is for the number of pass
for(int c=1;c<pnum;c++) //inner loop will take charge of the swapping of values
{ if(A[c] > A[c+1])
{ temp = A[c];
A[c] = A[c+1];
A[c+1] = temp;
}//end for if
}//end for forloop
int turnaround(int gantarray[],int pnum)//for turnaround time program
{
double average;
double sum = 0;
cout<<"Turnaround Time:\n\n";
for(int ctr=0; ctr<pnum; ctr++)//loop for outputting turnarround values
{
cout<<"P"<<ctr+1<<" = "<<gantarray[ctr+1]<<" - "<<gantarray[0]<<" = "<<gantarray[ctr+1]<<"\n";
sum += gantarray[ctr+1];//to be use for average
}//end forloop
average = sum/pnum;//toget the average
cout<<"Ave. Turnaround Time = "<<average<<" milliseconds\n\n";//outputtingaverage for turn around time