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

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

CS Homework 11 p. 1. CS Homework 11

CS Homework 11 p. 1. CS Homework 11

CS Homework 10 p. 1. CS Homework 10

Spring CS Homework 12 p. 1. CS Homework 12

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

Spring CS Homework 6 p. 1. CS Homework 6

CS Fall Homework 11 p. 1. CS Homework 11

Spring CS Homework 7 p. 1. CS Homework 7

CS Exam 2 Study Suggestions

Spring CS Homework 3 p. 1. CS Homework 3

Your First C++ Program. September 1, 2010

CS Week 14 Lab Exercise

CIS 130 Exam #2 Review Suggestions

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

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

CS Homework 2 p. 1. CS Homework 2

CS Final Exam Review Suggestions - Fall 2017

C++ For Science and Engineering Lecture 2

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

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

Lab 1: First Steps in C++ - Eclipse

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

CS Final Exam Review Suggestions - Spring 2014

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

Exercise 1.1 Hello world

Week 2: Data and Output

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

CS 132 Exam #1 - Study Suggestions

Lab: Supplying Inputs to Programs

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

CS101 Linux Shell Handout

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

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

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

CS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Homework 09. Collecting Beepers

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

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Unit 13. Linux Operating System Debugging Programs

CS31 Discussion 1E Spring 17 : week 05

EECS 183, Week 5. General. Variables I/O. 0. At which location do you have to take the exam? 1. Source code vs. object code? 2. What s a library?

CS 31 Discussion ABDULLAH-AL-ZUBAER IMRAN WEEK 6: C-STRINGS, STRUCT, CLASS AND PROJECT4

Getting started with C++ (Part 2)

Unit 10. Linux Operating System

Linux Tutorial #1. Introduction. Login to a remote Linux machine. Using vim to create and edit C++ programs

Operator overloading

CS Homework 6. Deadline. Purpose. How to submit. Important notes. Problem 1. Spring CS Homework 6 p. 1

CS 31 Discussion 1A, Week 1. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

PIC 10A. Review for Midterm I

CS Homework 11

MS Visual Studio.Net 2008 Tutorial

CS1110 Lab 1 (Jan 27-28, 2015)

Spring CS Homework 8 p. 1. CS Homework 8

Lab 7 (50 pts) Due Sunday, May 20)

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

CS 106B Lecture 2: C++ Functions

Introduction to C ++

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

CSc Introduction to Computing

Introduction to Unix

Chapter 2. Editing And Compiling

CS Software Engineering for Scientific Computing. Lecture 5: More C++, more tools.

CS Homework 2 p. 1. CS Homework 2

Computer Science II Lecture 1 Introduction and Background

Getting Started with Visual Studio

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

PIC 10A Objects/Classes

Slide 1 CS 170 Java Programming 1

CS 11 C++ track: lecture 1

CS Fall Homework 5 p. 1. CS Homework 5

Lecture 2 Tao Wang 1

Chapter 2, Part I Introduction to C Programming

Chapter 1 - What s in a program?

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

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

EECS 183. Week 3 - Diana Gage. www-personal.umich.edu/ ~drgage

These are notes for the third lecture; if statements and loops.

Eat (we provide) link. Eater. Goal: Eater(Self) == Self()

C++ Support Classes (Data and Variables)

CS 463 Project 1 Imperative/OOP Fractals

Lab # 02. Basic Elements of C++ _ Part1

1) Log on to the computer using your PU net ID and password.

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

CS Homework 1 p. 1. CS Homework 1

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.5. for loop and do-while loop

CS Homework 8 p. 1. CS Homework 8

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

6.096 Introduction to C++ January (IAP) 2009

Programming. Computer. Program. Programming Language. Execute sequence of simple (primitive) instructions What instructions should be provided?

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

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

C++ for Java Programmers

Lab 1: Introduction to Java

Practicum 5 Maps and Closures

Transcription:

