#include<iostream>
#include<cstdlib>
#include<fstream>


using namespace std;
int main()
{   int n=3;
    float total,wait[3]={0};
    float p[3],twaiting=0,waiting=0;
    int proc;
    int stack[3];
    float burst[3],arrival[3],sburst,temp[3],top=3;

for(int i=0;i<n;i++)
    {
        p[i]=i;
        stack[i]=i;
        cout<<"Enter arival time : ";
        cin>>arrival[i];
        cout<<endl<<"Enter burst time: ";
        cin>>burst[i];
        temp[i]=arrival[i];
        sburst=burst[i]+sburst;

    for(i=0;i<sburst;i++)
    {
        proc=stack[0];
        if(temp[proc]==i)
        {
            //temp[proc]=i+1;;
            twaiting=0;
        }
        else
        {
            twaiting=i-(temp[proc]);

        }
        temp[proc]=i+1;

        wait[proc]=wait[proc]+twaiting;
        waiting=waiting+(twaiting);
         burst[proc]=burst[proc]-1;
            if(burst[proc]==0)
        {
            for(int x=0;x<top-1;x++)
            {
                stack[x]=stack[x+1];
             }
            top=top-1;
            for(int z=0;z<top-1;z++)
            {
                 if((burst[stack[0]]>burst[stack[z+1]]) && (arrival[stack[z+1]] <= i+1))

                       int t=stack[0];
            stack[0]=stack[z+1];
            stack[z+1]=t;
              }
          }
        }

cout<<"waiting:"<<waiting;
return 0;
    }
    }
}

There is a little problem at line 57. It says that the t is nt declared. I assume it that i need to insert a header file like <stdlib>.Please help me

Dani AI

Generated

Three things are causing the behavior seen in the thread: scope and brace misuse (the missing/incorrect braces that hid the t declaration), reuse of the same loop index for both input and simulation, and several uninitialized/incorrectly typed counters (for example sburst used before it is set, and top treated as a float). already nailed the scope/initialization points and suggested a manual dry run — both observations are correct and should guide fixes.

Concrete fixes to apply (in this order):

  • Declare loop counters and counters as integer types (e.g. int totalBurst = 0; int top = n;) and initialize them. Never rely on an uninitialized sburst — set it to zero before adding bursts.
  • Read all process inputs in one loop, then run the simulation in a separate time loop. Do not reuse the same i for both loops; use a clearly named time variable for simulation.
  • Put multi-statement if bodies inside braces and declare swap temporaries outside the if (or at least inside a braced block); e.g. int t; if (...) { t = ...; ... }.
  • Move return 0; so it is after all loops (not inside the input or simulation loop).
  • Keep a copy of the original burst values if turnaround calculations are needed; use a rem (remaining) array for the countdown.

Small skeleton showing the structure to follow:

int totalBurst = 0;
for (int i = 0; i < n; ++i) {
  // read arrival[i], burst[i]; rem[i] = burst[i]; totalBurst += burst[i];
}
for (int time = 0; time < totalBurst; ++time) {
  // pick proc = stack[0];
  // update wait, rem[proc]--;
  if (rem[proc] == 0) {
    // remove front, --top;
    // for (int z = 0; z < top - 1; ++z) {
    //   if (cond) { int t = stack[0]; stack[0] = stack[z+1]; stack[z+1] = t; }
    // }
  }
}
return 0;

Quick debugging checklist: print totalBurst, top, stack[0] at key steps; assert top > 0 before accessing stack[0]; test with trivial inputs (one short job, one long job) to confirm waiting/finish times; use a debugger or simple cout traces to follow time, proc, and rem[]. These changes remove undefined behavior and make the loop termination predictable.

Recommended Answers

All 7 Replies

take another look of the if statement 
it only takes one command and whatever was declared in it is destroyed
and gone, cant be approached from the outside of the scope.

if you want all 3 commands to be performed together just add {}

and if you want only the t to be changed and that it could be approached from the
outside just declare it before the IF statement
and update its valure inside of it

thx.okey i solved it already...but now im having trouble..the thing continued to asking me the same thing in a loop..it doesnt end....Help me with me..

your first loop has "i" and the second loop has "i" also
could be it

if i change to the i to j in the second loop..its ends after taking 1 input only..i need to tk 3 input n show 3 input asnwer

you have many uninitialized values that you should fix.
also does the program works fine for you?
because there are illegal calls - segmentation faults
at the IF statement.
try working with a debugger and go slowly , checking if everything you try to
reach should be reached.

What happens when you sit at your desk with the code, pencil, and paper and actually follow the program line by line? Writing the variable values as they change?

but for your last question , look at the return 0 you have at the end
well its NOT in the end, you put it at the end of the loop instead outside of it
and init the variables at the top:

int n=3;
float total=0,wait[3]={0};
float p[3]={0},twaiting=0,waiting=0;
int proc=0;
int stack[3]={0};
float burst[3]={0},arrival[3]={0},sburst=0,temp[3]={0},top=3;
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.