Lab: Algorithms

Learning Objectives

Exercise 1: Detecting prime numbers (10 points)

Write a console program that asks the user to enter an integer greater than 1. The program determines if the number entered is prime and writes a message to standard output stream stating whether the number is prime or not. For example, if the user enters 42, the program displays "42 is not prime." If the user enters 47, the program displays "47 is prime."

Develop 8 test cases and verify that your program runs correctly for them. The test cases should contain some prime numbers as well as non-prime values. Include test cases that try to break your program so that you are sure you have implemented the code correctly.

Include the 8 test cases within a multi-line comment near the top of your source code file. Make sure the comment is nicely organized and easy to read and understand.

Exercise 2: Listing prime numbers (10 points)

Write a console program that reads a positive integer entered by the user. The program computes and displays all the prime numbers between 2 and the number entered.

To solve this problem, you need to nest an inner loop within an outer loop.

Develop test cases and include them in a multi-line comment.

Exercise 3: Factoring numbers (10 points)

Write a console program that asks the user to enter a positive integer greater than 1. The program computes the prime factors of the number and prints them. For example, if the user enters 100, the program displays 2 2 5 5.

Develop 8 test cases and verify that your program runs correctly for them. The test cases should contain some prime numbers as well as non-prime numbers. Include tests for edge cases, such as the numbers 2 (the first prime) and 4 (the first non-prime). Develop test cases that try to break your program so that you are sure your code is correct.

Include a description of your test cases in a multi-line comment near the top of your source code. Make sure the comment is nicely organized and easy to read and understand.

Exercise 4: Generating random colors (10 points)

Write a program that generates a different list of 100 random RGB color values using hexadecimal notation. The program should write something like the following into cout.

324AF3
6A3125
... 98 additional RGB values

To solve this problem you need to generate strings with 6 characters where each character is randomly chosen from the following set of symbols.

{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }

To make the choice random, you need to use the modulo operator (%). Specifically, use the expression rand()%16 to get a random integer between 0 and 15.

You are required to solve this problem by defining a loop within a loop. In fact, with the language features of C++ covered until this point, the solution to this problem requires a loop within a loop. If you know how to solve the problem without a nested loop, then you are required to solved it with a nested loop anyway, because learning how to write nested loops is the main learning objective of this exercise.

The program should generate a different list of color values each times it runs. To get this result, you need to seed the random number generator with a new value each time the program runs. One easy way to do this is to use the current time in seconds as a seed value. The following code shows how to do this.

#include <cstdlib>

...

main()
{
    srand(time(0));

...