Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / CS 2A Object-Oriented Programming in C++ Assignment 5 SYLLABUS ASSIGNMENTS LESSONS SOLUTIONS Assignment 5

CS 2A Object-Oriented Programming in C++ Assignment 5 SYLLABUS ASSIGNMENTS LESSONS SOLUTIONS Assignment 5

Computer Science

CS 2A Object-Oriented Programming in C++ Assignment 5 SYLLABUS ASSIGNMENTS LESSONS SOLUTIONS Assignment 5.1 [30 points] Write a program that asks the user how many numbers will be entered and then has the user enter those numbers. When this is done, report to the user the position of the first 7 entered and the last 7 entered. By position we mean, for example, that if the first 7 is the 2nd number entered then its position would be 2. Turn in the following 3 outputs exactly as you see them below to demonstrate that your program works in each case. Don't forget to use a named constant for the "7", and make sure to use a name that will still make sense if we change the value of the constant to another number such as "8". Sample screen output 1: How many numbers will be entered? 8 Enter num: 5 Enter num: 7 Enter num: 6 Enter num: 7 Enter num: 7 Enter num: 3 Enter num: 8 Enter num: 6 The first 7 was in position 2 The last 7 was in position 5 Sample screen output 2: How many numbers will be entered? 8 Enter num: 5 Enter num: 2 Enter num: 6 Enter num: 7 Enter num: 1 Enter num: 3 Enter num: 8 Enter num: 6 The first 7 was in position 4 The last 7 was in position 4 Sample screen output 3: How many numbers will be entered? 8 Enter num: 5 Enter num: 1 Enter num: 6 Enter num: 5 Enter num: 9 Enter num: 3 Enter num: 8 Enter num: 6 Sorry, no 7's were entered. Hints: Everything you need to know to solve the problem is in the lessons. This is one of those problems that will challenge your problem solving skills. There's nothing hard about the C++ code here. What's hard is solving the underlying problem. One way to think about it would be to forget about computers for a moment, and figure out how you would solve this problem if you are the computer and a friend is the user. Suppose your friend gives you 1,000 numbers, and when she's done, you have to tell her which position the first 7 was in and which position the last 7 was in. Suppose you can't actually remember anything at all, so you have to write down whatever it is that you need to keep track of (this simulates using variables in a computer program). Ask yourself, what do you write down before you start? What do you do each time your friend says the next number? And you can't write down all of the numbers, you can only be keeping track of say 3 - 5 numbers at a time. Another angle if this seems hard would be to instead pretend that you have two friends, one being the computer and one being the user. Your job is to watch your friend who is the computer and explain to her what she needs to do each time a new number is stated by your friend who is the user. Once you've got that problem solved, the process of translating that into C++ code is the easy part. :) Assignment 5.2 [30 points] Write a program that reads an integer and calculates the sum of the squares of the numbers from 1 up to that integer. For example, if the integer 4 is entered, the sum of the squares is 30 (1 + 4 + 9 + 16). The program should repeat this process until the user wants to quit. An input value less than or equal to 0 signals the end of the data. Note: one way to calculate this would be to use the formula n(n+1)(2n+1)/6. You must write this program without using that formula. Sample screen output: Enter a The sum Enter a The sum Enter a number of the number of the number greater squares greater squares greater than from than from than 0 1 0 1 0 (less than 1 to quit): 4 to 4 is 30 (less than 1 to quit): 1 to 1 is 1 (less than 1 to quit): 0 Turn in your source code and one output exactly like the sample given above. Assignment 5.3 [25 points] Write a program that prints out the number of words in textfiles. We will define a word to be any sequence of non-whitespace characters. So "hi&there...mom" would be considered a single word. Solve this problem by reading each word into a string variable. Your program should ask the user for the name of the file to count words in, and then print the number of words in the file on the screen. It should continue to do this until the user types "quit" for the name of the file. You will make your life much easier if you do not use any variables of type char in this assignment. Use variables of type string instead. Turn in your source code and a sample output that has the user entering these 5 input files: file 1 | file 2 | file 3 | file 4 | file 5 Hints about reading input files: Using Visual Studio: 1. right-click on the "Source Files" folder in the Solution Explorer and choose "Add" then "New Item". 2. Then in the "Add New Item" window, navigate to "Utility" and select "Text File (.txt)". This adds a text file to the "Source Files" folder in the Solution Explorer. 3. copy the data from the webpage where it is provided and paste it into this file. Using Xcode: Extensions are part of the file name. If you want to count the words in a file named "myfile.txt", typing "myfile" or "myfile.cpp" won't work. For Windows users, before you start this assignment, I suggest that you make sure that Windows is showing you the complete file name of your files, including the extensions. Windows hides this from you by default. In Windows 10 the procedure is: Start Menu > Control Panel > Appearance & Personalization > File Explorer Options > View > uncheck box "Hide extensions for known file types" > OK In Windows 7 and 8 the procedure is as follows: 1. 2. 3. 4. 5. Choose "control panel" from the start menu Choose "Folder Options" from the list of control panel items. In Windows 8 you may have to type "Folder Options" into the control panel search bar. Choose the "view" tab from the Folder Options window Find the checkbox that says "Hide extensions for known file types". Uncheck that checkbox. Submit Your Work Name your source code file(s) according to the assignment number (a1_1.cpp, a4_2.cpp, etc.). Execute each program and copy/paste the output into the bottom of the corresponding source code file, making it into a comment. Use the Assignment Submission link to submit the source file(s). When you submit your assignment there will be a text field in which you can add a note to me (called a "comment", but don't confuse it with a C++ comment). In this "comments" section of the submission page let me know whether the program(s) work as required. © 1999 - 2021 Dave Harden CS 2A Object-Oriented Programming in C++ Lesson 5: Loops 2 SYLLABUS ASSIGNMENTS LESSONS SOLUTIONS Section 5.1: do-while Loops Have you noticed that since we have been talking about loops only one new construct has been introduced? We first introduced the while loop, and then showed how a while loop is used to write three different types of loops: question- type, special-value-type, and counter-controlled. In this section we introduce a second loop construct. With this new loop construct you will be able to write question-type loops and special-value type loops with a new twist. In all of the question-type and special-value-type loops we have discussed thus far, we have been careful to allow the user the option of not executing the loop even once. Sometimes this is not desirable: there are times when we would like to assume that the user will execute the loop at least once. C++ provides a special loop construct called a do-while loop to handle this situation. The only difference between a while loop and a do-while loop is that in a while loop the condition is checked at the top of the loop, whereas in a do-while loop the condition is checked at the bottom of the loop. The result of this is that the do-while loop is always executed at least once, because C++ doesn't check the condition until execution reaches the bottom of the loop. The general form of the do-while loop is: do { } while (); Notice that the primary difference in appearance between this and the while loop is that the while () part now comes at the bottom instead of at the top. When you are writing a program and you come to a point where you know need a loop, you will have to decide whether to use a while loop or a do-while loop. In general, the principal is Use a do-while loop if you know that the user will want to execute the loop at least once. Use a while loop if you aren't sure. However, it is also true that the while loop is used far more often than the do-while loop. My recommendation is that you always start off using a while loop. You can switch to a do-while loop if you finish your program and decide that you could clean up your code a bit by using a do-while. Example 1: Question-type loops Let's go back to (you guessed it) our paycheck problem. Let's rewrite it using a question-type loop, but this time assuming that the user wants to execute the loop at least once: #include using namespace std; int main() { int hours; int paycheck; char response; int payRate; do{ cout > hours; cout > payRate; paycheck = hours * payRate; cout ' && currchar == '=') { count++; } } } prevchar = currchar; infile.get(currchar); infile.clear(); infile.close(); cout

Option 1

Low Cost Option
Download this past answer in few clicks

16.89 USD

PURCHASE SOLUTION

Already member?


Option 2

Custom new solution created by our subject matter experts

GET A QUOTE