Assignment #1 Simple C++

Similar documents
Programming Abstractions

CS 106B, Lecture 1 Introduction to C++

CS106B Handout 18 Autumn 2012 October 15 th, 2012 Recursive Backtracking I

Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:

CS106X Handout 04 Winter 2018 January 8 th, 2018 C++ Strings

Assignment #1: and Karel the Robot Karel problems due: 3:15pm on Friday, October 4th due: 11:59pm on Sunday, October 6th

Binary Representation. Jerry Cain CS 106AJ October 29, 2018 slides courtesy of Eric Roberts

CMPSCI 187 / Spring 2015 Hanoi

Exercise: Inventing Language

n! = 1 * 2 * 3 * 4 * * (n-1) * n

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

Assignment 3 Functions, Graphics, and Decomposition

Lesson 5C MyClass Methods. By John B. Owen All rights reserved 2011, revised 2014

CS101 Introduction to Programming Languages and Compilers

printf( Please enter another number: ); scanf( %d, &num2);

Assignment #1: /Survey and Karel the Robot Karel problems due: 1:30pm on Friday, October 7th

Programming Abstractions

Recursive Data and Recursive Functions

Mehran Sahami Handout #7 CS 106A September 24, 2014

Programming Abstractions

CSC-140 Assignment 4

Due Thursday, July 18 at 11:00AM

For example, let s say we define an array of char of size six:

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

6.S189 Homework 2. What to turn in. Exercise 3.1 Defining A Function. Exercise 3.2 Math Module.

Practice problems Set 2

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

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

AP CSA 3rd Period MR. D. Course Map

Sixth lecture; classes, objects, reference operator.

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

Lecture 1: Overview

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Practice Midterm Exam #1

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Understanding Recursion

Functions that return lists

Practice Final Examination #2

def F a c t o r i a l ( n ) : i f n == 1 : return 1 else : return n F a c t o r i a l ( n 1) def main ( ) : print ( F a c t o r i a l ( 4 ) )

1 Writing recursive functions

Practical Questions CSCA48 Week 6

ECE 2036 Lab 1: Introduction to Software Objects

Divisibility Rules and Their Explanations

CMSC 201 Spring 2017 Project 1 Number Classifier

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Help File on C++ and Programming at CSUSM by Rika Yoshii

Problem Set 4: Streams and Lazy Evaluation

CS102: Variables and Expressions

CS106X Handout 04 Autumn 2009 September 25 th, 2009 C++ Strings

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

A couple of decent C++ web resources you might want to bookmark:

2015 Canadian Computing Competition: Junior Division. Sponsor:

CCBC Math 081 Order of Operations Section 1.7. Step 2: Exponents and Roots Simplify any numbers being raised to a power and any numbers under the

Section Handout #3: Strings and Files

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

(Refer Slide Time: 01.26)

University of California, Berkeley College of Engineering

VARIABLES & ASSIGNMENTS

Repetition Through Recursion

Have the students look at the editor on their computers. Refer to overhead projector as necessary.

Practice Final Examination #2

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

(f) d={ alchemist :( a, t ), shaman : ( s, n ), wizard : ( w, z )} d[ shaman ][1]

Properties and Definitions

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

Lecture 2: SML Basics

Recursion. Contents. Steven Zeil. November 25, Recursion 2. 2 Example: Compressing a Picture 4. 3 Example: Calculator 5

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

CS Homework 10 p. 1. CS Homework 10

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Assignment 2.4: Loops

CS Homework 11 p. 1. CS Homework 11

6.001 Notes: Section 4.1

If Statements, For Loops, Functions

61A LECTURE 6 RECURSION. Steven Tang and Eric Tzeng July 2, 2013

Autumn 2012 October 29 th, 2012 CS106X Midterm Examination Solution

Eric Roberts Handout #27 CSCI 121 October 3, Project #2 Snowman. This assignment is designed to build your skills in the following areas:

