CS 106B, Lecture 1 Introduction to C++

Similar documents
CS 106X, Lecture 16 More Linked Lists

CS 106X, Lecture 10 Recursive Backtracking

CS 106X, Lecture 14 Classes and Pointers

Homework Assignment 2: Java Console and Graphics

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

Queues and Unit Testing

CS 106A, Lecture 5 Booleans and Control Flow

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

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

Programming Abstractions in C++

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

COMP322 - Introduction to C++ Lecture 01 - Introduction

CS 106X, Lecture 7 Introduction to Recursion

Intensive Introduction to Computer Science. Course Overview Programming in Scratch

CS 106B Lecture 2: C++ Functions

CS 200, Section 1, Programming I, Fall 2017 College of Arts & Sciences Syllabus

Programming Karel the Robot

CS 106A, Lecture 27 Final Exam Review 1

Computer Science II Lecture 1 Introduction and Background

CS 106X, Lecture 23 Dijkstra and A* Search

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

Announcements. 1. Forms to return today after class:

CPS 216, Problem Solving and Programming Techniques in C++ Saleh M. Alnaeli, Ph.D. Spring, Lab_Week2

WELCOME TO CS 16! Enrollment status: 117/ Problem Solving with Computers-I

CS 106A, Lecture 25 Life After CS 106A, Part 1

CS 106A, Lecture 27 Final Exam Review 1

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

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

CSE 142. Lecture 1 Course Introduction; Basic Java. Portions Copyright 2008 by Pearson Education

Objects and Graphics

CS 106A, Lecture 3 Problem-solving with Karel

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

CS 106A, Lecture 16 Arrays

CSC116: Introduction to Computing - Java

CS 241 Data Organization. August 21, 2018

Lecture 7. Log into Linux New documents posted to course webpage

Scientific Computing

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

Welcome to CompSci 201

Fundamentals of Programming CS-110. Lecture 2

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

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

PIC 10A. Review for Midterm I

Lecture 1: Welcome! CSE 373: Data Structures and Algorithms CSE WI - KASEY CHAMPION 1

Introduction to Computer Systems

Your First C++ Program. September 1, 2010

CSC116: Introduction to Computing - Java

Expressions and Control Statements

Introduction to Data Structures

Control Statements. if for while

Assignment #1 Simple C++

Rochester Institute of Technology Golisano College of Computing and Information Sciences Department of Information Sciences and Technologies

Lecture 1: CSE 373. Data Structures and Algorithms

CS 106X Lecture 26: Inheritance and Polymorphism in C++

Introduction to Java

Menu. Class 1: Introduction. Staff. Course Structure and Expectations. Contacting Us. Contacting You

WELCOME TO CS 16! Problem Solving with Computers-I.

CS 106A, Lecture 14 Events and Instance Variables

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

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:

Welcome to Solving Problems with Computers I

CS 107 Lecture 0: Welcome

CE221 Programming in C++ Part 1 Introduction

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

CSE 373: Introduction, ADTs, Design Decisions, Generics. Michael Lee Wednesday Jan 3, 2017

ECOR Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

CSC116: Introduction to Computing - Java

CS 106A, Lecture 20 ArrayLists and HashMaps

CIS*1500 Introduction to Programming

Memory Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

CIS 130 Exam #2 Review Suggestions

Object Oriented Design

EE 109 Lab 8a Conversion Experience

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:

Advanced Programming CMPS 109

Introduction to Programming System Design CSCI 455x (4 Units)


CS 302: Introduction to Programming in Java. Lecture 9

You must pass the final exam to pass the course.

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

COSE212: Programming Languages. Lecture 0 Course Overview

CSE 240 Introduction to Computer Architecture

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

Unit 7. 'while' Loops

CS 376b Computer Vision

Assignment #2: Intro to Java Due: 11AM PST on Wednesday, July 12

Outline. CIS 110: Introduction to Computer Programming. What is Computer Science? What is computer programming? What is computer science?

CS 106A, Lecture 23 Interactors and GCanvas

Arrays 2 CS 16: Solving Problems with Computers I Lecture #12

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

CS 241 Data Organization using C

Central Washington University Department of Computer Science Course Syllabus

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

6.096 Introduction to C++ January (IAP) 2009

Tutorials. Tutorial every Friday at 11:30 AM in Toldo 204 * discuss the next lab assignment

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

Created by Julie Zelenski, with edits by Jerry Cain, Keith Schwarz, Marty Stepp, Chris Piech and others.

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Transcription:

