CS Homework 11 p. 1. CS Homework 11

Similar documents
CS Homework 10 p. 1. CS Homework 10

Homework 11 Program Setup (with some IMPORTANT NEW STEPS!)

CS Homework 11 p. 1. CS Homework 11

CS Fall Homework 11 p. 1. CS Homework 11

Deadline. Purpose. How to submit. Important notes. CS Homework 9. CS Homework 9 p :59 pm on Friday, April 7, 2017

CS Homework 10 p. 1. CS Homework 10

Spring CS Homework 12 p. 1. CS Homework 12

Spring CS Homework 6 p. 1. CS Homework 6

Spring CS Homework 7 p. 1. CS Homework 7

CS Week 14 Lab Exercise

Spring CS Homework 3 p. 1. CS Homework 3

CMSC 201 Fall 2016 Homework 6 Functions

CS Homework 2 p. 1. CS Homework 2

CS Exam 2 Study Suggestions

CS Final Exam Review Suggestions - Fall 2017

CS Final Exam Review Suggestions - Spring 2014

CIS 130 Exam #2 Review Suggestions

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

OOP- 4 Templates & Memory Management Print Only Pages 1-5 Individual Assignment Answers To Questions 10 Points - Program 15 Points

CS Homework 1 p. 1. CS Homework 1

Tips from the experts: How to waste a lot of time on this assignment

Spring CS Homework 2 p. 1. CS Homework 2. To practice with PL/SQL stored procedures and functions, and possibly exception handling.

Carleton University Department of Systems and Computer Engineering SYSC Foundations of Imperative Programming - Winter 2012

CS Homework 7 p. 1. CS Homework 7. Problem 1 - START THIS A.S.A.P. (in case there are PROBLEMS...)

Carleton University Department of Systems and Computer Engineering SYSC Foundations of Imperative Programming - Winter 2012

Prerequisites: Read all chapters through Chapter 4 in the textbook before attempting this lab. Read through this entire assignment before you begin.

CS Homework 2. Deadline. How to submit. Purpose. Initial set of CS 328 PL/SQL and SQL coding standards

sftp - secure file transfer program - how to transfer files to and from nrs-labs

CS 170 Java Programming 1. Week 10: Loops and Arrays

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Pattern Maker Lab. 1 Preliminaries. 1.1 Writing a Python program

Slide 1 CS 170 Java Programming 1 Arrays and Loops Duration: 00:01:27 Advance mode: Auto

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

CS 170 Java Programming 1. Week 5: Procedures and Functions

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Programming assignment A

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

CS 150 Lab 10 Functions and Random Numbers

Programming Assignment Multi-Threading and Debugging 2

CIT 590 Homework 5 HTML Resumes

Unit 13. Linux Operating System Debugging Programs

CS 132 Exam #1 - Study Suggestions

CMSC 201 Spring 2019 Lab 06 Lists

Tips from the experts: How to waste a lot of time on this assignment

Style and Submission Guide

CIS Homework 3

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

Computer Programming: C++

OOP- 5 Stacks Individual Assignment 35 Points

COMP 105 Homework: Type Systems

CS Homework 2 p. 1. CS Homework 2

Note. The above image and many others are courtesy of - this is a wonderful resource for designing circuits.

CS Homework 11

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

LAB: WHILE LOOPS IN C++

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

CS Fall Homework 5 p. 1. CS Homework 5

Lab 1: Introduction to Java

************ THIS PROGRAM IS NOT ELIGIBLE FOR LATE SUBMISSION. ALL SUBMISSIONS MUST BE RECEIVED BY THE DUE DATE/TIME INDICATED ABOVE HERE

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Documentation Requirements Computer Science 2334 Spring 2016

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

Tips from the experts: How to waste a lot of time on this assignment

CS 051 Homework Laboratory #2

Slide 1 CS 170 Java Programming 1

Reviewing all Topics this term

More Complicated Recursion CMPSC 122

C++ For Science and Engineering Lecture 2

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

CSE100 Principles of Programming with C++

CS Homework 5 p. 1. CS Homework 5

ITP 165: Introduction to C++ Programming Homework 10: Superhero fight Due: 11:59pm, December 1 st, 2017

CS16 Midterm Exam 2 E02, 09F, Phill Conrad, UC Santa Barbara Wednesday, 11/18/2009

Lab 3. A Multi-Message Reader

CMSC 201 Spring 2016 Homework 7 Strings and File I/O

gcc o driver std=c99 -Wall driver.c bigmesa.c

Problem Set 1. You might want to read Chapter 12 of the course reader before attempting this problem.

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

CSE 5A Introduction to Programming I (C) Homework 4

CS1110 Lab 1 (Jan 27-28, 2015)

Lesson 2 Variables and I/O

Copied from: on 3/20/2017

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

LOOPS. Repetition using the while statement

CS 170 Java Programming 1. Week 12: Creating Your Own Types

Com S 227 Spring 2018 Assignment points Due Date: Thursday, September 27, 11:59 pm (midnight) "Late" deadline: Friday, September 28, 11:59 pm

All about flow control

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

