max
defined as
int max(int a, int b)
{
return (b > a) ? b : a;
}
but that my program had three int
s, x
,
y
and z
, amongst which I need the largest. Were
I to write
max(x,y,z)
the compiler would complain ... the only max
function it
knows about only takes two arguments! However, I could say the following:
max(max(x,y),z)
This is our first example of composition of functions. When the
function max
gets called, its two argument expressions are
evaluated. The first is max(x,y)
, which evaluates to the
larger of the two values, and the second is simply z
. So,
what we get out of this is the maximum of all three values.
The most important thing I can tell you about composing functions, is that there is really nothing to talk about. Function arguments are given in your code by expressions, right? And those expressions are evaluated before calling the function to produce the argument objects that are passed to the function. So, whether or not the argument expressions themselves contain function calls is immaterial — the system works the same.
rand()
Functioncstdlib
contains a function rand()
that
you can use. It returns a random int
in the range
0,....,RAND_MAX, where RAND_MAX is a variable defined in
cstdlib
. So, if you need to print out 10 random
numbers in the range [0,1], you could do the following:
for(int i = 0; i < 10; i++)
cout << double(rand())/RAND_MAX << " ";
cout << endl;
If I run this, I get:
0.513871 0.175726 0.308634 0.534532 0.94763 0.171728 0.702231 0.226417 0.494766 0.124699
Unfortunately, if I run it again, I get the same sequence of
numbers! As a matter of fact, every time I run it, I get the same
10 "random" numbers! The problem is this, the computer doesn't
generate random numbers. Instead, it has a long sequence of
numbers that it generates, which looks more or less random -
i.e. there is no discernable pattern. But every time I run my
program, I just start over again with that same sequence.
To combat this, you can call srand()
, which seeds the
random number generator. Essentially, when you give srand
a seed,
it uses the seed to pick a starting point in this sequence of "random" numbers.
With different starting points each time you run the program, you appear to be
getting different sequences of random numbers.
Typically, you use the time()
function from ctime
to
seed your random number generator. Since the time changes each time you run
the program, you should get different sequences of "random" numbers. The
following program shows how this all works:
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
// Seed the random number generator based on the time
srand(time(0));
// Print out 10 random numbers in the range [0,1]
for(int i = 0; i < 10; i++)
cout << double(rand())/RAND_MAX << " ";
cout << endl;
return 0;
}
A: Because it would only return from the current function.
So there is a function "exit" that you can call to exit from anywhere. In particular, consider the following statement:
exit(0); // requires #include <cstdlib>
It will exit the program, no matter where you are.
Write the following programs:
#include <iostream>
using namespace std;
int main()
{
// read size n, allocate array of n doubles, read values into array
int n;
cin >> n;
double * A = new double[n];
for(int i = 0; i < n; i++)
cin >> A[i];
// Now what?
return 0;
}
... and add code after the "Now what?" comment to solve one
of the problems below for the array A of n elements created
in the first part of the code.
~/$ ./ex1a
4
34 12 8 29
minimum element is A[2] = 8.
Solution: TE7a.cpp
~/$ ./ex1b
4
34 12 8 29
A = [34, 12, 8, 29]
Solution: TE7b.cpp
3 7 8 5 \_/ \_/ \_/ 4 1 -3A run of your program should look like this:
~/$ ./ex1c
4
14 16 8 29
2 -8 21
Solution: TE7c.cpp
~/$ ./ex1d
10
0 1 2 -3 4 -5 -6 7 8 9
negative: -3 -5 -6
positive: 1 2 4 7 8 9
Solution: TE7d.cpp