CS 106B, Lecture 1 Introduction to C++ reading: Programming Abstractions in C++, Chapters 1 & 2 This document is copyright (C) Stanford Computer Science and Ashley Marty Stepp, Taylor, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides Based created on slides by Marty created Stepp, by Keith Chris Schwarz, Gregg, Keith Julie Schwarz, Zelenski, Julie Jerry Zelenski, Cain, Eric Jerry Roberts, Cain, Mehran Eric Roberts, Sahami, Mehran Stuart Sahami, Reges, Stuart Cynthia Reges, Lee, and Cynthia others. Lee, and others

Plan for Today Course Overview and Expectations Course Staff introduction Course Policies Introduction to C++ Syntax Import statements Console input/output Our first programs Assignment 0 - NameHash 2

Course Staff Instructor: Ashley Taylor OH: 11:20-12:15PM M-Th ataylor4@stanford.edu Head TA: Shreya Shankar OH: 1:30-3:30PM Tuesday shreya@cs.stanford.edu 3

Section Leaders (SLs) Lead required 50-minute sections (5% of your grade) If you need to miss a week, just attend a different section. Full list at cs198.stanford.edu Grade homework Hold office hours (LaIR) from 7-11PM, Sunday- Wednesday, in the first floor of Tresidder Sign up for section before 5PM on Tuesday at cs198.stanford.edu 4

