2010 - Homework #5

Functions

Write your programs in your 2010/5 folder on Odin.

Do not use any goto.
No global variables.

Level of difficulty
-------------------
   part-1  difficult
   part-2  easy      <---start there
   part-3  medium
Part-1

Name your program: lab5a.cpp

Display the first 100 prime numbers.

Start with the exact program below.
You may copy and paste it into your editor.
Do not change the main() function at all.

This program requires the use of a static local variable.

#include <iostream> using namespace std; int get_next_prime_number(); int main() { cout << "Lab5a - get 100 prime numbers.\n" << endl; for (int i=1; i<=100; i++) cout << get_next_prime_number() << " "; cout << "\n\nEnd of program!\n" << endl; return 0; }
Your job is to complete the function named: get_next_prime_number. Look at the psuedo-code below. We will review it on Friday. Use the psuedo-code to find a prime number. You need one more loop because you are trying various numbers such as 8, 9, 10, 11 and you will find 11 is prime. Return 11, then make sure your static local variable remembers the last prime number that it found was 11. Next time you call the function, it will start at 12. Program output looks like this...
Lab5a - get 100 prime numbers. 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 End of program!
Here is pseudo-code for testing if a number is prime. Complete function pseudo-code is now shown below. n is number to be tested prime is a boolean variable (arrow) is an assignment mod modulo operator % n ← 1 prime ← false do while prime is false n ← n + 1 prime ← true div ← 2 do while div is less than n result ← (n mod div) if result equals 0 prime ← false end-if div ← div + 1 end-do end-do return n
Here's how to get this program done... 1. Start the program coding. 2. Do not change the function header, prototype, or main(). All coding is done inside the function. 3. You may use a for-loop, while loop, or do-while loop. 4. Use the pseudo-code to build your test for prime. 5. Do not optimize this program. This is a difficult assignment. Do as much as you can. If you get stuck, email Gordon.
Hints: 1. Your get_next_prime_number() function will have a static local variable. 2. Your static local variable will be initialized. maybe like this... static int n = 2; or static int n = 1; 3. To test your function, initialize your static variable like this: static int n = 1000000; You should see the next 100 prime numbers starting at 1-million. It will look like this... Calculation of 100 primes can be slow. To see the progress as it's working, like above, add flush to your cout. cout << prime_number << " " << flush;
Part-2

Celsius Temperature Table
-------------------------

Name your program 2010/5/ctable.cpp

The formula for converting a temperature from Fahrenheit to Celsius is

  C = (F - 32) x (5 / 9)

where F is the Fahrenheit temperature and C is the Celsius temperature.

Write a program with a function named toCelsius that accepts a Fahrenheit
temperature as an argument. The function will return the temperature as
Celsius.

Call the function from main().

Demonstrate the function by calling it in a loop that displays a table of the
Fahrenheit temperatures 0 through 20, and shows their Celsius equivalents.

Hints:
  Use a for-loop that declares Fahrenheit as the iterator.
  You may name the Fahrenheit variable F, like in the formula.
  F will be a double type.

Please...
• Perfect code style.
• Perfect indenting.
• Very neat output.


Sample of program output. Yours can look even better.
Celsius Temperature Table ------------------------- Fahrenheit Celsius ---------- ------- 0.000 -17.778 1.000 -17.222 2.000 -16.667 3.000 -16.111 4.000 -15.556 5.000 -15.000 6.000 -14.444 7.000 -13.889 8.000 -13.333 9.000 -12.778 10.000 -12.222 11.000 -11.667 12.000 -11.111 13.000 -10.556 14.000 -10.000 15.000 -9.444 16.000 -8.889 17.000 -8.333 18.000 -7.778 19.000 -7.222 20.000 -6.667
Part-3

Choose one program from the end of Chapter-6 in our textbook
------------------------------------------------------------

Name your program 2010/5/lab5b.cpp

Do not choose #7, #12, #22, or #23.

Choose a challenging one please.
If too easy, your score could be lower.

For input, you may use random numbers where possible.

1. Markup
2. Rectangle Area - Complete the Program
3. Winning Division
4. Safest Driving Area
5. Falling Distance
6. Kinetic Energy
7. Celsius Temperature Table
8. Coin Toss
9. Present Value
10. Future Value
11. Lowest Score Drop
12. Star Search
13. Days Out
14. Order Status
15. Overloaded Hospital
16. Population
17. Transient Population
18. Paint Job Estimator
19. Using Files-Hospital Report
20. Stock Profit
21. Multiple Stock Sales
22. isPrime Function
23. Prime Number List
24. Rock, Paper, Scissors Game

Your program should have must have...
  1. Perfect code styling.
  2. Consistent code indenting.
  3. Functions written below the main() function.
  4. Function prototypes above main().
  5. Beautiful output to the screen.