Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

sleep in C++

Options
  • 06-01-2004 10:09pm
    #1
    Closed Accounts Posts: 1,135 ✭✭✭


    do you know the syntax for using sleep() in c++?

    I think its sleep... to pause the program just for moments... I can't find it on any websites... but I'm nearly sure I've come across it before .. that it does exist?


Comments

  • Registered Users Posts: 1,372 ✭✭✭silverside


    it is win32 specific

    ::Sleep(long l) means the current thread 'goes to sleep' for at least l milliseconds.

    ::Sleep(0) can be used to yield to waiting threads, if any

    Obviously not to be used in portable code. I don't know what the unix equivalent is.

    Look at MSDN if you have any more questions on this.


  • Closed Accounts Posts: 1,135 ✭✭✭KlodaX


    yes win32

    tanks.


  • Registered Users Posts: 2,759 ✭✭✭Col_Loki


    Its the same in unix and using C code....... just a btw


  • Registered Users Posts: 6,316 ✭✭✭OfflerCrocGod


    This should be in programming; anyway dont forget #include <dos.h>.


  • Closed Accounts Posts: 493 ✭✭muffen


    do you know the syntax for using sleep() in c++?

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/sleep.asp

    I cannot believe anyone can program without the MSDN libraries :)
    If you need to know a syntax, just search google for the function, adding "site:microsoft.com" in the query. You'll get the MSDN entry for the function as the first hit most of the times.

    anyway dont forget #include <dos.h>.

    dos.h??

    This is what MSDN libraries has to say about it:
    Header: Declared in Winbase.h; include Windows.h.

    In other words, include windows.h.


  • Advertisement
  • Registered Users Posts: 24 Examking


    #include <iostream>
    #include <iomanip>
    #include <ctime>
    #include <cstdlib>
    #include <fstream>
    #include <windows.h>
    #include <string>
    #include <cmath>//these lines indicates that we may be using some objects created by
    ////another programmer and that the definitions are stored in the files/libraries between the <> brackets

    using namespace std;//The next line indicates that we will be using objects from the std namespace in particular

    double random(double min, double max);
    bool outputToFile(const char* filename, double* data,int n);
    void generateWalk(double *data,int n,double T,double sigma);
    void ensembledata(double *data,int nperwalk, int nens, double T, double sigma);
    bool meanvar(double* data, int size, double *mean , double* var);
    //defining all the functions we intend to use in the program

    int main()
    { srand((unsigned)time(nullptr));//In order to generate random-like numbers, srand is usually initialized to some distinctive runtime value.
    // like the value returned by function time (declared in header <ctime>)
    //generate a random walk
    double *mdata = new double[1000],mean = 0, var = 0;
    //make one random walk
    generateWalk(mdata,1000,10,1);
    outputToFile("ACtest1.csv",mdata,1000);
    //make 100 random walks and store last position
    ensembledata(mdata,1000,1000,10,1);
    outputToFile("ACtest2.csv",mdata,1000);
    meanvar(mdata,1000,&mean,&var);
    cout<<fixed<<setprecision(3);
    cout<<"mean="<<setw(7)<<mean<<",var="<<var<<endl;
    //test it for different sigmas
    double sigvals[]={1,2,3,4,5,6,7,8,9};
    int i = 0;
    for(i = 0;i<9;i++)//for loop to test all the different sigma values in the sigvals array
    {ensembledata(mdata,1000,1000,10,sigvals);
    meanvar(mdata,1000,&mean,&var);
    cout<<"mean="<<setw(7)<<mean<<",var="<<setw(7)<<var<<",sigma="<<setw(7)<<sigvals<<endl;

    }
    delete[]mdata;//tidies up memory by clearing dynamic memory allocation
    system("PAUSE");
    return EXIT_SUCCESS;
    }

    double random(double min, double max)//function to generate random floating point numbers, parameters(min and max), output is type double
    {
    double r3 = min + ((double)(rand()))/( (double)(RAND_MAX/(max-min)));//calculates a random floating point number between 0 and (max-min), we then add min to the value
    // giving us a random floating point number in the interval [min,max], the varaible r3 is assigned this value
    return r3;//returns a random value in the range [min,max]

    }

    bool outputToFile(const char* filename, double* data,int n)//function used to input array data into a csv file
    {ofstream ofile(filename,ios::out);// this either calls the file or creates the file with the given filename(if it doesnt already exist)
    if(!ofile)// an IF statement that returns an error if the file cant be openend or created
    {cout<<"error opening file"<<endl;// prints "error opening file"
    delete [] filename;//tidies up the memory, deletes the character array pointed to by filename
    return EXIT_FAILURE;//The phrase EXIT_SUCCESS is defined in iostream
    //Defining values in this way can make programs more readable. We return this in case another program is waiting for the result.
    }
    int i = 0;//integer used to input each data value
    for(i = 0; i < n; i++)//for loop which writes each value of the array of length n into our csv value
    {ofile << data;//puts the data in the i'th slot of data into the file

    ofile << endl;//data entered into a new row each time, data into a new cell
    }
    ofile.close();//closes the file
    return EXIT_SUCCESS;//bool functions must return a value so we return EXIT_SUCCESS which has a value of 1

    }

    void generateWalk(double *data,int n,double T,double sigma)//void type function that doesnt return a value
    //creates a random walk of length n for a given T and Sigma
    {double st, delta, alpha;//defines 3 double type variables : st, delta and alpha
    double x = 0;//define double x and give it value 0
    st = (T)/(n);//calulate the value of T/n and let st be that value
    alpha = sqrt(st);//calulate the square root of st and let alpha be that value
    delta = (alpha)*sigma;//multiply alpha by sigma and let delta be the resulting value
    data[0]=0;//sets the first point of our array to 0, otherwise our random walk would start at - delta or +delta
    int i;//defines integer variable i
    for(i = 1; i < n; i++)// for loop which fills in our random walk position at each time step into our data array
    {if((rand()%2)==0)//if staement which gives a 50/50 chance of delta being poisitve or negative
    //if random number generate = 0, delta is positive, if its 1 then delta is negative
    {x = delta;
    }
    else
    {x = delta*(-1);
    }
    data = data[i-1] + x ;//assigns data as the previous data point + or minus delta
    }

    }

    void ensembledata(double *data,int nperwalk, int nens, double T, double sigma)//void function type with 4 parameter
    {// nens random walks with nperwalk steps
    double *myarray = new double[nperwalk];//new array (pointer type) used to input the last step of each generateWalk into
    int i = 0;//int used to help count number of random walks
    for(i=0;i<nens;i++)//for loop that runs generateWalk nens times
    {generateWalk(myarray,nperwalk,T,sigma);//calls generate walk
    data = myarray[nperwalk - 1];//Assigns the last step in generateWalk into the i-th space in the array data, the last steps must go into data as main calls functions that use the values in data


    }
    delete [] myarray ;//deltes the dynamic memory array
    }

    bool meanvar(double* data, int size, double *mean , double* var)//function called mean var which returns values of type bool
    {
    double sum = 0; //creates a double type variable and lets it = 0, used to calculate the sum of all the finishing points
    double *start = data; // double pointer type variable which references the start point of our array
    int i = 0; //integer i which value = 0
    for (i = 0; i < size; i++)// for loop for mean calulation
    sum += *data++; //Equivalent to sum=sum + (the value in the first memory address in data), then increments data to the second memory address in data and repeats the process size times(Sums all values in data array up to the sum'th value)
    *mean = sum / size; //caluates the mean and stores it where *mean is pointing to
    //calculate std
    sum = 0; //changes sum back to 0
    data = start; // brings the value of data back to the first memory address
    for (i = 0; i < size; i++) //for loop for varience calulation
    {sum += (*data - *mean)*(*data - *mean);//formula for the numerator of varience
    data++;} // increments data by plus one, used so then *data will point to the next value in the array when the loop is run again
    *var = sum / (size - 1); //calulates varience and stores where *var is pointing to
    return EXIT_SUCCESS; // bool functions must return 0 or 1
    }


  • Moderators, Technology & Internet Moderators Posts: 11,016 Mod ✭✭✭✭yoyo


    After 13 years did this sleeping thread need waking? :P . Please don't bump old threads. Closed.


This discussion has been closed.
Advertisement