Whom to Contact? Non-coding homework or course logistics questions Piazza (https://piazza.com/class/jgo9q8mwgn26s8) Coding homework questions or Qt issues LaIR or head TA/instructor OH Conceptual Questions (no code) CLaIR (same time and place as LaIR) or head TA/instructor OH Homework grading questions Email your SL Alternate exam scheduling, assignment regrade requests, switching section to work with a partner, extension requests Email the head TA Honor Code Questions or Course Feedback Email the instructor or attend instructor OH 5

Course Tools Course website (important announcements, handouts, etc.) http://web.stanford.edu/class/cs106b Course Forum: Piazza https://piazza.com/class/jgo9q8mwgn26s8 LaIR and CLaIR: 7-11PM, Sunday through Wednesday, in Tresidder Lecture Videos: canvas.stanford.edu Textbook: Eric Roberts Programming Abstractions in C++ http://web.stanford.edu/class/cs106b/textbooks.html Homework Turn-in: Paperless cs198.stanford.edu/paperless Our IDE: Qt Creator http://web.stanford.edu/dept/cs_edu/qt-creator/qt-creator.shtml 6

Homework 6.5 near-weekly homeworks (the schedule is on our website: http://web.stanford.edu/class/cs106b/schedule/) Cumulatively 35% of your grade Graded on functionality and style Use a bucket-system : most grades are a check-plus or a check Pair Programming: pairs must be in the same section, work together on an assignment Late assignments Everyone gets three free 24-hour late days for the quarter May turn in an assignment no more than 48-hours late; the last assignment will not be accepted late After late days are used, each additional 24-hour period is one bucket deduction Hint: always read (and re-read) the homework spec 7

Exams Midterm Wednesday, July 25, 7-9PM in Hewlett 200 Final Saturday, August 18, 8:30-11:30AM. Location TBA The final will be cumulative with more emphasis on material covered in the second half of the quarter All exams are closed-book, closed-note though you may bring one 8.5x11 double-sided sheet of notes with you Please fill out the exam form (on the course website) before Friday (part of Homework 0) Students with accommodations should send their accommodations letter to Shreya and me 8

Honor Code and CS 106 http://honorcode.stanford.edu/ Please help us ensure academic integrity: Do not look at other people's solution code (outside of your pair). Do not give your solution code to others, or post it on the web. Indicate any assistance received on HW (books, web sites, friends). Report any inappropriate activity you see performed by others. Assignments are checked for similarity with help of software tools. If you realize that you have made a mistake, you may retract your submission to any assignment at any time, no questions asked. If you need help, please contact us and we will help you. See Honor Code handout on course web site 9

Course Overview Mastering ADTs (Collections) Understanding recursion and recursive backtracking Managing memory with pointers Implementing collections using data structures like linked lists and trees Learning about graphs and graph algorithms Analyzing algorithmic efficiency 10

Modeling Hierarchies Source: http://www.izzness.com/postpic/2014/11/executive-branch-u-s-government-chart_616676.jpg 11

Phylogenetic Trees Source: https://en.wikipedia.org/wiki/file:tree_of_life_svg.svg 12

Fractal Graphics Source: https://upload.wikimedia.org/wikipedia/commons/0/0e/terragen_render.jpg 13

Google Maps Source: https://www.google.com/maps 14

Announcements Assignment 0 released today, due Friday Google form with course logistic information Fill out the exam conflict survey by 5PM on Friday Try to install Qt Creator tonight, and stop by our Qt Creator Installation help session tomorrow from 1:30-3:30PM in Gates B02 Sign up for section at cs198.stanford.edu Section signups close Tuesday at 5PM Make sure you sign up for the same sections as your partner (if you have one) You'll be assigned a section before Wednesday's lecture 15

What is C++? (1.2) C++: A programming language developed in 1983 by Bjarne Stroustrup. one of the world's most widely used languages today built for systems programming with high speed/efficiency built on older C language by adding object-oriented programming continues to be improved over time (latest version: C++17) C++ syntax has many similarities with Java and C similar data types (int, double, char, void) similar operators (+, -, *, /, %), keywords use of { } braces for scope comes equipped with a large standard library for you to use 16

C++ programs/files (1.3) C++ source code lives in.cpp files Additional declarations can be put in "header".h files Source code is compiled into binary object files (.o) unlike a Java.class, C++ executables are platform-dependent file1.cpp file2.cpp compile object file object file library executable library link 17

First C++ program (1.1) helloworld /* * hello.cpp * This program prints a welcome message * to the user. */ #include <iostream> using namespace std; int main() { cout << "Hello, world!" << endl; return 0; } 18

First C++ program (1.1) helloworld /* * hello.cpp * This program prints a welcome message * to the user. */ #include <iostream> using namespace std; int main() { cout << "Hello, world!" << endl; return 0; } Program comments Inline comments can be written as: // comment 19

First C++ program (1.1) helloworld /* * hello.cpp * This program prints a welcome message * to the user. */ #include <iostream> using namespace std; int main() { cout << "Hello, world!" << endl; return 0; } Import statements C++ libraries are written with angle brackets Local (and Stanford) libraries have quotes: #include "lib.h" 20

First C++ program (1.1) helloworld /* * hello.cpp * This program prints a welcome message * to the user. */ #include <iostream> using namespace std; int main() { cout << "Hello, world!" << endl; return 0; } Namespaces Functions and variables are divided (scoped) by namespace Normally would refer to them as namespace::symbol The "using" keyword removes the need for the namespace (brings those symbols into the global program scope) 21

First C++ program (1.1) helloworld /* * hello.cpp * This program prints a welcome message * to the user. */ #include <iostream> using namespace std; int main() { cout << "Hello, world!" << endl; return 0; } Main function entry point for the program Should always return an integer (0 = success) Functions do not need to be part of a class in c++ 22

Familiar syntax (1.5-1.8) int x = 42 + 7 * -5; // variables, types double pi = 3.14159; char c = 'Q'; /* two comment styles */ bool b = true; for (int i = 0; i < 10; i++) { if (i % 2 == 0) { x += i; } } while (x > 0 && c == 'Q' b) { x = x / 2; if (x == 42) { return 0; } } foobar(x, 17, c); barbaz("this is a string"); // for loops // if statements // while loops, logic // function call // string usage 23

User Input and Output reading: Programming Abstractions in C++, Chapter 2, 4 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others.

Console output: cout cout << expression << expression... cout << "You are " << age << " years old!"; endl A variable that means "end of line" Same as "\n", but more compatible with all operating systems cout << "You are " << age << " years old!" << endl; 25

Getting Console Input Use the Stanford Library simpio: #include "simpio.h" Function name Description getinteger("prompt") repeatedly prompts until an integer is typed; returns it getreal("prompt") repeatedly prompts until double is typed; returns it getline("prompt") prompts and reads/returns an entire line of text getyesorno("prompt") repeatedly prompts for a Yes/No answer; return it as a bool string fullname = getline("student name? "); int age = getinteger("how old are you? "); double gpa = getreal("what's your GPA so far? "); if (getyesorno("destroy the universe?")) {... } NOTE: cin is discouraged Doesn't handle errors well or work with Stanford libraries Difficult to get full lines of input 26

Stanford library http://stanford.edu/~stepp/cppdoc/ 27

Exercise: Stanford vs Cal stanfordvscal Write a program to compute who won the Stanford-Berkeley game. Assume that the user enters valid integers. Example output: Stanford points scored? 87 Cal points scored? 3 Stanford won! 28

Stanford vs Cal solution /* This program prints a score of a football game. */ #include <iostream> #include "simpio.h" using namespace std; int main() { int stanford = getinteger("stanford points scored? "); int cal = getinteger("cal points scored? "); if (stanford > cal) { cout << "Stanford won!" << endl; } else if (cal > stanford) { cout << "Cal won!" << endl; } else { cout << "A tie." << endl; } return 0; } 29

Overflow (extra) slides This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others.

Which is not an error? fixerrors #import <iostream> // A. using namespace std; public static int main() { // B. int age = getinteger("how old are you?"); // C. } cout << "You are " + age + ", wow!" + endl; // D. 31