Skip to main content

Exercises (Part 2)

Now that you know the theory, it's time for some practice!

In this section you'll find a collection of exercises on what we have done since the last training (the generation of random numbers with C++). For each exercise one or multiple solutions are provided, but try to make the program yourself first. Sometimes there will be and hint to help you if you are struggling.

For simplicity, we will (almost always) assume for these exercises that the input of the user is valid. If you want to handle the case of invalid inputs, you may want to use a do-while loop. Also, all exercises (except for the first) require the use of dynamic random numbers generation (using srand()), so avoid static generation.

A difficulty is assigned to each exercise using a 3-star rating. The difficulty is calibrated for beginners. Keep in mind that it is a subjective parameter, therefore may not be accurate.

Pseudorandom number generation (★☆☆)​

Write a program to generate 5 random numbers between 1 and 10 statically, then convert the program to use the srand() function, generating dynamic random numbers.

Show solution(s)
prng-static.cpp
#include <iostream>
#include <cstdlib>
#define min 1
#define max 10
using namespace std;

int main() {
int n;

for (int i = 1; i <= 5; i++) {
n = rand() % (max - min + 1) + min;
cout << "Number " << i << " generated: " << n << endl;
}

return 0;
}

Greatest and smallest number (★☆☆)​

Write a program that generates dynamically two numbers, then prints out what is the greatest and smallest of the two.

Show solution(s)
greatest-and-smallest-number.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#define min 1
#define max 10
using namespace std;

int main() {
srand(time(NULL));

int num1 = rand() % (max - min + 1) + min,
num2 = rand() % (max - min + 1) + min;

if (num1 > num2) {
cout << "The number" << num1 << " is the largest" << endl;
cout << "The number" << num2 << " the smallest" << endl;
} else if (num1 == num2) {
cout << "The numbers " << num1 << " and " << num2 << " are equal" << endl;
} else {
cout << "The number " << num2 << " is the largest" << endl;
cout << "The number " << num1 << " the smallest" << endl;
}

return 0;
}

Greatest number (★☆☆)​

Write a program that generates 10 random numbers between 5 and 30, then determines the greatest among them.

Show solution(s)
greatest-number.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#define min 5
#define max 30
using namespace std;

int main() {
srand(time(NULL));

int num, maximum = 5;

for (int i = 0; i < 10; i++) {
num = rand() % (max - min + 1) + min;

cout << "Number " << i + 1 << " generated: " << num << endl;

if (num >= maximum)
maximum = num;
}

cout << "The maximum number generated is: " << maximum << endl;

return 0;
}

Heads or tails (★☆☆)​

Write a program that simulates the toss of 3 coins, prints out the results and counts how many heads and tails came out.

Show solution(s)
head-or-tails.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#define min 0
#define max 1
using namespace std;

int main() {
srand(time(NULL));

int coinTosses = 3,
headsCount = 0,
x;

for (int i = 0; i < coinTosses; i++)
{
x = rand() % (max - min + 1) + min;
if (x == 0)
{
cout << "Heads" << endl;
headsCount++;
}
else {
cout << "Tails" << endl;
}
}

cout << "\nNumber of heads: " << headsCount << endl;
cout << "Number of tails: " << (coinTosses - headsCount) << endl;

return 0;
}

Perfect square (★☆☆)​

Write a program that generates 3 positive random numbers between 1 and 10 (extremes included), then prints out whether their sum is a perfect square or not.

Show solution(s)
perfect-square.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#define min 1
#define max 10
using namespace std;

int main() {
srand(time(NULL));

int a = rand() % (max - min + 1) + min;
int b = rand() % (max - min + 1) + min;
int c = rand() % (max - min + 1) + min;

int sum = a + b + c;

cout << "Generated numbers:" << endl << a << endl << b << endl << c << endl;
cout << "\nSum: " << sum << endl;

float r = sqrt(sum);
int rad = r;

if (r == rad)
cout << "Perfect square." << endl;
else
cout << "Not a perfect square." << endl;

return 0;
}

Count multiples (★☆☆)​

Write a program that generates n positive random numbers (let the user choose n) in a non-specified range and counts how many numbers are multiples of 3, printing them on the console.

Show solution(s)
count-multiples.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
srand(time(NULL));

int n, count = 0;

cout << "How many numbers you want to generate?" << endl;
cin >> n;

for (int i = 1; i <= n; i++) {
int val = rand();
if (val % 3 == 0)
{
cout << "Multiple number of 3: " << val << endl;
count++;
}
}

if (count == 0)
cout << "No multiple number of 3 was generated." << endl;
else
cout << count << " numbers multiple of 3 have been generated." << endl;

return 0;
}

Random average (★☆☆)​

