Practical 6 - Standard Functions
Practical 6 - Standard Functions
Practical 6
1. Trace the programs below. Understand the output produced on the screen.
(a) Testing the cmath library functions
// Testing the cmath library functions.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double sqrtnum1=900, sqrtnum2=9;
cout << fixed << setprecision(1);
cout << "sqrt(" << sqrtnum1 << ") = " << sqrt(sqrtnum1)
<< "\nsqrt(" << sqrtnum2 << ") = " << sqrt(sqrtnum2);
double ceilnum1=9.2,ceilnum2=-9.8;
cout << "\nceil(" << ceilnum1 << ") = " << ceil(ceilnum1)
<< "\nceil(" << ceilnum2 << ") = " << ceil(ceilnum2);
double floornum1=9.2,floornum2=-9.8;
cout << "\nfloor(" << floornum1 << ") = " << floor(floornum1)
<< "\nfloor(" << floornum2 << ") = " << floor(floornum2);
cout << "\npow(" << 2.0 << ", " << 7.0 << ") = " << pow( 2.0, 7.0 );
cout << "\npow(" << 9.0 << ", " << 0.5 << ") = " << pow( 9.0, 0.5 );
cout << setprecision( 1 ) << "\nsin(" << 0.0 << ") = " << sin( 0.0 );
cout << "\ncos(" << 0.0 << ") = " << cos( 0.0 );
cout << "\ntan(" << 0.0 << ") = " << tan( 0.0 ) << endl;
return 0;
}
Page 1 of 7
BACS1013 & BACS1014 Problem Solving and ProgrammingPractical 5
int main()
{
string string1( "cat" );
string string2;
string string3;
string2 = string1;
string3.assign( string1 );
cout << "string1: " << string1 << "\nstring2: " << string2
<< "\nstring3: " << string3 << "\n\n";
string3 += "pet";
string1.append( "acomb" );
return 0;
}
Page 2 of 7
BACS1013 & BACS1014 Problem Solving and ProgrammingPractical 5
int main()
{
string string1( "Testing the comparison functions." );
string string2( "Hello" );
string string3( "stinger" );
string string4( string2 );
cout << "string1: " << string1 << "\nstring2: " << string2
<< "\nstring3: " << string3 << "\nstring4: " << string4 << "\n\n";
if ( string1 == string4 )
cout << "string1 == string4\n";
else
{
if ( string1 > string4 )
cout << "string1 > string4\n";
else
cout << "string1 < string4\n";
}
Page 3 of 7
BACS1013 & BACS1014 Problem Solving and ProgrammingPractical 5
int main()
{
char s1[35] = "GoodDays";
char s2[35] = "LongWay";
strcat_s (s2,s1);
strncat_s (s2,s2, strlen(s2));
2. Write a C++ program that reads a string of text from the user and convert the string to all uppercase.
(a) Declare your string using string class
(b) Declare your string using C-string (assume the size of string is 50)
3. Write a C++ program that continuously prompts the user to enter a character to verify whether it is a
vowel (a, e, i, o, u), until the user press ‘9’ to stop.
4. Write a program that gets a character from the user and tells whether it is a letter. If the character
entered is a letter, your program also has to tell its numerical location in the alphabet (for example,
‘e’ and ‘E’ would both be 5). If it is not a character, its numerical location will be -1. Note: You are not
allowed to use isalpha() function but you can use toupper() or tolower() function.
5. This program is a simple guessing game. The computer is to generate a random number between 1
and 5. The user is given up to 3 tries to guess the exact number. After each guess, you are to check if
the guessed number is equal to the random number. If it is equal, no more guesses should be made.
If the user has not guessed the number after 3 tries, display the number with an appropriate message
and terminate the game.
Page 4 of 7
BACS1013 & BACS1014 Problem Solving and ProgrammingPractical 5
6. Write a C++ program that receives a positive floating-point number from the user. Your program will
round the number to two decimal places. For example, if 123.456789 are entered, 123.460000 would
be printed on the screen as the output.
7. Write a C++ program that will find and count the number of a similar character input by user from the
string below:
"I am a good student. My Lecturer loves me very much. Thank you"
Page 5 of 7
BACS1013 & BACS1014 Problem Solving and ProgrammingPractical 5
Extra exercise
1. Write a program to display a table of values for angle α (in degree), α (in radian) and side C of the
triangle shown. The length of side C can be calculated using the law of cosines as shown below. Use
for loop to calculate C as angle α varies from 0 to 90 degrees in 15 degree increments. Use math library
function to assist you. Define PI as 3.14159.
2. Write a C++ program that asks the user to enter a line of text and then displays the text. From the text,
counts and displays the occurance of an uppercase character, lowercase character, space,digit and
other characters. The user's input data is shown in boldface.
Enter a line of text: KL Airplane has 14 rows.
Text Count
-----------------------------------------
Upper case 3
Lower case 14
Space 4
Digit 2
Others 1
3. Write a C++ program that asks the user to enter a sentence and your program will display the sentence
backward.
Example:
Page 6 of 7
BACS1013 & BACS1014 Problem Solving and ProgrammingPractical 5
4. Write a C++ program that calculates the squares and cubes of the integers from 1 to 10 using pow()
function and uses setw() to print the following neatly formatted table of values:
Number Square Cube
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
5. Write a C++ program that prints the following table using the sqrt function in the cmath library.
Number SquareRoot
0 0.0000
2 1.4142
… …
18 4.2426
20 4.4721
Page 7 of 7