Programming Standards: You must conform to good programming/documentation standards. Some specifics:

LAB 5, THE HIDDEN DELIGHTS OF LINKED LISTS

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

CS Homework 8 p. 1. CS Homework 8

Programming Assignment #2

Post Experiment Interview Questions

CpSc 1111 Lab 4 Formatting and Flow Control

Homework 7: Subsets Due: 11:59 PM, Oct 23, 2018

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Assignment 3: Binary numbers, data types and C Programs Due 11:59pm Monday 26th March 2018

Transcription:

CS 111 - Homework 11 p. 1 Deadline 11:59 pm on Monday, May 2, 2016 How to submit Each time you would like to submit your work: CS 111 - Homework 11 If your files are not already on nrs-labs, be sure to use sftp/winscp/filezilla to get them TO nrs-labs (ideally in a folder/directory named 111hw11). Then, if you are not already logged onto nrs-labs, then use PuTTY/ssh to do so, and use cd to change to the folder/directory where your homework files are -- for example, cd 111hw11 Use the ls command to make sure your desired.cpp and.h files are really there: ls Use ~st10/111submit to submit them, with a homework number of 11 Make sure that ~st10/111submit shows that it submitted ALL of your.cpp and.h files you were intending to submit! Purpose To design and implement C++ functions, including main functions, and including some involving arrays, for-loops, and also a sentinel-controlled-style while loop. Important notes As always, start the homework problems early! Then you'll have time to e-mail me your.cpp and.h files along with the error message(s) if you run into an error you don't know how to handle. Be sure to follow the course coding style discussed in class and demonstrated in posted examples. In particular, remember to indent as shown and demonstrated in posted examples. You are no longer required to use funct_play2 to develop your C++ functions. You may still use it if you would like (keeping in mind its limitations for main and void functions!). If you are not using funct_play2, note that you need to include all of the components shown in the given templates (for main functions, non-main functions, and.h files) on the public course web page, whether you actually use those templates or not. Remember that you can JUST compile any single C++ function on nrs-labs with the command: g++ -c filename.cpp (If all goes well, this doesn't print anything to the screen, but it does result in a file filename.o, ready to be linked and loaded with other files to create a C++ executable file.) Remember that you can compile, link, and load to create a C++ executable program on nrs-labs with the command: g++ main_filename.cpp other1.cpp other2.cpp... -o main_filename

CS 111 - Homework 11 p. 2...being sure to include the.cpp files for ALL functions used in that program; Also, to help you write these g++ calls to compile, link, and load your C++ programs (to result in C++ executable programs), there is a little script compile-helper on nrs-labs that asks you to enter details about your program-to-be and then "builds" an appropriate g++ command, which it runs as well as prints out so you can gradually learn how to write these yourself. (NOTE: it assumes you have already written all of the.cpp and.h files for your program, and have transferred them all to your current working directory on nrs-labs.) You are, of course, still expected to follow the Design Recipe for all functions that you design/define, whether they are non-main or main functions. Remember, you will receive significant credit for the signature, purpose, header, and examples/tests portions of your functions. Typically you'll get at least half-credit for a correct signature, purpose, header, and examples/tests, even if your function body is not correct. (and, you'll lose at least half-credit if you omit these or do them poorly, even if your function body is correct). Problem 1 As an array warm-up, write a main function in a file named array_play.cpp. This main function's purpose is simply to allow you to try creating an example array and then do something with its contents. Here are the requirements: decide on how big an array you would like -- any size larger than 5 is acceptable. This size should be written as a named constant. decide what type of value you would like for your array to contain. declare an array of that type and size, and initialize it to values of your choice. then write a for loop to do something to/with each value in your array. You can choose -- it can be as simple as simply printing each value in the array, or something more elaborate (you can modify each value, call some function of your choice for each value, etc.) Whatever you choose, make sure it has some output/result printed to the screen. Submit your resulting array_play.cpp. Problem 2 This problem does not involve arrays -- its purpose is to provide more for loop practice. Recall that, in Homework 8, Problem 5, you wrote a function get_worth that expects a character representing a coin: 'Q' or 'q' -- quarter 'D' or 'd' -- dime 'N' or 'n' -- nickel

