This is The function i have written to get waiting times of processes

``````class NonPreemptive
{
public:
int* getWaitingTime(int* process, int* arrivalTime, int* burstTime, int nProcess)
{
int ct[5],temp,j;
int* wt = (int*)malloc(sizeof(int)* 5);
wt[0]=0,j=0;
for(int i=process[j++];i<=nProcess;i++)
{
if((arrivalTime[0]==0)&&(arrivalTime[j]<arrivalTime[j+1])&&(0<burstTime[j]<10))
{
ct[i]=arrivalTime[i]+burstTime[i]+wt[i];
wt[i+1]=ct[i]-arrivalTime[i+1];
if((arrivalTime[j+1]+burstTime[j+1])>(arrivalTime[j+2]+burstTime[j+2]))
int temp=process[j+1];process[j+1]=process[j+2];process[j+2]=temp;
}
else return NULL;
}return wt;
}
};``````

I am working on Non preemptive scheduling algorithm.....
My below function needs to written the waiting time of the processes 1,2,3,4,
and below are the 3 test cases needs to be satisfied.
Test case 1:

``````int proc[] = {1, 2, 3, 4};
int arrivalTime[] = {0, 2, 4, 5};
int burstTime[] = {7, 4, 1, 4};``````

Test case 2:

``````int proc[] = {1, 2, 3};
int arrivalTime[] = {0, 4, 8};
int burstTime[] = {6, 2, 1};``````

Test case 3:

``````int proc[] = {1, 2, 3, 4};
int aTime[] = {2, 0, 4, 5};
int bTime[] = {7, 4, 1, 4};``````

we have given a constraints like below to satisfy:

i) If more than 1 processes have arrived before the completion of current process
then allocation should be done for the process which having min value of
(arrivalTime+burstTime);
ii) Burst time of processes should be between 0<burst time<10;
ii) arrival Time of the first process should be 0;
vi)arrival Times should be in increasing order.
v)if above condition fails then return NULL;

output for above should be:

``````test case1: {0,6,3,7}
test case2:{0,2,0}
test case3:{NULL}``````

This code is a mess, and 99% is that it is mixed c and c++ , it has not been set out tidily so neither I nor you know what is going on.

Things to fix:

Why use malloc to allocate memory use `int* wt=new int[5];` Then remember to delete that memory. You fail to do that in your code because if you get the the return NULL you will have made a memory leak.

The loop construction: `for(int i=process[j++];i<=nProcess;i++)` is 100% too complex. There are nProcess and process has n variables
so what about `for(int i=0;i<nProcess;i++)` So if process[0] is > nProcess you stop that is wrong.

Then further stangeness, j goes to 1 and stays there,so why write `arrivalTime[j+1]` so why no write `arrivalTime[2]` .

The next problem is that you write

``````if ((arrivalTime[j+1]+burstTime[j+1]) >
(arrivalTime[j+2]+burstTime[j+2]))
int temp=process[j+1];
process[j+1]=process[j+2];
process[j+2]=temp;``````

Unfortunately, you declare temp again, and that MASKS the original temp. e.g consider this

``````int temp=1;
if (1<2)
int temp=4;
std::cout<<"Temp == "<<temp<<std::end;;``````

This piece of code prints 1 it does not print 4.

Finally, why pass nProcess when this routine only can deal with 4 entries.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.