8.5.1 Standard Header File 8.5.2 Standard Functions
8.5.1 Standard Header File 8.5.2 Standard Functions
Where???
i.
ii.
iii.
iv.
v.
Header File
#include <iostream>
#include <cstdlib>
#include <cmath>
Example:
Header file iostream contains functions cin and
cout.
Header file & function
#include <iostream>
int main()
{
cout<<“I Love Computer Science.”;
return 0;
}
Let’s Recall…
Example:
#include <iostream>
means it will include the header file iostream so that
the program can use related functions (i.e cin and
cout)
Examples of Standard Header Files
General Form:
#include <Header File>
Examples:
#include <iostream>
#include <cmath>
Header file <iostream>
Example :
cin>>radius;
cout<<
Example :
cout<<“Area = \n”;
cout<<area;
Usage of cin>> and cout<<
#include <iostream>
int main()
{
int number;
cout<<"Enter your favorite number: ";
cin>>number;
cout<<"Number entered: " <<number ;
return 0;
} **masukkan print screen programming
codeblock
Header file <cmath>
pow(base,exponent)
Example:
cout<<pow(7,3); //means 73
pow : Example
/* pow example */
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout<<"3 ^ 2 = "<<pow(3,2)<< endl;
cout<<"7 ^ 3 = "<<pow(7,3)<< endl;
cout<<"4.73 ^ 12 = "<<pow(4.73,12)<<endl;
cout<<"32.1 ^ 1.54 = "<<pow(32.1,1.54)<< endl;
return 0;
}
pow : Output
sqrt( )
sqrt(x)
Returns the square root of x.
Example:
Y = sqrt(49);
cout<<Y;
sqrt : Example
/* sqrt example */
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
double param, result;
param = 1024.0;
result = sqrt(param);
cout << "sqrt(1024) = " << result;
return 0;
}
sqrt : Output
Summary
<iostream> <cmath>
i) _____________ i) ______________
ii) _____________ ii) ______________
Sample Question 2:
2. Given the program below
#include <iostream>
int main()
{
double a, b, c, root1, root2;
cout<<"Please enter values for a, b and c: ";
cin>>a>>b>>c;
root1 = -b + sqrt(pow(b,2)- 4 *a *c))/(2*a);
root2 = -b - sqrt(pow(b,2)- 4 *a *c))/(2*a);
cout<<“The roots are: "<<root1<<root2;
return 0;
}
Write one preprocessor directive that should be included in the
program above.
________________________________________________