CS 111 - Homework 11 p. 3 'C' or 'c' or 'P' or 'p' -- cent/penny...and it returns the decimal worth of that coin. Copy at least get_worth.cpp and get_worth.h into your working folder/directory for Homework 11, since you will be using and calling that function in this problem. (Note that there is a version of this function on the course Canvas site, under "Files", in the "Homework 8 Example Solutions (in-progress)" folder Also recall the string class method at, that expects the position of a desired character in that string, and returns the char at that position in the string (remembering, also, that it considers the position of the first character in the string to be position 0, not 1). Think about this -- do you see that you could use a count-controlled for loop with this at method to do something with each char within a string? (Hint: consider function vertical, that uses a count-controlled loop and the at method to "grab" each character in a string and output it on its own line (thus outputting it in a "vertical" style). Likewise, consider function sum_ints, that shows how you can use a count-controlled loop to sum all of the integers from 1 to a given integer. NOW consider: what if you had a string whose characters were coin values? For example: "qdnncdqpn" Using the design recipe, write a function string_worth that expects a string of coin characters, and returns the sum of the decimal worths of the coin characters in that string. For example, string_worth("qdnncdqpn") == 0.25 +.10 +.05 +.05 +.01 +.10 +.25 +.01 +.05 string_worth("qn") == 0.30 For full credit, string_worth must also: appropriately call and use get_worth use a count-controlled for loop To formally test your function, design a main function in a file named string_worth_test.cpp that: prints a message saying that you are testing function string_worth, and that trues mean passed copy each example/test from string_worth's examples into its own separate cout statement, such that the result of that test will be printed on its own line Submit your resulting files string_worth_test.cpp, string_worth.cpp, and string_worth.h (as well as at least get_worth.h and get_worth.cpp). Problem 3 Develop a C++ function how_many that expects a desired string, an array of strings, and the array's size, and returns the number of times the desired string appears in that array. It is required to appropriately use a for loop. (suggestion: during the design recipe, after making your specific examples/tests, develop pseudocode for how you figured out how many times your example string appeared in your example array -- how did you know when a match was found? how do you keep track of how many you've seen so far?) (hint: for this one there had better be more than one example call! What are the different cases you should check? But as an additional hint -- if you design it well, you can probably use the same example array for

CS 111 - Homework 11 p. 4 more than one example call. What should be different in those calls, then?) To formally test your function, design a main function in a file named how_many_test.cpp that: prints a message saying that you are testing function how_many, and that trues mean passed declares and initializes at least one appropriate array, using a named constant for its size copies each example/test from how_many's examples into its own separate cout statement, such that the result of that test will be printed on its own line Submit your files how_many.h, how_many.cpp, and how_many_test.cpp. Problem 4 Copy at least starline.cpp and starline.h into your working folder/directory for Homework 11, since you will be using and calling function starline in this problem. (You can also find an example solution for this on the course Canvas site, under "Files", in the "Week 12 Lab Exercise Example Solutions (in-progress)" folder.) Consider, again, function starline, that expects the number of asterisks/stars to output, and it returns that number, but also has the side-effect of printing that many asterisks on one line to the screen (if <=0, no stars are output). You could use this to create a kind of horizontal bar chart, calling it for each of a set of values. And what is an array but a set of values? Write a C++ function bar_chart that expects an array of integers and its size, returns the number of rows in the resulting bar chart (that is, the array's size...!), and has the side-effect of printing to the screen a horizontal bar chart with the help of starline, printing a line of *'s the length of each array value. This function must appropriately call starline and it must use an appropriate for loop. For example, for: const int NUM_MSRS = 7; int measures[num_msrs] = {3, 1, 6, 2, 8, 4, 5}; bar_chart(measures, NUM_MSRS) == NUM_MSRS...and has the side-effect of causing the following to be printed to the screen: *** * ****** ** ******** **** ***** To formally test your function, design a main function in a file named bar_chart_test.cpp that: prints a message saying that you are testing function bar_chart calls bar_chart at least 2 times, each time with different arrays of different sizes because bar_chart has a desired side-effect in addition to its return value, for EACH of its

CS 111 - Homework 11 p. 5 examples/tests, it should first print a message saying that what follows should rows of stars of lengths <list them>, followed by true, and then put that example/test in its own separate cout statement, such that the result of that test will be printed on its own line. Submit your resulting files bar_chart.h, bar_chart.cpp, and bar_chart_test.cpp. Problem 5 We will/did discuss sentinel-controlled while loops in the Week 14 Lab. You will practice writing one of these in this problem. A user might like a version of Problem 2's string_worth that allows them to enter more than one string of coin characters. So......for practice writing a sentinel-controlled-style while loop, write a main function in a file named count_change.cpp whose purpose is to serve as a repeated-interactive front-end for the function string_worth. That is, your main function will repeatedly prompt the user to enter a string of coin characters, and then use string_worth to determine the worth of the coins in the entered string and print to the screen a polite, readable message telling the user the worth of the coins in the entered coin-characters string, until the user indicates that he/she would like to stop by entering the special sentinel value. For this program, the user entering an empty coin-string would make for a quite reasonable sentinel value. Here are two additional requirements: this program should now allow blanks in the entered coin character strings by using the function getline to read in the entered coin-characters string also keep a running total of the worth of all of the strings entered by the user, and after the user enters the special sentinel value, print a final polite, readable message telling the user the total worth of all of the entered coin-character strings he/she entered during that program run. Make sure that this main function uses a properly structured sentinel-controlled-style while loop (as described in the Week 14 Lab) in asking the user to enter the next coin-characters string, and then displaying total worth of the entered string. Also, be sure to include what the user needs to type in to quit as part of your interactive prompt to the user. Test-run your count_change program on an appropriate variety of test input until you are convinced that it is working properly. (In this case, it is also wise to test it on a case in which the user immediately enters the sentinel value, the first time he/she is asked. What should happen in that case?) Submit your resulting count_change.cpp. (You will be submitting/have submitted the files for string_worth and get_worth as part of Problem 2.)