CSCI 102 - Fall 2023 Fundamentals of Computation

HW 7

You will write a program that utilizes for loops.

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.

Program 1 - MinMax

Description & Requirements

In this program, the user will enter an integer, n, (you may assume n will be at least 4 or more) and then proceed to enter n more integers between 00 and 99, inclusive (and you may assume numbers outside this range will NOT be input). Your job is to find and track the minimum (smallest) 2 numbers and maximum (biggest) 2 numbers. After receiving all n integers you should output the smallest, 2nd smallest, 2nd biggest and biggest numbers (in that order) to the screen (1 per line).

Example 1

If the user entered:

4
50 20 10 70

Your program should output 4 lines at the end of the program that reads:

10
20
50
70

Example 2

If the user entered:

10
0 99 90 10 8 92 96 4 2 98

Your program should output 4 lines at the end of the program that reads:

0
2
98
99

Idioms Used

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

Approach

To start, consider writing the program for just finding the min and 2nd min value and ignore the max portion of the program. If you can get the min part working, you can mimic that same approach to also get the max part working.

We can start by considering what variables we need. Clearly we need a 1st min and 2nd min and likely a variable for receiving input. Beyond that, it is not clear so we can wait until we code further and potentially discover the need for another variable. Note that because the computer process 1 item at a time, these variables will store the 1st min and 2nd min thus far (i.e. of the values seen up to this point in time). The idea is that we will replace their values as we encounter better input values.

With a little thought you will see that you can decompose the task into an initialization portion of the code and then the regular iterative/steady-state (loop) portion. Let’s focus on the iterative/looping part first. Unless there is some good reason, you should generally process 1 input value per loop iteration. So let’s assume we have initial values for min and 2nd min, and we get a new input value. What are the cases we should check regarding the relationship between the min, 2nd min, and new input value. In each case, how would we want to update our variables (consider the Shifting idiom linked above). You can consider writing code (or pseudocode) for these cases and actions.

To consider initialization, we can often take one of a few approaches:

Each of these approaches can work but may come with potential issues if you do not take care.

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 HW8 assignment (MinMax 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 minmax.cpp. In the left window pane you should now see minmax.cpp appear (it must be named minmax.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 - Vowels

Description & Requirements

This program will utilize your knowledge of ASCII characters and loops. In this program, the user will enter an integer, n, (n will be 1 or more) and then proceed to enter n characters (no spaces). These characters may be a mix of uppercase, lowercase, digits, and punctuation. Your job is to track how many capital (upper-case) characters are entered and how many vowels (a, e, i, o, or u either lower- or upper-case) characters are entered. After receiving all n characters, you should output the number of capitals/upper-case characters that were entered on one line and then the number of vowels on the last line of your output. Just output these 2 numbers on the last two lines of your program output.

Note: Do not use string variables. Just use an int and a char variable and read in 1 character at a time from cin.

Example 1

If the user enters the following input:

4
Aa7U

Your program should produce the following output:

2
3

This is because there are 2 capitals/upper-case letters in Aa7U and 3 vowels.

Example 2

If the user enters the following input:

10
A%EeIiOobB

Your program should produce the following output:

5
7

This is because there are 5 capitals/upper-case letters in A%EeIiOobB and 7 vowels.

Idioms Used

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

Hints

1. Remember ASCII character ranges are contiguous/sequential, meaning if 'A' is ASCII 65 decimal then 'B' woudl be 66 and 'C' would be 67.

2. You never need to hard code ASCII numbers, instead if you want the ASCII code for A just write 'A'.

3.: You can perform arithmetic or comparison on characters just as you can with integers.

4.: There are some helpful library functions you may use in the cctype header (i.e. #include <cctype> ). Perform an internet search to learn more but tolower() and toupper() could be useful.

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 login to Vocareum, start the HW8 assignment (Vowels 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 vowels.cpp. In the left window pane you should now see vowels.cpp appear (it must be named vowels.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.