A4 Loops - sheerazwalid/COMP-I GitHub Wiki

Assignment Folder

The source code files for this assignment should be placed in a folder named loops in your Cloud9 workspace. The files should be named as ex1.cpp for exercise 1, ex2.cpp for exercise 2, and so on.

Exercise 1: 'For' Loops (10 points)

Write a C++ program that prints every integer between 330 and 550, inclusive.

Hint: use a for loop.

Exercise 2: Guess-the-Number Game (10 points)

Write a program that implements Guess-the-Number game. The program should enter a loop that starts by printing "What is the number?" After printing this, it reads the user response. (Use cin >> n to read the user response.) If the user enters a value less than 1776, the program prints "too small" and continues the loop. If the user enters a number larger than 1776, the program prints "too big" and continues the loop. If the user enters the number 1776, the program prints "you got it" and then terminates.

Exercise 3: Random integers (10 points)

Write a program that generates a list of 100 random integers in [0, 100), which means between 0 and 99 inclusive.

To solve this problem, use the randomInteger function you developed for the A3 Functions assignment. In other words, use a loop to call the randomInteger function 100 times.

Save the program output by redirecting the standard output stream to a file. To do this, run the program as follows.

./a.out > numbers.txt

Open numbers.txt in a text editor to see the result.

Exercise 4: 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. Here is an example:

/*
   n       result
  ---    ------------
    2    prime
    3    prime
    4    not prime
...
*/

Exercise 5: Detecting prime numbers with functions (10 points)

Implement a function that takes an integer value and returns true if the integer is a prime number and false otherwise. The function should have the following signature.

bool isPrime(int n)

Include the function in a console program that calls the isPrime function with a range of values that test several common cases as well as the boundary cases. Use an assert statement for each test case.

Exercise 6: 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.

Submit Your Work

Submit your work by sending the url of your Cloud9 workspace to the teaching assistant and CC the instructor. The subject line of your email should be 201 Assignment: Loops.

Example quiz questions

  1. Any problem from the above exercises.

  2. When the following code runs, what does it print?

int i = 3;
while (i < 27) {
    cout << i + 1;
    i = i * 3;
}
  1. When the following code runs, what does it print?
int n = 10;
for (int i = 0; i < 40; ++i) {
    n = n + 3;
}
cout << n + 2;
  1. When the following code runs, what does it print?
int i = 5;
while (i > 0) {
    i = i - 2;
}
cout << i;
  1. When the following code runs, what does it print?
int i = 4;
for (int k = 0; k < 300; ++k) {
    i = i + 3;
}
cout << i;
  1. When the following code runs, what does it print?
int i = 66;
while (i > 3) {
    i = i / 2;
}
cout << i;
  1. Write code that prints 220 random integers that are greater than or equal to 0 and strictly less than 200.

  2. Write code that computes the sum of integers 100 through n, inclusive, where n is an integer greater than 100. In other words, compute 100 + 101 + ... + n. Print the sum that you compute.

  3. Implement the guess-the-number game.