|
stopwatch
Especially for time-critical calculations it is helpful to know the duration of the computations. The function time from the time library is used in order to retrieve the elapsed seconds since a certain date back in the 1970's and then assign the value to a variable of type time_t. Of course, time differences are relative and do not depend on the zero date.
download example
#include <iostream.h> // for cout
#include <stdlib.h> // for rand()
#include <time.h> // for time()
int main(void)
{
// variables to store time at start and end of application
time_t start_time, finish_time;
// store start time
time(&start_time);
// generate random numbers between 0 and 9999
for(int i=0;i<1e8;++i)
int number=rand()%10000;
// store finish time
time(&finish_time);
// duration of the application in seconds
cout << "Duration: " << (int) difftime(finish_time, start_time) << " seconds.\n";
return 0;
}
time and date
When you work with a log file in order to report your activities it makes sense to store the actual date and time when the application was started and when it was finished. The function ctime from the time library will convert your seconds to an appropriate format.
download example
#include <iostream.h> // for cout
#include <stdlib.h> // for rand()
#include <time.h> // for time()
int main(void)
{
// variables to store time at start and end of application
time_t start_time, finish_time;
// store start time and formatted output
time(&start_time);
cout << "Start: " << ctime(&start_time);
// generate random numbers between 0 and 9999
for(int i=0;i<1e8;++i)
int number=rand()%10000;
// store finish time and formatted output
time(&finish_time);
cout << "Finish: " << ctime(&finish_time);
return 0;
}
uniform random distribution
You can easily generate random numbers from a uniform distribution by using the function rand that is part of the stdlib library. In order to initialize the random generator use the function srand. When you want to reproduce a certain sequence of random numbers use srand with the same parameter value, e.g. srand(1).
download example
#include <iostream.h> // for cout
#include <time.h> // for time()
#include <stdlib.h> // for srand() and rand()
int main(void)
{
// variable to store the generated random number
int number;
// initialize random generator with current time
srand((unsigned) time(NULL));
// generate 10 random numbers
for(int i=0;i<10;++i)
{
// generate random number between 0 and 99
number=rand()%100;
// output of the random number
cout << "The following number has been generated: " << number << endl;
}
return 0;
}
normal random distribution
When you want to draw random numbers from a normal distribution rather than from a uniform distribution the following function normal can be defined in order to generate normally distributed random numbers.
download example
#include <iostream.h> // for cout
#include <stdlib.h> // for srand(), rand() and RAND_MAX
#include <math.h> // for sqrt(), log() and sin()
#include <time.h> // for time()
double normal(const double &, const double &);
int main(void)
{
// initialize random generator
srand((unsigned) time(NULL));
// generate 10 random numbers from normal distribution
for(int i=0;i<10;++i)
cout << normal(5, .7) << endl;
return 0;
}
// draw number from normal random distribution
double normal(const double &mean, const double &std)
{
static const double pii=3.1415927;
static const double r_max=RAND_MAX+1;
return std*sqrt(-2*log((rand()+1)/r_max))*sin(2*pii*rand()/r_max)+mean;
}
file output
Very often there are numbers being calculated during your application that have to be stored in a file. This can be achieved very easily by directing a stream of type ofstream from the fstream library to a file. Everything else remains the same as when you use cout in order to write to the console.
download example
#include <stdlib.h> // for rand() and exit()
#include <fstream.h> // for ofstream
int main(void)
{
// create a file stream for output
ofstream myOutput;
// direct stream to a certain file
myOutput.open("test.txt");
// check if action was successful, otherwise abort application
if(!myOutput)
{
cerr << "An error occurred while opening the file.";
exit(1);
}
for(int i=0;i<9;++i)
{
// write generated number to file stream
myOutput << rand()%10000 << endl;
}
// write one more number without a following space
myOutput << rand()%10000;
// close file
myOutput.close();
return 0;
}
file input
Now that you have seen how to write numbers to a file you can read them in again. This is very analogous to reading keys from the keyboard. The ofstream's counterpart is ifstream from the fstream library.
download example
#include <stdlib.h> // for exit()
#include <fstream.h> // for ifstream and cout
int main(void)
{
int number, counter(0);
// create a file stream
ifstream myInput;
// direct stream to a certain file
myInput.open("test.txt");
// check if action was successful, otherwise abort application
if(!myInput)
{
cerr << "An error occurred while opening the file.";
exit(1);
}
// read numbers from file until the end of file is reached
while(!myInput.eof())
{
// read from file stream
myInput >> number;
// write file input to the screen
cout << number << endl;
// count how many numbers read so far
counter++;
}
// close file
myInput.close();
// number of numbers in the file
cout << endl << counter << " numbers in the file." << endl;
return 0;
}
pointer to function
It is possible to set a pointer to a function. The code below should give you an idea of how to proceed. Note that the star is within the brackets.
download example
#include <iostream.h> // for cout
// define myFunction as a pointer to a function
typedef double (*myFunction)(int);
double sum(int);
double product(int);
int main(void)
{
myFunction f;
// f points to the function 'sum'
f=sum;
cout << "sum: " << f(5) << endl;
// f points to the function 'product'
f=product;
cout << "product: " << f(5) << endl;
return 0;
}
// calculate 0+1+2+..+n where n>=0
double sum(int n)
{
int i, s;
for(i=1,s=0; i<=n; s+=i++);
return s;
}
// calculate 1*2*3*..*n where n>=1
double product(int n)
{
int i, p;
for(i=2,p=1; i<=n; p*=i++);
return p;
}
|