CSCI 102 - Fall 2023 Fundamentals of Computation

HW 6

You will write a program that utilizes while and if statements.

Writing Your Code

There are 2 parts to this assignment. Each will be submitted through Vocareum. We recommend you use the web-based editor and compiler available through Vocareum.

When you start each part of the assignment in Vocareum you will find a starter (“skeleton”) file for that part where you should write your program.

Program 1 - Difference

Description & Requirements

In this program you should ask the user to repeatedly enter double values and count how many times 2 consecutive values differ by at most 1. Your program should quit when the user enters 0 (and need not consider it). Before you exit, print the number of consecutive values that differed by at most 1 on the last line of output. You do not know how many numbers will be entered by the user so process only one number at a time and then iterate. You can output a prompt each time you want to ask the user to enter a number or just do a cin without any prompt.

Example 1

If the user entered:

1
2.0
4.5
4.0
0

Your program should output 1 line at the end of the program that reads:

2

This is the correct output because only 1 and 2.0 along with 4.5 and 4.0 differed by at most 1. 2.0 and 4.5 differed by more than 1.

Example 2

If the user entered:

1
1.7
0.8
-0.1
-1
0

Your program should output 1 line at the end of the program that reads:

4

This is the correct output because only 1 and 1.7 differ by at most 1 as does 1.7 and 0.8, 0.8 and -0.1, and -0.1 and -1. We do not check whether 0 is within 1 of the previous number because 0 is the input that causes us to exit.

Example 3

If the user entered:

0

The correct output should just be:

0

Idioms Used

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

Other Tips

Writing Your Code with Vocareum

Write your program using the Vocareum editor (it will save automatically when you click outside of the editor window). Be sure to indent your code correctly and add comments describing major chunks of your code.

When you’re ready to test it, you can:

Repeat the build and run process until you believe your code works. Then click the Submit button. This will submit your code. You are allowed to submit as many times as you like. When you click Submit, our scripts will automatically run a few (not all) of our grading test cases through your program and report the results. Look at those results to ensure your code is passing these basic tests. If everything passes, you are done! Your code is submitted. If something fails, you can simply go back edit, build, run, and submit again (as much as you like).

If you have submitted on Vocareum at this point you are done with Part 1!

Writing Your Code with Another Website

The instructions below are only if you want to use some other editor/compiler to write your code. To develop your code you can use any text editor/compiler you like or the web-based editors/compilers we have referenced in the past:

Leave your browser window open with your code since it is not permanently saved anywhere.

When you believe your program is finished leave that browser tab/window open and in a new tab/window, login to Vocareum, start the HW6 assignment (Difference 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 diff1.cpp. In the left window pane you should now see diff1.cpp appear (it must be named diff1.cpp) and you can click on it to open it. A blank window should appear on the right. Cut/paste your code from your other tab/window to this window and you can click Submit. 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 in Vocareum or back in your other tab/window and then cut/paste back to Vocareum.

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 - Odd Digits

Background

This program will utilize your knowledge of the decimal number system that was presented back in the first week (Digital Representation). Remember a decimal number a3a2a1a0 (where each ai is a digit in the number) really represents the value given by: (a3 * 10^3) + (a2 * 10^2) + (a1 * 10^1) + (a0 * 10^0). For example: 934 decimal really represents the value (9 * 100) + (3 * 10) + (4 * 1). In the program described below you will need to separate the number into individual digits and process them one at a time. For example, we would want to take 934 and split it into a 4, a 3, and a 9. To do this, we can use a modulo operation to isolate 1 digit and then a division to alter the value so we can find the next digit (and then repeat this process). We will leave it at that and ask you to consider the details yourself.

Description & Requirements

Your program should ask the user to enter three decimal integers: num1, num2, and num3.

Your program should:

Example 1

Running your program with inputs 12008337 936649 77823359 should result in the output:

77823359
12008337
936649

This is because 77823359 has 6 odd digits, 12008337 has 4 odd digits, and 936649 has 3 odd digits.

Example 2

Running your program with inputs 200000 99999 13000 should result in the output:

99999
13000
200000

This is because 99999 has 5 odd digits, 13000 has 2 odd digits, and 200000 has 0 odd digits.

Idioms Used

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

Program Structure

The first challenge is to consider how you can even take a single value and count its odd digits. The Background section above should help give you a clue for how you can use an iterative process to isolate each digit one at a time. A loop will be required.

If you can write code to determine how many odd digits are in a single value then you can simply repeat that code for each of the 3 input numbers. However, we strongly encourage you to write a function which calculates the count of odd digits in a value, then just call that function for each of the three values.

Once you have determined the number of odd digits in each number you can use a sequence of if statement to determine which value has the most, second-most, and least.

Writing Your Code with Vocareum

Write your program using the Vocareum editor (it will save automatically when you click outside of the editor window). Be sure to indent your code correctly and add comments describing major chunks of your code.

When your ready to test it, you can:

Repeat the build and run process until you believe your code works. Then click the Submit button. This will submit your code. You are allowed to submit as many times as you like. When you click Submit, our scripts will automatically run a few (not all) of our grading test cases through your program and report the results. Look at those results to ensure your code is passing these basic tests. If everything passes, you are done! Your code is submitted. If something fails, you can simply go back edit, build, run, and submit again (as much as you like).

If you have submitted on Vocareum at this point you are done with Part 2!

Writing Your Code with Another Website

The instructions below are only if you want to use some other editor/compiler to write your code. To develop your code you can use any text editor/compiler you like or the web-based editors/compilers we have referenced in the past:

Leave your browser window open with your code since it is not permanently saved anywhere.

When you believe your program is finished leave that browser tab/window open and in a new tab/window, login to Vocareum, start the HW6 assignment (Difference 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 odd.cpp. In the left window pane you should now see odd.cpp appear (it must be named odd.cpp) and you can click on it to open it. A blank window should appear on the right. Cut/paste your code from your other tab/window to this window and you can click Submit. 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 in Vocareum or back in your other tab/window and then cut/paste back to Vocareum.

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

Please post any questions about using Vocareum on EdStem.