Professional Documents
Culture Documents
1 Lecture 9 Exercises
The only exercises we did in Lecture 9 involved library functions. The answers are
provided here for your information but there will not be any questions on the quiz that
require knowledge of library functions.
1.1 Exercise 1
Replace bold lines in following program with the abort library function.
#include <fstream> // provides ifstream, ofstream
#include <iostream> // provides cout
using namespace std;
int main()
{
ifstream infile;
infile.open("yards.in");
if (!infile) {
cout << "Unable to open input file" << endl;
cout << "Abnormal termination program" << endl;
return 0;
}
return 0;
}
1.2 Exercise 2
Write a code segment to generate and display 5 random numbers between 0 and 1000.
assume srand has already been called with the current time to properly initialize the
random generator
2 Lecture 10 Exercises
2.1 Exercise 1
1) write a function which accepts an integer value as input, and returns an integer which
is the cube of that input.
Cube
int
int y 3
2.2 Exercise 2
Write a function that accepts a char as input and returns true if the char is a digit from 0
to 9 or false if the character is not a digit from 0 to 9.
ch
char
IsDigit
bool true if ch is 0 to 9
false if ch is not 0 to 9
2.3 Exercise 3
Write a main function that calls the Cube function from exercise 1. Have it calculate the
cube of the numbers from 1 to 10 and display them. int Cube (int y)
//function prototype
int main ()
{
for (int i = 1; i <= 10; i++) {
cout << i << cubed is << Cube (y) << endl;
}
return 0;
}
2.4 Exercise 4
Write a main function that calls the IsDigit function from Exercise 2. Have it read in 10
input characters from a user and for each one, display whether it is a digit or not.
bool IsDigit (char ch); // function prototype
// function prototype
int main ( )
{
char inputChar;
for (int i = 1; i <= 10; i++) {
cout << Enter character: ;
cin >> inputChar;
if (IsDigit (inputChar))
cout << inputChar << is a digit << endl;
else
cout << inputChar << is not a digit << endl;
}
return 0;
}
2.5 Exercise 5
Write a function named Smallest that takes three integer inputs and returns an integer that
is the smallest of the three inputs. Write the prototype for the Smallest function. Write a
program that gets 3 integers from a user and displays the smallest.
// function prototype
int main () {
int x, y, z;
cout << Enter three integers ;
cin >> x >> y >> z;
cout << The smallest integer is: << Smallest (x, y, z) << endl;
return 0;
}
int Smallest (int a, int b, int c) {
int smallest = a;
if (smallest > b)
smallest = b;
if (smallest > c)
smallest = c;
return smallest;
}
2.6 Exercise 6
Write a function that, given a letter of the alphabet, returns true if the letter is a vowel
(lower or uppercase) and returns false if the letter is not a vowel.
IsAVowel
letter
(char)
Also write the prototype for IsAVowel.
Write a program to invoke the IsAVowel function. Inputs a letter and prints out whether it
is or is not a vowel.
//function prototype
int main ( ) {
char ch;
cout << Enter a letter: ;
cin >> ch ;
if (IsAVowel (ch))
cout << ch << is a vowel << endl;
else
cout << ch << is not a vowel << endl;
return 0;
}
bool IsAVowel (char ch) {
switch (ch) {
case a: case A: case e: case E: case i: case I:
case o: case O: case u: case U:
return true;
}
return false;
}
2.7 Exercise 7
1) Find the error in each of the following program segments and explain how to fix it.
a) int sum (int x, int y) {
int result;
result = x + y;
}
if (0 == n)
return 0;
else
n = n + n;
}
c) in main program:
double x = 1E10;
cout << "square of 1E10 = " << square (x) << endl;
int square (int x)
return x * x;
}
2.8 Exercise 8
Find the error in the following function and fix it.
void displayErrorMessage (int errorNumber)
{
switch (errorNumber) {
case 0: cout << "Fatal Error!" << endl;
break;
case 1: cout << "Error!" << endl;
break;
default: cout << "Invalid error code" << endl;
}
return true;
}
3 Lecture 11 Exercises
Change the GetMinAndMax function (see below) so that it validates the
users input. Only integers between 0 and 1000 are allowed
(including 0 and 1000).
If invalid input is detected, GetMinAndMax stops processing input and returns false to
the caller.
If GetMinAndMax successfully processes the input and calculates the minimum and
maximum values, it returns true to the caller.
int main() {
int minimum = 0, maximum = 0;
GetMinAndMax (minimum, maximum);
cout << "Minimum value is " << minimum << endl;
cout << "Maximum value is " << maximum << endl;
return 0;
}
void GetMinAndMax (int &min, int &max) {
int input;
min = INT_MAX;
max = INT_MIN;
for (int i = 0; i <= 5; i++) {
cout << Enter an integer: ;
cin >> input;
if (min > input)
min = input;
if (max < input)
max = input;
}
}
}
}
3.2 Exercise 2
1) Change the main function so that it calls your new
version of GetMinAndMax properly
3.3 Exercise 3
Fix the error in the following program segment
void sum (int n) {
if (0 == n)
return 0;
else
n = n + n;
}
3.4 Exercise 4
3) Rewrite the following function prototype to return the result
as a parameter instead of as a return value
int Square (int y);
3.5 Exercise 6
Write a C++ function that satisfies the following pre and post
conditions:
/*
* IsUppercase
*
* PRE: Input character ch. Function will determine if this
*
is an uppercase letter or not.