CSCI 102 - Fall 2023 Fundamentals of Computation

HW 8

You will write a program that utilizes nested loops.

Writing Your Code

There are 2 parts to this assignment. NOTE: No skeleton code is provided for this assignment. You will write your code from scratch. You should name your files primes.cpp (part 1) and grid.cpp (part 2). Each will be submitted through Vocareum. We recommend you use the web-based editor and compiler available through Vocareum.

Program 1 - Prime numbers between m and n

Description & Requirements

In this program, the user will enter two positive integers, m and n, with value 2 or more. Your job is to find all prime numbers in the range [m to n] - (inclusive) and output each one, in order of smallest to largest on a separate line. You do not need to provide prompts asking the user to enter m or n if you do not want; just use cin to get the numbers. You must output the prime numbers in order (1 per line). If there are no prime numbers in the range, or the range is empty (i.e. the user entered an m value that is greater than n) you should output one line with the text No primes in range to the screen followed by a newline.

Hints: To determine if a number is prime we must ensure it has no other factors other than 1 and itself. Try to answer these two questions to come up with your code:

  1. How would I test if a number i is a factor of x?
  2. Given a number, x, in what range of values must possible factors be?

Example 1

If the user entered:

2 7

Your program should output exactly the lines:

2
3
5
7

Example 2

If the user entered:

2 30

Your program should output exactly the lines:

2
3
5
7
11
13
17
19
23
29

Example 3

If the user entered:

20 15

Your program should output exactly the lines:

No primes in range

Be sure to indent your code correctly and add comments describing major chunks of your code.

Idioms Used

Study and consider how to use some or all of the following idioms:

Submitting your code

When you believe your program is finished login to Vocareum, start the HW8 assignment (Primes Part), and in the upper-left of the resulting window, click New..File. A textbox will appear where you can type in the filename (leave the work/ portion there and) just type primes.cpp. In the left window pane you should now see primes.cpp appear (it must be named primes.cpp) and you can click on it to open it. A blank window should appear on the right. You can write your program in that windows. You can click the Run or Submit buttons to test your program and submit it, respectively.

If you’ve used some other editor, cut/paste your code from your other tab/window to this window. Alternatively, rather than clicking New..File, you can just use the Upload button to upload the file if you have it on your VM (but the file must be named primes.cpp)..

When you click Submit it should run a few automated tests to let you know if it compiled and passed some basic sanity checks. Look at the output to ensure things are working. If not, you’ll need to modify your code and resubmit.

Be sure you click submit otherwise your code will just be saved, but not submitted!!

Please post any questions about using Vocareum on EdStem.

Program 2 - Grid

Description & Requirements

In this program, the user will enter two positive integers (i.e. 0 or more), m and n. Your job is to print a grid pattern to the screen that has m rows and n columns of cells. (See below for an example to help the following requirements make sense.) A cell is defined to be two spaces (i.e. " " ). These cells should be separated with vertical and horizontal borders. The vertical borders should be made with | characters while the horizontal borders should be made with a dash character (-). At the intersection of a vertical and horizontal border, the vertical border (|) should be printed and not the horizontal. A grid with 0 rows or columns should produce no output.

Please do not prompt the user with a cout as that will confuse our checker script.

Example 1

If the user entered:

6 6

Your program should output exactly the lines:

|--|--|--|--|--|--|
|  |  |  |  |  |  |
|--|--|--|--|--|--|
|  |  |  |  |  |  |
|--|--|--|--|--|--|
|  |  |  |  |  |  |
|--|--|--|--|--|--|
|  |  |  |  |  |  |
|--|--|--|--|--|--|
|  |  |  |  |  |  |
|--|--|--|--|--|--|
|  |  |  |  |  |  |
|--|--|--|--|--|--|

Example 2

If the user entered:

12 3

Your program should output exactly the lines:

|--|--|--|
|  |  |  |
|--|--|--|
|  |  |  |
|--|--|--|
|  |  |  |
|--|--|--|
|  |  |  |
|--|--|--|
|  |  |  |
|--|--|--|
|  |  |  |
|--|--|--|
|  |  |  |
|--|--|--|
|  |  |  |
|--|--|--|
|  |  |  |
|--|--|--|
|  |  |  |
|--|--|--|
|  |  |  |
|--|--|--|
|  |  |  |
|--|--|--|

Example 3

If the user entered:

1 0

Your program should output nothing.

Be sure to indent your code correctly and add comments describing major chunks of your code.

Idioms Used

Study and consider how to use some or all of the following idioms:

Submitting your code

When you believe your program is finished login to Vocareum, start the HW8 assignment (Grid Part), and in the upper-left of the resulting window, click New..File. A textbox will appear where you can type in the filename (leave the work/ portion there and) just type grid.cpp. In the left window pane you should now see grid.cpp appear (it must be named grid.cpp) and you can click on it to open it. A blank window should appear on the right. You can write your program in that windows. You can click the Run or Submit buttons to test your program and submit it, respectively.

If you’ve used some other editor, cut/paste your code from your other tab/window to this window. Alternatively, rather than clicking New..File, you can just use the Upload button to upload the file if you have it on your VM (but the file must be named grid.cpp)..

When you click Submit it should run a few automated tests to let you know if it compiled and passed some basic sanity checks. Look at the output to ensure things are working. If not, you’ll need to modify your code and resubmit.

Be sure you click submit otherwise your code will just be saved, but not submitted!!

Please post any questions about using Vocareum on EdStem.