Revision Problems
Revision Problems
3. Write a C++ program that takes the radius of base and height of a cylinder to compute and display area
of base and volume of the cylinder rounded to the nearest hundredth according to the following
formulas:
Area of base = π x Radius2
Volume of cylindar = π x Radius2 x Height
4. In a laboratory, the time of an experiment is measured in seconds. Write a C++ program to enter the
time as an integer number of seconds, convert and print out it as a complete integer number of hours,
complete integer number of minutes and remaining seconds (if any).
5. The Pythagorean Theorem states that the sum of the squares of the sides of a right triangle is equal to
the square of the hypotenuse. For example, if two sides of a right triangle have lengths 3 and 4, then
the hypotenuse must have a length of 5. The integers 3, 4, and 5 together form a Pythagorean triple.
There is an infinite number of such triples. Given two positive integers, m and n, where m > n, a
Pythagorean triple can be generated by the following formulas.
Side1 = m2- n2
Side2 = 2mn
Hypotenuse = √𝑺𝒊𝒅𝒆𝟏𝟐 + 𝑺𝒊𝒅𝒆𝟐𝟐
Write C++ program that reads in values for m and n and prints the values of the Pythagorean triple
generated by the formulas above.
for (int k = 1; k < 5; k++) int a = .., b = .., c = ..; // a, b, and c are assigned some values
{ for (int j = 1; j < 5; j++) if ((a == b) && (a != b ))
switch ( k % 3 ) cout << “It is the first case!!”<< endl;
{ case 0: else
cout << setw(2) << if ((c == b) || (c != b ))
‘@’; cout << “It is the second case!!”<< endl;
break; else
case 1: cout << “It is the third case!!”<< endl;
cout << setw(2) << j;
break;
default:
cout << setw(2) <<
‘#’;
}
cout << endl;
}
int num = 182348, s = 9, g = 0, d;
const int ten = 10;
do
{ d = num % ten;
if ( d < s )
s = d;
if ( d > g )
g = d;
num = num / ten;
} while ( num != 0 );
cout << “Value 1 = “ << s << endl;
cout << “Value 2 = “ << g << endl;
cout << “Value 3 = “ << num << endl;
8. Write a C++ main function that takes and validates a big integer number between 999 and 999999 to
compute and display the sum and count of its even digits and only the sum of its odd digits.
9. Write only the C++ loop to generate each of the following sequences:
5 7 10 14 15 20 21 25 28 30 35
2 1 1 2 0 2 0 1 1 0 2
10. Write a C++ program that takes and validates a positive integer number between 0 and 99999
(inclusive) to notify the user with the message "ONE SINGLE DIGIT" if the number has only one digit,
"TWO BIG DIGITS" if the number has two digits, "THREE BIG DIGITS", if the number has three
digits, or “MORE AND MORE DIGITS”, if the number has more than three digits.
11. There are 900 people in a town whose population increases by 10 percent each year. Write a C++
program that displays the annual population and determines how many years (countYears) it will take
for the population to pass 20,000.
1 1 0 0 0 1 2 3 4 1 2 4 8
## 0 0 5 6 7 8 16 32 64 128
123 0 1 0 0 0 0 9 10 11
#### 0 0 1 0 0 0 12
12345 0 0 0 1 0 0 13 14 15 16
###### 0 0 0 0
1234567 1 0
######## 0 0 0 0 0 1
III – Arrays
1. Write only the C++ code to create the following array and and fill it with the shown data. Also, the the
code to display the array content as a 2D array.
A X X X X
X A X X X
X X A X X
X X X A X
X X X X A
2. Write only the C++ code to create the following array and and fill it with the shown data. Also, the the
code to display the array content as a 2D array.
A X X X X
A A X X X
A A A X X
A A A A X
A A A A A
13. Write a C++ program to check if a given 3x3 matrix comprises a magic square or not. A magic square
is a square where the sum of all the elements in each row, column and diagonal is the same.
Example
2 7 6 3 4 8
9 5 1 5 7 1
4 3 8 9 2 6
The first square is a magic square as the sum of each row, column and diagonal is equal
(15), while the second is not a magic square as the sum of the first row (15) and the sum
of the second row (13) are not equal.
Your program should ask the user to input all the numbers in your 3x3 matrix, the output
whether the input matrix is a magic square or not.
4. Define a C++ function MaxMin that takes three numbers and returns the largest and smallest ones.
5. Define a C++ function SUM_DIGITS that takes a positive integer number and returns back the sum of
its digits.
6. Define a C++ function EXISTS that takes two integer numbers, the first is a big number and the second
is one single digit. The function returns true if the single digit does exist in the big number and false
otherwise.
7. Write a C++ function Occ that takes two positive integer numbers, the first is a big positive integer number
(BIG) and the second is a single digit d. The function returns the occurrence of the single digit d in the big
number BIG.
8. Define a C++ function Validate_Binary that takes a binary number (composed of 1’s and 0’s) to check
such number. The function returns true if the number is composed only of 0’s and 1’s, and false,
otherwise.
9. Write a C++ function GRADE that inputs a student’s average and returns the letter grade ‘A’ if a student’s
average is 90-100, ‘B’ if the average is 80-89, ‘C’ if the average is 70-79, ‘D’ if the average is 60-69, and
‘F’ if the average is lower than 60. Define the function twice, once using nested-if and second using
switch statement.
10. Write a C++ function that finds and returns the second largest element in an array of doubles.
12. The ancient Greek Euclid developed a method for finding the Greatest Common Divisor (GCD) of two
integers A and B (A greater than B). His method is:
a. If the remainder (R) of A / B is 0, then B is the GCD.
b. If it is not 0, then assign B to A and the remainder (R) to B.
c. Return to step 1 and repeat the process.
Write a modular C++ program that takes two integer numbers N and M and prints out their GCD. The
program should be structured as three functions: the main, swap and GCD. The main function takes two
integer numbers N and M and if N < M it calls the function swap that will interchange the values of N and
M (N is assigned to M and M is assigned to N). Then, the function main calls the function GCD that
receives the values of N and M as A and B, performs Euclid’s process (defined above) and returns the
corresponding GCD value to the main function that prints it out.
Write a recursive version of the function GCD.
5 3 1 2 0 6 2 8
a. Write a C++ recursive function that prints the contents of the array A from location 0 through location
7. Show a sample call for such function
b. Write a C++ recursive function that returns the sum of the contents of the array from location 0
through location 7. Show a sample call for such function.
c. Write a C++ recursive function that returns the number of non-zero elements in the array from
location 0 through location 7. The Show a sample call for such function.
d. Write a C++ recursive function to perform a recursive linear search. It searches for an input number
x in the input array from location 0 through location 7. The function returns the location of x in the
array if found, or -1, otherwise.
14. Write a recursive function, findSum, that calculates the sum of successive integers starting at 1 and ending
at n (that is, findSum(n) = (1 + 2 + 3 + … + (n -1) + n).
Problems 3
15. In Mathematics, factorial of an integer number n is referred to as n! and is defined as follows:
For example:
4! = 4 x !3 = 4 x 3 x !2 = 4 x 3 x 2 x 1! = 4 x 3 x 2 x 1 x 0! Where 0! = 1
Define a recursive function factorial that takes a positive integer number n and returns its
factorial. Test your function factorial for n = 5.
16. Define a recursive function power (Y, n) that computes Yn where Y and n are positive integer numbers.
Consider the following rule:
18. Define a recursive function sumOfArray that takes an array A of size n to compute and return back the
sum of its elements.
Consider the following rule:
Sum of array A of n elements = A[n-1] + sum of array A of (n – 1) elements
int A[10], n = 29, num, index = 0; int getM (int, int, int);
const int two = 2; void main ()
cout << “The “ << setw(6) << n << “ is { int A[9] = {2, 6, 15, 11, 12, 6, 8, 9, 9}, B[3];
now “; for (int k = 2; k < 9 ; k = k+3)
do B[k/3] = getM(A[k-2], A[k-1], A[k]);
{ A[index] = n % two;
n = n / two; cout << “Final Content: “ << endl;
index++;
for ( int c = 0; c < 3; c++ )
} while (n != 0);
cout << B[c] << endl;
for ( k = index-1; k >= 0; k--)
cout << A[k]; system ("pause");
cout << endl << “Done!!” << endl; }
int getM(int a, int b, int c)
{ int M;
if ( a > b )
M = a;
else
M = b;
if ( c > M )
M = c;
return M;
}
int getOCC (int, int); bool myfunc(int n);
void main () void main()
{ int res, BIG = 50003; {
for (int d = 0; d < 10 ; d++) int A[5] = {2, 6, 5, 11, 12};
{ res = getOCC( BIG, d); for (int k = 0; k < 3 ; k++)
If ( res > 0 ) If (myfunc(A[k]))
cout << setw(5) << d << setw(5) << res << endl; cout << setw(4) << A[k];
} cout << endl;
system ("pause"); system ("pause");
} }
bool myfunc(int n)
int getOCC( long M, int n) {
{ int c = 0; const int ten = 10; For ( int c = 2; c <= n / 2; c++ )
do { if (n % c == 0)
if ( M % ten == n ) return false;
c++;
M = M / ten; return true;
} while ( M != 0 ); }
return c;
}
1. An Instructor has recorded the grades of sn exam of his CSCE 1001 section (25 students) in a file
(“score.dat”) of two columns, the first contains the first name of the student (recorded without any
spaces) and the second contains his/her score (integer value ) as follows:
Karim 70
Ahmed 90
Zayed 75
Dina 80
Khaled 95
………. ….
………. ….
Youssef 66
p = new int;
*p = 15.88;
q = new int;
*q = 23.8;
*p = *q;
p = q;
3. Write a C++ program that find the common elements between two dynamic arrays of variable sizes.
Your program should ask the user to input the sizes of the two arrays and the elements in each of them.
The program then should display the common elements that appear in both arrays
Sample Input
4 7 11 3 9
8 1 4 12 10 7 2
Output:
4
7
4. Extend the definition of the counter class by adding a member function compareCounter that
compares two counter objects and returns -1, 0, or 1 depending on whether the count attribute of
the counter object it’s applied to is less than (-1), or equal to (0), or greater than (1), the count
attribute of its counter argument.
5. Design and implement a Dice class . Each instance of this class should have as member variables
the die’s current side (number of dots) and the total number of times it has been rolled. There
should be an accessor member functions for of the two attributes. Two other member functions
roll and reset, are the modifier member functions. roll should use a random number to determine
the current side of a die. Develop the class for dice by writing formal specifications including a
default constructor and test it in a simple tester program.
6. In a graphics program, we need to represent simple graphical objects such as circles, squares,
rectangles, and triangles. We want to be able to position these objects anywhere on the screen and
draw them in various sizes and colors. Besides drawing the objects themselves, we want to display
the object’s characteristics, including the center coordinates, area and perimeter.
a. Design a class that can be used to represent circle objects. The circle class should have data
members to represent the x- and y-coordinates of a circle object’s center as well as the radius, area,
and perimeter. Normally, a user of this class would set values for all of a circle object’s member
variables except the area and perimeter. These attributes can be computed (using member
functions) after the radius is set by the class user.
b. Add a member function to the circle class to compute the distance between the center of
the circle object it’s applied to and the center of a circle argument.
c. Design a class that can be used to represent square objects. The square class should have
data members to represent the x- and y-coordinates of a square object’s center as well as the side
length, area, diagonal, and perimeter. Normally, a user of this class would set values for all of a
square object’s attributes except the area, diagonal and perimeter. These attributes can be
computed (using member functions) after the side length is set by the class user.
1. Given the following basic stack implementation, answer the questions that show below:
#include <iostream>
using namespace std;
class Stack
{
int top;
public:
int a[MAX]; //Maximum size of Stack
Stack();
bool push(int x);
int pop();
bool isEmpty();
};
Stack::Stack(){
top = -1;
}
bool Stack::push(int x)
{
if (top >= (MAX-1))
{
cout << "Stack Overflow";
return false;
}
else
{
a[++top] = x;
cout<<x <<" pushed into stack\n";
return true;
}
}
int Stack::pop()
{
if (top < 0)
{
cout << "Stack Underflow";
return 0;
}
else
{
int x = a[top--];
return x;
}
}
bool Stack::isEmpty()
{
return (top < 0);
}
return 0;
}
2. The following is the code for a queue that is modelled using an array. The queue is designed such
that index 0 of the array is always the front of the queue. Back is a variable that is used to designate
where the end of the queue is. Answer the questions below:
class Queue
{
int back;
public:
int a[MAX]; //Maximum size of Stack
Queue();
bool enqueue(int x);
int dequeue();
bool isEmpty();
void print();
};
Queue::Queue(){
back = -1;
}
bool Queue::enqueue(int x)
{
if (back >= (MAX-1))
{
cout << "Queue Overflow";
return false;
}
else
{
a[++back] = x;
return true;
}
}
int Queue::dequeue()
{
if (back < 0)
{
cout << "Queue Underflow";
return 0;
}
else
{
int x = a[0];
for (int i=1; i<=back; i++){
a[i-1]=a[i];
}
back--;
return x;
}
}
bool Queue::isEmpty()
{
return (back < 0);
}
void Queue::print(){
cout << "Content of queue: ";
...
cout << endl;
}
return 0;
}
a. This queue is designed such that the front of the queue is always at index 0 of the array.
While adding an element to the end of the queue is simple, understand the code above to answer
what the overhead is with removing an element from the queue (dequeue)?
b. Add a function int peek() that simply gives you back the element at the beginning of the
queue without actually dequeueing it from the queue.
c. Add a function int size() that gives you back the number of elements inside the queue
(not the maximum possible size of the queue).
d. Complete the code for the print function above to print the content of the queue without
removing anything from the queue.
______________________________________________________________________________
________________________________________________________________________