Week 4 EECS 183 MAXIM ALEKSA. maximal.io

Similar documents
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 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Lab Instructor : Jean Lai

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

EECS402 Lecture 02. Functions. Function Prototype

Programming Language. Functions. Eng. Anis Nazer First Semester

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

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

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:

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

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

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

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

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

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:

Increment and the While. Class 15

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

Computer Science II Lecture 1 Introduction and Background

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

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

CSCI 111 Midterm 1 Exam Spring Solutions 09.00am 09.50am, Monday, March 21, 2016

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CSCE 206: Structured Programming in C++

PIC 10A. Review for Midterm I

Computer Programming

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

University of Michigan EECS 183: Elem. Programming Concepts Fall 2011 Exam 1: Part 1: Form 1. Professors: ML Dorf, Elliot Soloway

To become familiar with array manipulation, searching, and sorting.

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

Scientific Computing

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Local and Global Variables

Your First C++ Program. September 1, 2010

WARM UP LESSONS BARE BASICS

Ch 6. Functions. Example: function calls function

4. C++ functions. 1. Library Function 2. User-defined Function

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

Fundamentals of Programming CS-110. Lecture 2

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

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

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Name SECTION: 12:45 2:20. True or False (12 Points)

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

CS Homework 10 p. 1. CS Homework 10

CS 106B Lecture 2: C++ Functions

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

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

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

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

Name Section: M/W or T/TH. True or False (14 Points)

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

C++ Support Classes (Data and Variables)

Expressions, Input, Output and Data Type Conversions

A SHORT COURSE ON C++

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

Computer Programming

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

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

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

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Introduction to Programming using C++

Spring CS Homework 12 p. 1. CS Homework 12

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

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

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

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

Week 1: Hello World! Muhao Chen

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

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

Problem Solving: Storyboards for User Interaction

Lab01: C++ Expressions ES036a: Programming Fundamentals Fall 2007

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

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

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.6

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Fundamentals of Programming. Lecture 19 Hamed Rasifard

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

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.

Programming. C++ Basics

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

CSCE Practice Midterm. Data Types

C++ Programming Lecture 11 Functions Part I

CIS 130 Exam #2 Review Suggestions

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

EP578 Computing for Physicists

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

C++ Programming: From Problem Analysis to Program Design, Third Edition

FOR loops! Objectives! 1E3! Topic 7! n To be able to write a FOR statement! n To be able to predict what a given FOR statement will do.!

Introduction to C++ Dr M.S. Colclough, research fellows, pgtas

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Transcription:

Week 4 EECS 183 MAXIM ALEKSA maximal.io

Agenda Functions Scope Conditions Boolean Expressions Lab 2 Project 2 Q&A

Lectures 15% 36% 19% 8:30am 10:00am with Bill Arthur 10:00am 11:30am with Mary Lou Dorf 1:00pm 2:30pm with Héctor García-Ramírez 4pm 5:30pm with Bill Arthur 30%

Enrolled

Favorite apps

maximal.io/eecs183 slides, notes, source code, links, et cetera

Functions

Functions side effects 0 or more inputs 0 or 1 output Black box

Functions with return value int score0 = 100; int score1 = 80; int bestscore = max(score0, score1); cout << "The best score is " << bestscore << endl;

Moose or Meese? cout << pluralize("moose", "meese", n);

Singular or plural pluralstring singularstring number singularstring if number is 1 pluralstring otherwise pluralize

Singular or plural 4 "gallons" "gallons" "gallon" pluralize

Functions with side effects int main() { } printheader(); void printheader() { } cout << "Shopping List for Cupcakes" << endl;

Functions with side effects int main() { sayhello("hannah"); sayhello("michael"); } void sayhello(string name) { } cout << "Hello, " << name << "!" << endl;

Why functions? Organization Simplification Reusability

Nested expressions double result = pow(sqrt(4), ceil(abs(-2) + 0.75));

Libraries iostream string cmath algorithm

Defining a function int add(int a, int b) { int sum = a + b; return sum; }

Defining a function function header / signature int add(int a, int b) { int sum = a + b; return sum; }

Defining a function header / signature int add(int a, int b) { body int sum = a + b; return sum; }

Defining a function return type int add(int a, int b) { int sum = a + b; return sum; }

Defining a function name return type int add(int a, int b) { int sum = a + b; return sum; }

Defining a function name parameter list return type int add(int a, int b) { int sum = a + b; return sum; }

Defining a function header / signature int add(int a, int b) { body int sum = a + b; return sum; }

Defining a function int add(int a, int b) { int sum = a + b; return statement } return sum;

Declaring a function name parameter list return type int add(int a, int b);

Using a function #include <iostream> using namespace std; int add(int a, int b); int main() { int sum = add(1, 2); cout << sum << endl; } int add(int a, int b) { int sum = a + b; return sum; }

Using a function #include <iostream> using namespace std; prototype / declaration function call int add(int a, int b); int main() { int sum = add(1, 2); cout << sum << endl; } implementation / definition int add(int a, int b) { int sum = a + b; return sum; }

RME /** * Requires: Nothing. * Modifies: Nothing. * Effects: Returns sum of a and b. */ int add(int a, int b); int add(int a, int b) { int result = a + b; return result; }

