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

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

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

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

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Pre- Defined Func-ons in C++ Review for Midterm #1

Func%ons in C++ Part 2 CS 16: Solving Problems with Computers I Lecture #5

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

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

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

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

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

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

Welcome to Solving Problems with Computers I

Advanced Flow Control CS 16: Solving Problems with Computers I Lecture #5

MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Design and Debug: Essen.al Concepts Numerical Conversions CS 16: Solving Problems with Computers Lecture #7

More on Strings in C++ Arrays CS 16: Solving Problems with Computers I Lecture #11

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

Review for the Final Exam CS 8: Introduction to Computer Science, Winter 2018 Lecture #15

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

MIPS Assembly: Practice Questions for Midterm 1 Saving to and Loading from Memory CS 64: Computer Organization and Design Logic Lecture #6

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

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

File I/O CS 16: Solving Problems with Computers I Lecture #9

Vectors and Pointers CS 16: Solving Problems with Computers I Lecture #13

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

File I/O in Python CS 8: Introduction to Computer Science Lecture #11

Strings in Python 1 Midterm#1 Exam Review CS 8: Introduction to Computer Science Lecture #6

Numerical Conversions Intro to Strings in C/C++ CS 16: Solving Problems with Computers I Lecture #8

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

File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9

CS 106B, Lecture 1 Introduction to C++

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

More on File I/O Strings in C++ CS 16: Solving Problems with Computers I Lecture #10

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

MIPS Instruc,ons CS 64: Computer Organiza,on and Design Logic Lecture #8

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

Dynamic Arrays Makefiles and Mul3ple File Compiles

File I/O and String Manipula3ons CS 16: Solving Problems with Computers I Lecture #11

Introduc)on to Arrays in C++ Review for Midterm #2 CS 16: Solving Problems with Computers I Lecture #12

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

PIC 10A. Review for Midterm I

File I/O in Python Formats for Outputs CS 8: Introduction to Computer Science, Winter 2018 Lecture #12

More on Lists, Dictionaries Introduction to File I/O CS 8: Introduction to Computer Science Lecture #10

Flow Control in C++ Condi&onals & Loops CS 16: Solving Problems with Computers I Lecture #4

Week 1: Hello World! Muhao Chen

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

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

CIS 130 Exam #2 Review Suggestions

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

Getting started with C++ (Part 2)

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science, Winter 2018 Lecture #9

CprE 281: Digital Logic

CprE 281: Digital Logic

CS Exam 2 Study Suggestions

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

Python, Part 2 CS 8: Introduction to Computer Science Lecture #4

Finding Pi: Applications of Loops, Random Numbers, Booleans CS 8: Introduction to Computer Science, Winter 2018 Lecture #6

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

Fundamentals of Structured Programming

Python Lists and Dictionaries CS 8: Introduction to Computer Science, Winter 2019 Lecture #13

CS 241 Data Organization. August 21, 2018

CS 101 Computer Programming and Utilization. Lecture 5, More Numerical computing (Slides courtesy Prof Ranade)

Computer Programming. Dr. Fahad Computer Science Deptt.

Structures and Classes CS 16: Solving Problems with Computers I Lecture #15

File Input/Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #10

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

EL2310 Scientific Programming

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Practice test for midterm 1

Introduction to Scientific Computing with C++ Introduction Textbook Assignments Lab Accounts and Software HELLO ADD INTS MANDEL Conclusion

CSCI 111 Midterm 2 Exam Spring Solutions 09.00am 09.50am, Wednesday, May 04, 2016

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

C C C C++ 2 ( ) C C++ 4 C C

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Computer Science II Lecture 1 Introduction and Background

Call- by- Reference Func0ons Procedural Abstrac0ons Numerical Conversions CS 16: Solving Problems with Computers I Lecture #9

CS1 Lecture 3 Jan. 18, 2019

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

CSCI 1061U Programming Workshop 2. C++ Basics

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

PIC 10A. Final Review: Part I

Your First C++ Program. September 1, 2010

Do not turn to the next page until the start of the exam.

PASS$MOCK$EXAM$ $FOR$PRACTICE$ONLY$

Welcome to Computer Organization and Design Logic

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

MIDTERM EXAM THURSDAY MARCH

Fundamentals of Programming CS-110. Lecture 2

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

Scientific Computing

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Announcements. 1. Forms to return today after class:

Python Lists 2 CS 8: Introduction to Computer Science Lecture #9

Unit 7. 'while' Loops

Transcription:

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6 Ziad Matni Dept. of Computer Science, UCSB

Administrative CHANGED T.A. OFFICE/OPEN LAB HOURS! Thursday, 10 AM 12 PM Muqsit Nawaz Friday, 11 AM 1 PM Xiyou Zhou Linux Workshop THIS Week! HFH Conference Room (HFH 1132) Friday, April 20 th, 1:00 2:30 PM Material will be put up on the class website Your 1 st Midterm Exam is NEXT TUESDAY (4/24)!!! Omgomgomgomgomgomgomgomgomgomg 4/19/18 Matni, CS16, Sp18 2

