[go: up one dir, main page]

0% found this document useful (0 votes)
37 views7 pages

Practical 6 - Standard Functions

This document contains instructions and code snippets for 5 programming problems related to C++ and string handling. It includes examples of using math library functions like sqrt(), exponential and logarithmic functions, and string functions like assignment, concatenation, comparison and C-style strings. It also provides instructions for problems involving reading user input, converting strings to uppercase, checking for vowels, determining character location in alphabet, and implementing a guessing game.

Uploaded by

yang ma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views7 pages

Practical 6 - Standard Functions

This document contains instructions and code snippets for 5 programming problems related to C++ and string handling. It includes examples of using math library functions like sqrt(), exponential and logarithmic functions, and string functions like assignment, concatenation, comparison and C-style strings. It also provides instructions for problems involving reading user input, converting strings to uppercase, checking for vowels, determining character location in alphabet, and implementing a guessing game.

Uploaded by

yang ma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

BACS1013 & BACS1014 Problem Solving and ProgrammingPractical 5

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 expnum1=1, expnum2=2;


cout <<"\nexp(" << expnum1 << ") = " << setprecision(6) << exp(expnum1);
cout <<"\nexp(" << setprecision(1) << expnum2 << ") = "
<< setprecision(6) << exp(expnum2);

double lognum1=2.718282, lognum2=7.389056;


cout << "\nlog(" << lognum1 << ") = " << setprecision(1) << log(lognum1)
<< "\nlog(" << setprecision(6) << lognum2<< ") = "
<< setprecision(1) << log(lognum2);

double log10num1=1, log10num2=10, log10num3=100;


cout << "\nlog10(" << log10num1 << ") = " << log10(log10num1)
<< "\nlog10(" << log10num2 << ") = " << log10(log10num2)
<< "\nlog10(" << log10num3 << ") = " << log10(log10num3) ;

double fabsnum1=13.5, fabsnum2=0, fabsnum3=-13.5;


cout << "\nfabs(" << fabsnum1 << ") = " << fabs(fabsnum1)
<< "\nfabs(" << fabsnum2 << ") = " << fabs(fabsnum2)
<< "\nfabs(" << fabsnum3 << ") = " << fabs(fabsnum3);

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

(b) Demonstrating string assignment and concatenation using string class


// Demonstrating string assignment and concatenation
#include <iostream>
#include <string>
using namespace std;

int main()
{
string string1( "cat" );
string string2;
string string3;

string2 = string1;
string3.assign( string1 );
cout << "string1: " << string1 << "\nstring2: " << string2
<< "\nstring3: " << string3 << "\n\n";

string2[ 0 ] = string3[ 2 ] = 'r';


cout << "After modification of string2 and string3:\n"
<< "string1: " << string1
<< "\nstring2: " << string2 << "\nstring3: ";

for ( int i = 0; i < string3.length(); i++ )


cout << string3.at( i );

string string4( string1 + "apult" );


string string5;

string3 += "pet";
string1.append( "acomb" );

string5.append( string1, 4, string1.length() );


cout << "\n\nAfter concatenation:\nstring1: " << string1
<< "\nstring2: " << string2 << "\nstring3: "
<< string3 << "\nstring4: " << string4
<< "\nstring5: " << string5 << endl;

return 0;
}

Page 2 of 7
BACS1013 & BACS1014 Problem Solving and ProgrammingPractical 5

(c) Demonstrating string comparison capabilities using string class


// Demonstrating string comparison capabilities
#include <iostream>
#include <string>
using namespace std;

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";
}

int result = string1.compare( string2 );


if ( result == 0 )
cout << "string1.compare( string2 ) == 0\n";
else if ( result > 0 )
cout << "string1.compare( string2 ) > 0\n";
else
cout << "string1.compare( string2 ) < 0\n";

result = string1.compare( 2, 5, string3, 0, 5 );


if ( result == 0 )
cout << "string1.compare( 2, 5, string3, 0, 5 ) == 0\n";
else if ( result > 0 )
cout << "string1.compare( 2, 5, string3, 0, 5 ) > 0\n";
else
cout << "string1.compare( 2, 5, string3, 0, 5 ) < 0\n";

result = string4.compare( 0, string2.length(), string2 );


if ( result == 0 )
cout << "string4.compare( 0, string2.length(), string2 ) == 0"<<endl;
else if ( result > 0 )
cout << "string4.compare( 0, string2.length(), string2 ) > 0"<<endl;
else
cout << "string4.compare( 0, string2.length(), string2 ) < 0"<<endl;

result = string2.compare( 0, 3, string4 );


if ( result == 0 )
cout << "string2.compare( 0, 3, string4 ) == 0" << endl;
else if ( result > 0 )
cout << "string2.compare( 0, 3, string4 ) > 0" << endl;
else
cout << "string2.compare( 0, 3, string4 ) < 0" << endl;
return 0;
}

Page 3 of 7
BACS1013 & BACS1014 Problem Solving and ProgrammingPractical 5

(d) Using C-string for string


#include <iostream>
#include <cstring>
using namespace std;

int main()
{
char s1[35] = "GoodDays";
char s2[35] = "LongWay";

cout << "Before modification: " << s1 << endl;


cout << strlen (s1) << " characters in string 1" << endl;

s1[strlen (s1)-1] = '\0';


cout << "\nAfter modification: " << s1 << endl;
cout << strlen(s1) << " characters in string 1\n\n";

strcat_s (s2,s1);
strncat_s (s2,s2, strlen(s2));

cout << "After append, string2: " << s2 << endl;


cout << "Total characters in string2: " << strlen(s2) << endl;
return 0;
}

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.

I am thinking of a number between 1 and 5 Example of


Can you guess what is it? 5 Successful
Not this. Please try again: 2
Not this. Please try again: 3
Dialog
Congratulations! You did it.

Page 4 of 7
BACS1013 & BACS1014 Problem Solving and ProgrammingPractical 5

I am thinking of a number between 1 and 5 Example of


Can you guess what is it? 2 Unsuccessful
Not this. Please try again: 4
Not this. Please try again: 3 Dialog
Sorry :( The number was 1.
Better luck next time.

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"

Example output on the screen:


I am a good student. My Lecturer loves me very much. Thank you

Which character to count? d


There are 2 character 'd' in the text.

I am a good student. My Lecturer loves me very much. Thank you

Which character to count? z


There isn't any character 'z' you requested in the text.

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.

Sample output is shown below:


α(in degree) α(in radian) C²=A²+B²-2(A)(B)cos(α)
---------------- ---------------- ------------------------------
0° 0.00 10.00
15° 0.26 10.66
30° 0.52 12.39
45° 0.79 14.74
60° 1.05 17.32
75° 1.31 19.91
90° 1.57 22.36

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.

The text you have entered is: 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:

Enter your sentence: This is the sentence.


Your sentence printed backward: .ecnetnes eht si sihT

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

You might also like