Write a program that generates 20 random numbers in the interval [5, 30], calculates the average and counts how how many numbers are greater than 20, printing all information on the console.

Show solution(s)
rand-average.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#define min 5
#define max 30
using namespace std;

int main() {
srand(time(NULL));

int n, sum = 0, count = 0, i;
float average;

for (i = 0; i < 20; i++) {
n = rand() % (max - min + 1) + min;
cout << " Generated number: " << n << endl;

if (n > 20)
count++;

sum += n;
}

average = (float)sum / i;
cout << "\nAverage: " << average << endl;
cout << "There are "<< count << " numbers greater than 20" << endl;

return 0;
}

Dice roll (★☆☆)​

Write a program that simulates the roll of a six-sided dice (where each side has a unique number) for n times, showing the results and how many times the number 6 came out.

Show solution(s)
dice-roll.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#define min 1
#define max 6
using namespace std;

int main() {
srand(time(NULL));
int x, count = 0, y;

cout << "How many times do you want to roll the dice?" << endl;
cin >> x;

for (int i = 0; i < x; i++) {
y = rand() % (max - min + 1) + min;

cout << "The extracted number is: " << y << endl;

if (y == 6)
count++;
}
cout << "The number 6 came out " << count << " times." << endl;

return 0;
}

Vowels counter (★★☆)​

Write a program that generates 3 uppercase characters of the English alphabet and prints how many of these characters are vowels on the terminal.

Show solution(s)
vowels-counter.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#define min 65 // see ASCII table
#define max 90 // see ASCII table
using namespace std;

int main() {
srand(time(NULL));
char lett1, lett2, lett3;

// a char in C/C++ is already a number (the character's ASCII code)
// so there's no need to explicitly make a conversion
lett1 = rand() % (max - min + 1) + min;
lett2 = rand() % (max - min + 1) + min;
lett3 = rand() % (max - min + 1) + min;

cout << lett1 << endl;
cout << lett2 << endl;
cout << lett3 << endl;

int count = 0;

if (lett1 == 'A' || lett1 == 'E' || lett1 == 'I' || lett1 == 'O' || lett1 == 'U') {
count++;
}
if (lett2 == 'A' || lett2 == 'E' || lett2 == 'I' || lett2 == 'O' || lett2 == 'U') {
count++;
}
if (lett3 == 'A' || lett3 == 'E' || lett3 == 'I' || lett3 == 'O' || lett3 == 'U') {
count++;
}

cout << "There are " << count << " vowels among the generated letters" << endl;

return 0;
}

Guess the number (★☆☆)​

Write a program to generate a random number between 1 and 20 that the user has to guess. Give the user some hints after their attempts, revealing if the generated number is greater or smaller than the one entered on the console. Write a variation of the program where there are only 3 attempts to guess the number, but reduce the range to [1, 10].

Show solution(s)
guess-the-number.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#define min 1
#define max 20
using namespace std;

int main() {
srand(time(NULL));

int attempt, count = 0;
int n = rand() % (max - min + 1) + min;

cout << "Secret number generated. Now guess it!" << endl;

do {
cout << "Enter a number from 1 to 20: " << endl;
cin >> attempt;
count++;

if (n != attempt && n < attempt)
cout << "Attempt failed, try with a lower number." << endl;
else if (n != attempt && n > attempt)
cout << "Attempt failed, try with a higher number." << endl;
} while (attempt != n);

cout << "You guessed in " << count << " attempts" << endl;

return 0;
}

Prime numbers (★★☆)​

Write a program that generates 10 numbers in the interval [10, 100] (extremes included) then counts how many prime numbers have been generated.

Show solution(s)
prime-numbers.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#define min 10
#define max 100
using namespace std;

int main() {
srand(time(NULL));

int num, count = 0;

for (int i = 1; i <= 10; i++) {
num = rand() % (max - min + 1) + min;

cout << i << " Number generated: " << num << endl;

int countDividers = 0;

for (int j = 1; j <= num; j++)
if (num % j == 0)
countDividers++;

if (countDividers == 2)
count++;
}

cout << count << " prime numbers have been generated." << endl;

return 0;
}

Dividers (★★☆)​

Write a program that generates 10 numbers in the interval [10, 50] (extremes included) then prints out all the dividers (those without the remainder) of the generated number.

Show solution(s)
dividers.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#define min 10
#define max 50
using namespace std;

int main() {
srand(time(NULL));

int num = rand() % (max - min + 1) + min;
cout << "Generated number: " << num << endl;

for (int i = 1; i <= num; i++) { // you can't start from 0!
if (num % i == 0) {
cout << i << " is a divider of " << num << endl;
}
}

return 0;
}