Tuesday, 4/24 in this classroom Starts at 2:00 PM **SHARP** Please start arriving 5-10 minutes before class I may ask you to change seats Please bring your UCSB IDs with you Closed book: no calculators, no phones, no computers Only allowed ONE 8.5 x11 sheet of notes one sided only You have to turn it in with your exam You will write your answers on the exam sheet itself. 4/19/18 Matni, CS64, Wi18 3

What s on the Midterm#1? From the Lectures, including Intro to Computers, Programming, and C++ Variables and Assignments Boolean Expressions (comparison of variables) Input and Output on Standard Devices (cout, cin) Data Types, Escape Sequences, Formatting Decimal Arithmetic Operations and their Priorities Boolean Logic Operators Flow of Control & Conditional Statements Loops: for, while, do-while Types of Errors in Programming Multiway Branching and the switch command Generating Random Numbers Functions in C++: pre-defined, user-defined void functions, the main() function call-by-ref vs. call-by-value Command Line Inputs to C++ Programs 4/19/18 Matni, CS16, Sp18 4

Midterm Prep 1. Lecture slides 2. Lab programs 3. Homework problems 4. Book chapters 1 thru 5* *check which lecture slides go with it!! 4/19/18 Matni, CS16, Sp18 5

Example Questions for Midterm #1 4/19/18 Matni, CS16, Sp18 6

Sample Question Multiple Choice Complete the following C++ code that is supposed to print the numbers 2 3 4 5 6 (with spaces in between): int c = 0; while ( ) { cout << c+2 << ; c++; } A. c < 7 B. c > 5 C. (c + 2) < 6 D. (c + 2)!= 6 E. c < 5 4/19/18 Matni, CS16, Sp18 7

Sample Question Multiple Choice What is the exact output of this C++ code? int prod(1); for (int m = 1; m <= 5; m += 2) { prod *= m; } cout << Total product is: << prod << endl; A. Total product is: 720 B. Total product is: 90 C. Total product is: 15 D. Total product is: 3 E. Total product is: 1 4/19/18 Matni, CS16, Sp18 8

#include <iostream> using namespace std; int findmax2(int a, int b); int main() { int x, y; cout << "Enter 2 numbers: "; cin >> x >> y; cout << "The biggest of these is: " << findmax2(x, y); return 0; } int findmax2(int a, int b) { } int max = a; if (b > a) { max = b; } return max; 4/19/18 Matni, CS16, Sp18 Sample Question Short-Answer Coding Complete this program in C++. Use what s given as clues for what s missing. 9

Sample Question Coding Syntax: Find 10 Mistakes (ignore styling) 2: Should be: <string> 3: Should be: using namespace std; 6: Should be: int number, x = 0; 9: Should be: \n 10: Missing ; at the end 11: Should be: cin >> word; 14: Must remove the ; at the end 16: Should be: cout << word << ; 17: Should be: x++ 19: Should be: cout << endl; return 0; 4/19/18 Matni, CS16, Sp18 10

Sample Question Short Program If we list all the integer numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Write a C++ program that can find the sum of all the integer multiples of 3 or 5 below any number n that is given via standard input.

Sample Question Short Program If we list all the integer numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Write a C++ program that can find the sum of all the integer multiples of 3 or 5 below any number n that is given via standard input. #include <iostream> using namespace std; int main() { int sum(0), int n; cout << "Enter n: "; cin >> n; for(int i = 1; i < n; i++) { if ((i % 3 == 0) (i % 5 == 0)) { sum += i; } } return 0; } 12

Lecture Outline Using and Interpreting Gradescope More Examples of Functions in C++ More Examples of Command-Line Use in C++ Makefiles 4/19/18 Matni, CS16, Sp18 13

A summary of what tests failed and what passed Interpretation: Student probably did not account for this type of error 4/19/18 Matni, CS16, Sp18 14

Watch Out For The use of cerr vs cout (esp. in this lab) Use cerr when relaying error messages Use cout for regular standard output When you create your programs, test them with as many different scenarios and edge cases as you can So that you can catch errors and understand where/why they occur 4/19/18 Matni, CS16, Sp18 15

function_example1.cpp More DEMOS! JJJ function_example2.cpp args.cpp 4/19/18 Matni, CS16, Sp18 16

YOUR TO-DOs q STUDY FOR YOUR EXAM! J q Finish Lab3 by next Monday q Prepare Lab4 for next Wednesday q Do HW6 by next Tuesday q Go to the Linux Workshop on Friday (optional, but recommended)! q Visit Prof s and TAs office hours if you need help! q Call your parents and say hello 4/19/18 Matni, CS16, Sp18 17

4/19/18 Matni, CS16, Sp18 18