Notes 3
Notes 3
----------------
sqrt function
------------
2
ax + bx + c = 0 ,
2
are given by the formula : x = -b +- sqrt(b - 4ac)
____________________
2a
int main()
{
return 0;
}
double sqrt(double) ;
#include <iostream>
#include <string>
#include <cmath> // needed for the definition of the sqrt function
int main()
{
#include <iostream>
#include <string>
#include <cmath> // needed for the definition of the sqrt function
int main()
{
double num ;
cin >> num;
double quadric_root = sqrt(sqrt(num));
cout << quadric_root << endl;
return 0;
}
Some illegal invocations (where the usual conversions are not able
to
convert the actual argument into the type defined by the formal
parameter)
are:
#include <iostream>
#include <string>
#include <cmath> // needed for the definition of the sqrt function
int main()
{
return 0;
}
Note that the compiler reports the line number in the include file
/usr/include/math.h which contains the prototype and it also
indicates
the line number in the source code where the illegal invocation
appears.
#include <iostream>
#include <string>
int main()
{
this_function_returns_no_value(1,2);
this_function_returns_no_value(3,4);
this_function_returns_no_value(34,7869);
return 0;
}
#include <iostream>
Some C++ are derived from the standard C libbraries and have a
c as the prefix as in:
#include <cassert>
#include <cmath>
Again, for the derived libraries, there is no need for the .h suffix
in the library name.
int main()
{
exit(1);
cout << "This will never be printed " << endl;
This program when compiled and run will do nothing and control
will be passed back to the operating system.
exit(0)
#include <iostream>
#include <string>
#include <stdlib.h>
int main()
{
EVERY time that we run the above program it will produce the values:
16838
5758
10113
17515
31051
5627
23010
7419
16212
4086
#include <iostream>
#include <string>
#include <stdlib.h>
int main()
{
int seed_value;
cout << "Please enter a seed value" ;
cin >> seed_value;
srand(seed_value);
for(int i = 0 ; i <= 9 ; i++)
cout << rand() << endl; // print out 10 random numbers between 0
and 32767
return 0;
}
The program's output when we run it two times with different initial
seed values
is:
cout << rand() % 7 << endl; // print out 20 random numbers between
0 and 6
0
1
3
0
3
6
4
6
6
5
0
2
0
1
2
4
5
4
6
6
2
0 1 2 3 4 5 6
--------------------------------------------------
* * * * * * *
* * * * * * *
* * * *
* *
You can see that even for a very small sample space of
20 values, the distribution is quite uniform. Each of
the 7 values between 0 and 6 is equally likely to occur.