4. Java language basics: Function. Minhaeng Lee

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd

Practice Midterm Exam #2

Assignment #4 Hangman Due: 3:15pm on Monday, November 3rd Your Early Assignment Help (YEAH) hours: 4:15pm-5:15pm, Friday, October 24th in Hewlett 200

Implementing an Algorithm for Boomerang Fraction Sequences in Python

Final Labs and Tutors

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

FORM 2 (Please put your name and form # on the scantron!!!!)

Lecture 5 8/24/18. Writing larger programs. Comments. What are we going to cover today? Using Comments. Comments in Python. Writing larger programs

Grade 7/8 Math Circles. Counting I

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CS 134 Programming Exercise 9:

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

CS2141 Software Development using C/C++ C++ Basics

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019

pointers + memory double x; string a; int x; main overhead int y; main overhead

C Language, Token, Keywords, Constant, variable

Fundamentals of Programming CS-110. Lecture 2

Chapter Goals. Contents LOOPS

Transcription:

Eric Roberts Handout #5 CS 106B January 7, 2015 Assignment #1 Simple C++ Due: Friday, January 16 Part 1. Get Qt Creator working Parts of this handout were written by Julie Zelenski. Your first task is to set up Qt Creator, which is the programming environment we use in this course. If you re using the machines in Stanford s public clusters, you don t need to do anything special to install the software. If you re using you own machine, you should click on the Qt Creator link on the CS 106B web page and follow the installation instructions appropriate to the platform (Mac, Windows, or Linux) that you are using. Once you have the compiler ready, go to the assignments section of the web site and download the starter files. The Assignment1 folder contains five separate project folders: one for this warm-up problem and one for each of the four problems in Part 2 of the assignment. Open the project file in the folder named 0-Warmup. Your mission in Part 1 is simply to get this program running. The source file we give you is a complete C++ program so complete, in fact, that it includes two bugs. The bugs are not difficult to track down (in fact, we ll tell you that one is a missing declaration and the other is a missing #include statement). This task is designed to give you experience with the way errors are reported by Qt Creator and what you need to do to correct them. Once you fix the errors, compile and run the program. When the program executes, it will ask for your name. Enter your first name, and the program will then print out a hash code (a number) generated for that name. We ll talk later in the class about hash codes and what they are used for, but for now just run the program, enter your name, and record the hash code. You ll email us this number. A sample run of the program is shown below: Once you ve gotten your hash code, we want you to email it to your section leader and introduce yourself. You don t yet know your section assignment, but will receive it via email after signups close, so hold on to your email until then. You should also cc me on the email (eroberts@stanford.edu) so I can meet you as well. Here s the information to include in your email: 1. Your name and the hash code that was generated for it by the program 2. Your year and major

2 3. When you took 106A (or equivalent course) and how you feel it went for you 4. What you are most looking forward to about 106B 5. What you are least looking forward to about 106B 6. Any suggestions that you think might help you learn and master the course material Part 2. Simple C++ problems Most of the assignments in this course are single programs of a substantial size. To get you started, however, the first assignment is a series of four short problems that are designed to get you used to using C++ and to introduce the idea of functional recursion. None of these problems require more than a page of code to complete; most can be solved in just a few lines. Problem 1 (Chapter 2, exercise 15, page 122) A weaker man might be moved to re-examine his faith, if in nothing else at least in the law of probability. Tom Stoppard, Rosencrantz and Guildenstern Are Dead, 1967 Write a program that simulates flipping a coin repeatedly and continues until three consecutive heads are tossed. At that point, your program should display the total number of coin flips that were made. The following is one possible sample run of the program: Problem 2 (Chapter 3, exercise 16, page 154) Most people at least those in English-speaking countries have played the Pig Latin game at some point in their lives. There are, however, other invented languages in which words are created using some simple transformation of English. One such language is called Obenglobish, in which words are created by adding the letters ob before the vowels (a, e, i, o, and u) in an English word. For example, under this rule, the word english gets the letters ob added before the e and the i to form obenglobish, which is how the language gets its name. In official Obenglobish, the ob characters are added only before vowels that are pronounced, which means that a word like game would become gobame rather than gobamobe because the final e is silent. While it is impossible to implement this rule

3 perfectly, you can do a pretty good job by adopting the rule that the ob should be added before every vowel in the English word except Vowels that follow other vowels An e that occurs at the end of the word Write a function obenglobish that takes an English word and returns its Obenglobish equivalent, using the translation rule given above. For example, if you used your function with the main program int main() { while (true) { string word = getline("enter a word: "); if (word == "") break; string trans = obenglobish(word); cout << word << " -> " << trans << endl; } return 0; } you should be able to generate the following sample run: Problem 3 (Chapter 7, exercise 9, page 347) As you know from Chapter 2, the mathematical combinations function c(n, k) is usually defined in terms of factorials, as follows: c(n, k) = n! k! (n k)! The values of c(n, k) can also be arranged geometrically to form a triangle in which n increases as you move down the triangle and k increases as you move from left to right. The resulting structure, which is called Pascal s Triangle after the French mathematician Blaise Pascal, is arranged like this:

4 c(0, 0) c(1, 0) c(1, 1) c(2, 0) c(2, 1) c(2, 2) c(3, 0) c(3, 1) c(3, 2) c(3, 3) c(4, 0) c(4, 1) c(4, 2) c(4, 3) c(4, 4) Pascal s Triangle has the interesting property that every entry is the sum of the two entries above it, except along the left and right edges, where the values are always 1. Consider, for example, the circled entry in the following display of Pascal s Triangle: This entry, which corresponds to c(6, 2), is the sum of the two entries 5 and 10 that appear above it to either side. Use this fact to write a recursive implementation of the c(n, k) function that uses no loops, no multiplication, and no calls to Fact. Write a simple test program to demonstrate that your combinations function works. If you want an additional challenge, write a program that uses c(n, k) to display the first ten rows of Pascal s Triangle. Problem 4 (Implementing numeric conversion) The strlib.h interface exports the following methods for converting between integers and strings: string integertostring(int n); int stringtointeger(string str); 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 The first function converts an integer into its representation as a string of decimal digits, so that, for example, integertostring(1729) should return the string "1729". The second converts in the opposite direction so that calling stringtointeger("-42") should return the integer 42. Your job in this problem is to write the functions inttostring and stringtoint (the names have been shortened to avoid having your implementation conflict with the library version) that do the same thing as their strlib.h counterparts but use a recursive implementation. Fortunately, these functions have a natural recursive structure because it is easy to break an integer down into two components using division by 10. This decomposition is discussed on page 42 in the discussion of the digitsum function. The integer 1729, for example, breaks down into two pieces, as follows:

5 1 7 2 9 1 7 2 n / 10 9 n % 10 If you use recursion to convert the first part to a string and then append the character value corresponding to the final digit, you will get the string representing the integer as a whole. As you work through this problem, you should keep the following points in mind: Your solution should operate recursively and should use no iterative constructs such as for or while. It is also inappropriate to call the provided integertostring function or any other library function that does numeric conversion. The value that you get when you compute n % 10 is an integer, and not a character. To convert this integer to its character equivalent you have to add the ASCII code for the character '0' and then cast that value to a char. If you then need to convert that character to a one-character string, you can concatenate it with string(). (The Java trick of concatenating with "" doesn t work because string constants are C-style strings.) You should think carefully about what the simple cases need to be. In particular, you should make sure that calling inttostring(0) returns "0" and not the empty string. This fact may require you to add special code to handle this case. Your implementation should allow n to be negative, as illustrated by the earlier example in which stringtoint("-42") returns 42. Again, implementing these functions for negative numbers will probably require adding special-case code.