Homework 3 - switch Statements and Nested Loops

Due Wednesday October 12, 2011 at 5:00pm

Homework Policy Clarification

If you wish me to comment on your assignment and give you a chance to correct any glaring errors, you MUST submit the assignment at least one day ahead of the due date AND resubmit the corrected assignment by the due date. I let this slide a little on Homework 2 since people were confused about this policy.

Since I will be driving on the evening of October 11th, for Homework 3, make sure to submit no later than noon on October 11th if you want comments and an opportunity to correct your assignment.

Assignment

The purpose of this assignment is to use the switch statement and function calls.

Name your files hw3_<problem>.cpp, such as hw3_1.cpp. Email all the cpp files to my Sleipnir account.

  1. (10 points) Update your menu program from Homework 2 Problem 2 to use the switch statement instead of the if-else if-else statement. Make sure your switch statement contains a default statement to cover the case when the user selects an unsupported menu option. You may alter the solution to Homework 2 Problem 2 instead of your code for that assignment if you wish.
  2. (10 points) Use nested for loops to find all prime numbers between 3 and 200. Recall from math that a number is prime if it is NOT evenly divisible (remainder of division is 0) by any number other than itself and 1. The outer loop will count from 3 to 200, keeping track of the number that you are currently testing for "prime-ness". The inner loop will actually test if a number is evenly divisible by numbers smaller than it (e.g. 2 to number-1 where number is the number you are testing for "prime-ness"). The modulo operation (num1 % num2) will be useful here since it returns the remainder. If the remainder is ever 0, the number is not a prime so you can skip it and continue on to testing the next number. Output the number when it is a prime (e.g. the inner loop tested all possible factors and the remainder was never 0). You may find Boolean flags useful for tracking "prime-ness" in the inner loop.