RME /** * Requires: n >= 0. * Modifies: Nothing. * Effects: Returns square root of n. */ double sqrt(double n); double sqrt(double n) { double result =??? return result; }

RME /** * Requires: Nothing. * Modifies: cout. * Effects: Says hello to someone with name name. */ void sayhello(string name); void sayhello(string name) { } cout << "Hello " << name << "!\n";

Scope main sqrt x: 5 n: 5

Scope increment-0.cpp increment-1.cpp increment-2.cpp

Scope Local variables (defined inside functions) are only known within functions wherein they were defined. Local variables are passed by value to other functions. That means that other functions receive a copy of the value, not the variable itself. The variable in the original function is unchanged unless overwritten.

scope.cpp #include <iostream> using namespace std; // prototypes void foo(); void bar(); // global variable int x = 5; int main() { cout << x << endl; int x = 7; cout << x << endl; foo(); cout << x << endl; bar(); } void foo() { x = 10; int x = 42; cout << x << endl; } void bar() { cout << x << endl; }

Swap 2 a b 5

Swap int a = 183; int b = 281; int temp = a; a = b; b = temp; cout << "a is " << a << endl; cout << "b is " << b << endl;

Swap void swap(int a, int b) { int temp = a; a = b; b = temp; }

Deadlines Today 27 Assignment 2 Monday 30 Lab 2 Friday 3 Project 2

Comparison Operators < <= x > >= y ==!=

Conditions if (condition) { } // do this

Conditions if (condition) { // do this } else { // do that }

Conditions if (course == 183) { cout << "EECS 183 is Elementary Programming Concepts" << endl; } else { cout << "I'm not familiar with that class!" << endl; }

Conditions if (condition) { // do this } else if (condition) { // do that } else { // do this other thing }

Conditions if (course == 183) { cout << "EECS 183 is Elementary Programming Concepts" << endl; } else if (course == 203) { cout << "EECS 203 is Discrete Mathematics" << endl; } else { cout << "I'm not familiar with that class!" << endl; }

Conditions if (x < y) { cout << "x is less than y" << endl; } else if (x > y) { cout << "x is greater than y" << endl; } else { cout << "x is equal to y" << endl; }

Conditions conditions-0.cpp conditions-1.cpp

True or false? if is a function. if (condition) { } // do this

True or false? Every if statement needs an else statement.

Conditions if (condition) { // do something } else { // do nothing }

Conditions if (condition) { // do something } else { // do nothing }

True or false? Every else statement must go with an if statement.

True or false? Order of if / else if / else statements matters.

Logical Operators &&!

Boolean Expressions if (condition && condition) { // do this if both conditions are true } if (condition condition) { // do this either condition is true } if (!condition) { // do this if condition is false }

Conditions if (condition) { // do nothing } else { // do something }

Conditions if (condition) { // do nothing } else { // do something }

Conditions if (!condition) { } // do something

Boolean Expressions Write a boolean expression in C++ that means a < b < c in a math class. Hint: this boolean expressions checks if a is equal to b: a == b

Operator Precedence Level Operator 2 ++ -- () []. 3 ++ --! + - & 5 * / % 6 + - 7 << >> 8 < > <= >= 9 ==!= 13 && 14 15 = = = %= += =

conditions-2.cpp Ask the user for an integer between 1 and 9. Say if that number is small (1, 2, 3), medium (4, 5, 6), large (7, 8, 9) or invalid (not between 1 and 9). Give me an integer between 1 and 9: 7 You typed a large number. Give me an integer between 1 and 9: 183 You typed an invalid number.

triangles-0.cpp Write a program that asks the user for (three) side lengths of a triangle and then prints if the triangle is valid or invalid. valid What is the length of side a? 3 What is the length of side b? 2 What is the length of side c? 10 Invalid invalid

triangles-1.cpp Write a program that asks the user for (three) side lengths of a triangle and then prints the type of the triangle (equilateral, isosceles, scalene, invalid). equilateral isosceles scalene invalid

Style

eecs183.org/docs/style

Indentation Indent each level of code by four spaces (usually can press Tab) int main() { int n; cin >> n; if (n > 0) { cout << "Thanks for the positive int!" << endl; } }

Silicon Valley http://www.hbo.com/silicon-valley

Lab 2 eecs183.org/labs/2 Exam question Implement getstudentfullname Implement main Enter student's name: Amy Enter the number of credits for Amy: 32 Amy is a sophomore Testing Implement test_isgregoriandate Implement test_isleapyear Submit lab2.cpp

Project 2 eecs183.org/projects/2 Implement Birthdays Implement functions Test functions Implement main Read date from user and print day of week Submit birthdays.cpp ******************************* Birthday Calculator ******************************* Menu Options ------------ 1) Determine day of birth 2) Print the next 10 leap years 3) Determine birthdays for the next 10 years 4) Finished Choice --> 1 Enter your date of birth format: month / day / year --> 1 / 27 / 2017 You were born on a: Friday Have a great birthday!!! Menu Options ------------ 1) Determine day of birth 2) Print the next 10 leap years 3) Determine birthdays for the next 10 years 4) Finished Choice --> 4 **************************************************** Thanks for using the Birthday Calculator ****************************************************