help with logic error

closed account (48CM4iN6)
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

cout<<"sorting...\n";//for sorted sample

for(int i = 1; i<=pnum; i++)//////for SORTED SAMPLE
cout<<p[i]<<" ";
cout<<endl<<endl;


cout<<"Gannt Chart\n\n";

for(i=0; i<pnum+1; i++)// outerloop is for the number in the GC
{

for(int ast=1;ast<=i; ast++)//innerloop for number of asterisk
{
cout<<"*";
}
GC += p[i];
cout<<GC;

gantarray[i] = GC;//for initializing the values into the ganttchart array


}



/*for(i=0; i<pnum+1; i++)
cout<<endl<<gantarray[i]<<" ";//for accessing THE GANTTCHART array*/



cout<<endl<<endl<<endl;


turnaround(gantarray,pnum);//call the turnaround function

waiting(p,gantarray,pnum);//call the waiting function

cout<<endl<<"Do you want to try again [Y/N]?";
cin>>ans;
ask(ans);//call the ask function

}



///////////////////////////////////////////////////////////////////////////////////

int numberofp(int p[],int pnum)
{

int ctr;

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

return 1;
}//end function sorting

//////////////////////////////////////////////////////////////////////////////

void ask(char ans)
{

ans = tolower(ans);

if(ans == 'y')//to restart the program if the user entered 'Y'
{
system("cls");
main();

}//end for if
else
exit(1);
}//END FOR ASK PROGRAM

//////////////////////////////////////////////////////////////////////////////////


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



return 1;
}//end of turnaround function

///////////////////////////////////////////////////////////////////////////////////

int waiting(int p[],int gantarray[],int pnum)//for waiting time program
{
double average;
double sum = 0;
This construction

int p[] = {0};


is invalid in C++. So your code does not conform the C++ standard
Last edited on
Topic archived. No new replies allowed.