CS 111 - Homework 9 p. 1 Deadline 11:59 pm on Friday, April 7, 2017 Purpose CS 111 - Homework 9 To give you an excuse to look at some newly-posted C++ templates that you might find to be useful, and to use the design recipe to design and implement C++ functions, including functions with the side-effect of printing to the screen using cout, and even including main functions. How to submit Each time you would like to submit your work: 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 111hw9). 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 111hw9 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 9 Make sure that ~st10/111submit shows that it submitted ALL of your.cpp and.h files you were intending to submit! 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. (OR -- you can e-mail me those error message(s) and let me know that you've submitted the related.cpp and.h files using ~st10/111submit, and I can look over your function's files that way in trying to answer your question.) 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. For some of these problems, you will not be using funct_play -- for some, you still can (and probably should). Even when no longer required, you may still use funct_play if you would like (keeping in mind its limitations for main and void functions!). When you are not using funct_play, 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

CS 111 - Homework 9 p. 2 (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...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 I have posted templates -- handy starting points -- for C++ main functions, C++ non-main functions, and.h files for C++ non-main functions; they are on the public course web site, in the References section, just after the link to the course syllabus. (I also put links to them along with this homework handout in "Homeworks and Handouts".) As a small warm-up -- let's give you an immediate reason to refer to these newly-posted templates! Make a file named 111hw9-1.txt (you can use nano for this on nrs-labs, or something like NotePad++/TextWrangler your computer and then copy it to nrs-labs using sftp/winscp/filezilla), and start this file with: your name the last modified date Then answer the following questions based on these posted templates, preceding each answer with the part that it is for (1 part a, 1 part b, etc.) 1 part a Look at the template for C++ main functions, in main-template.txt, and scroll down to the main function header and function body given in this template. What is main's function header in this template?

CS 111 - Homework 9 p. 3 1 part b Again look at the template for C++ main functions, in main-template.txt, and again scroll down to the main function header and function body given in this template. What C++ statement is the first statement inside the main function body in this template? 1 part c Again look at the template for C++ main functions, in main-template.txt, and again scroll down to the main function header and function body given in this template. What C++ statement is the last statement inside the main function body? 1 part d Now look at the template for C++ non-main functions, in non-main-function-template.txt. What word follows # in four of the lines, between the opening comment block and the function header? 1 part e Now look at the template for C++.h files (which you need for non-main functions, but usually not for main functions). What does this template note that you need to #include if your function has any parameters of type string or any named constants of type string? 1 part f Again look at the template for C++.h files. You'll see X_H in this template twice. What does the template tell you to replace those X's with? Problem 2 2 part a For some practice with cout, use funct_play to develop a function salutation that expects a name, has the side effect of printing to the screen a letter-style salutation including that name (and ending with a newline), and returns the length of the given name. (Remember, it is possible for a function to return something AND have a side-effect -- consider Week 10 Lecture 1's play_around and say_twice functions.) For example, salutation("ann") == 3 and has the side-effect of printing to the screen: Dear Ann, and salutation("charlie Brown") == 13 and has the side-effect of printing to the screen: Dear Charlie Brown, NOTE that, when you properly describe each example's side-effect, that will cause the main function being built for you in salutation_ck_expect.cpp to not compile! We'll fix this main function in salutation_ck_expect.cpp in 2 part b.

CS 111 - Homework 9 p. 4 2 part b Now, as mentioned, describing salutation's side-effect while writing salutation's examples/tests in funct_play caused funct_play to write bad-syntax cout statements in the main function in salutation_ck_expect.cpp. For example, if during funct_play you gave as an example: salutation("ann") == 3...and has the side-effect of printing to the screen: Dear Ann,...that causes funct_play to build these cout statements in salutation_ck_expect.cpp: cout << "(salutation(\"ann\") == 3): " << (salutation("ann") == 3) << endl; cout << "(...and has the side-effect of printing to the screen:): " << (...and has the side-effect of printing to the screen:) << endl; cout << "(Dear Ann): " << (Dear Ann) << endl; The first cout above is fine -- it outputs a char* literal and a function call result. The second two cout statements above are NOT OK, as they include bad syntax C++ expressions! SO, using nano, CHOP OUT the "bad" cout statements from salutation_ck_expect.cpp for each of your tests, and put a NEW cout statement before each of your tests that says what you SHOULD see when that test is run -- for example: cout << "should see a salutation for Ann, followed by a true test:" << endl; cout << "(salutation(\"ann\") == 3): " << (salutation("ann") == 3) << endl; SAVE your resulting salutation_ck_expect.cpp and re-compile EITHER using funct_compile OR by typing: g++ salutation_ck_expect.cpp salutation.cpp -o salutation_ck_expect Submit your resulting files salutation.cpp, salutation.h, and salutation_ck_expect.cpp. Problem 3 Consider Week 10 Lecture 2's main function in a file named hello.cpp: #include <cstdlib> #include <iostream> #include <string> #include <cmath> using namespace std; /*===== signature: main: void -> int purpose: expects nothing, and has the side-effect prints a greeting to the screen examples: when this is run, the following is printed to the screen:

CS 111 - Homework 9 p. 5 Hello, world! by: Sharon Tuttle last modified: 2017-03-30 =====*/ int main() { cout << "Hello, world!" << endl; } return EXIT_SUCCESS; You are now going to write a little main function, somewhat similar to this one, in a file named draw_pic.cpp. FUN FACT/REMINDER: To print a double quote (") or a backslash (\), you have to precede it with a backslash to "escape" its otherwise-special meaning. That is, cout << "moo\"moo\\moo" << endl; causes the following to be printed to the screen: moo"moo\moo With that said... Simply to practice with cout within a very simple main function, consider "ASCII art" -- drawing "pictures" with "plain" characters. For example, consider: XX or (very slightly modified from http://www.asciiworld.com/-cats-.html) /\_/\ ( o o ) > ^ < meow! or (from http://www.incredibleart.org/links/ascii/scarecrow_resources.html) \ / \o/ _/ \_ Write a main function in a file named draw_pic.cpp that simply prints to the screen a "hard-coded" ASCII art picture consisting of at least 4 lines of characters. ("Hard-coded" means it does the same thing every time -- it is not customizable, it can't be modified without modifying the program itself. The main function in hello.cpp could be said to be printing to the screen the "hard-coded" greeting Hello, world!) You may choose one of the shown examples, or come up with one of your own, or reproduce one you find on the web (IF you include the URL where you found it in a comment within your code -- credit your sources!).

CS 111 - Homework 9 p. 6 You can compile/link/load and create an executable for this program using the command: g++ draw_pic.cpp -o draw_pic (or you can try out compile-helper to walk you through this, if you would like!)...and, if you get no errors, you should be able to run the resulting program using the command: draw_pic Submit your resulting draw_pic.cpp. (NOTE: These might be posted on the course Moodle site for class perusal.) Problem 4 More typically, your main functions will call another function (or functions!) that you have written. NOTE that if your function RETURNS something you would like printed, you need to call it INSIDE a cout statement, so that the result the function RETURNS is printed to the screen: cout << sqrt(4) << endl; BUT, if your function has the side-effect of printing to the screen, and you JUST want that side-effect, and NOT what it returns, you can JUST call that function as if it were a statement...! next_sound("moo"); KEEPING THIS IN MIND,...either starting from the posted main template or from your main in draw_pic.cpp, develop a main function in a file salutation_play.cpp that simply calls salutation at least FOUR times, with at least FOUR different arguments. Some important reminders: what do you need to #include in salutation_play.cpp so that its main function can call salutation? be sure to modify the opening comment block based on what this main function is doing! remember that you can compile/link/load and create an executable for this program using the command: g++ salutation_play.cpp salutation.cpp -o salutation_play...and, if you get no errors, you should be able to run the resulting program using the command: salutation_play Submit your resulting salutation_play.cpp; (you already submitted salutation.cpp and salutation.h as part of Problem 2).