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

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

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

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

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

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

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

Chapter 4. Procedural Abstraction and Functions That Return a Value

Chapter Procedural Abstraction and Functions That Return a Value. Overview. Top-Down Design. Benefits of Top Down Design.

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

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

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

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

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

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

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

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

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

Lab Instructor : Jean Lai

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

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

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

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

Top-Down Design Predefined Functions Programmer-Defined Functions Procedural Abstraction Local Variables Overloading Function Names

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

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

C++ Programming Lecture 11 Functions Part I

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Chapter 3 Function Basics

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

Programming Language. Functions. Eng. Anis Nazer First Semester

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

Chapter Four: Loops II

Welcome to Solving Problems with Computers I

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

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

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

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

Dynamic Arrays Makefiles and Mul3ple File Compiles

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

CS 101: Computer Programming and Utilization. Abhiram Ranade

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

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

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

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

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

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

Computing and Statistical Data Analysis Lecture 3

Chapter 6 - Notes User-Defined Functions I

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

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

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

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science Lecture #7

PIC 10A. Review for Midterm I

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

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

a. a * c - 10 = b. a % b + (a * d) + 7 =

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

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

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

C++ basics Getting started with, and Data Types.

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

Chapter 3 - Functions

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

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

Functions and Recursion

(created by professor Marina Tanasyuk) FUNCTIONS

CS103L FALL 2017 UNIT 1: TYPES, VARIABLES,EXPRESSIONS,C++ BASICS

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.

CSCE 110 PROGRAMMING FUNDAMENTALS

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

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

Engineering Problem Solving with C++, Etter/Ingber

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

3.1. Chapter 3: The cin Object. Expressions and Interactivity

Using Free Functions

CSCI 1061U Programming Workshop 2. Function Basics

Introduc)on to C++ CS 16: Solving Problems with Computers I Lecture #2

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

Introduction. What is function? Multiple functions form a larger program Modular programming

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

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

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

Partha Sarathi Mandal

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

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

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

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

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

CSCE Practice Midterm. Data Types

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

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

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

CS 221 Lecture. Tuesday, 4 October There are 10 kinds of people in this world: those who know how to count in binary, and those who don t.

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

Fundamentals of Structured Programming

Local and Global Variables

Transcription:

Pre- Defined Func-ons in C++ Review for Midterm #1 CS 16: Solving Problems with Computers I Lecture #6 Ziad Matni Dept. of Computer Science, UCSB

Announcements Homework #5 due today Homework #6 issued today is due NEXT THURSDAY Because next Tuesday is midterm#1 exam Lab #3 is due Monday, 5/1 Your grades are now online! Access them through the class website and click on Class Grades, CMPSC 16, Spring 2017 Don t forget your TAs and Instructor s office hours!! J 4/20/17 Matni, CS16, Sp17 2

Material: Everything we ve done, incl. up to Th. 4/20 Homework, Labs, Lectures, Textbook Tuesday, 4/25 in this classroom Starts at 12:30pm **SHARP** (come early) Pre- assigned sea-ng DuraZon: 1 hour long Closed book: no calculators, no phones, no computers Only 1 sheet (single- sided) of wri_en notes Must be no bigger than 8.5 x 11 You have to turn it in with the exam You will write your answers on the exam sheet itself. 4/20/17 Matni, CS16, Sp17 3

4/20/17 Matni, CS16, Sp17 4

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, Formaing Decimal ArithmeZc OperaZons and their PrioriZes Flow of Control & CondiZonal Statements Boolean Logic Operators Loops: for, while, do while Types of Errors in Programming MulZway Branching and the switch command Command Line Inputs to C++ Programs FuncZons in C++ GeneraZng Random Numbers 4/20/17 Matni, CS16, Sp17 5

Ch. 1 Ch. 2 Ch. 3 Ch. 4 What s on the Midterm#1? Readings from the Textbook SecZon 4.2, 4.3, 4.4, 4.5 4/20/17 Matni, CS16, Sp17 6

What s on the Midterm#1? From the Assignments and Labs Review homework queszons Review labs and understand what you did The lab processes and experiences, especially The process of wrizng a program and compiling it The basic UNIX commands you use in lab: cd, ls, g++ 4/20/17 Matni, CS16, Sp17 7

Sample QuesZon Mul=ple Choice Complete the following C++ code that is supposed to print the numbers 2, 3, 4, 5, 6: 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/20/17 Matni, CS16, Sp17 8

Sample QuesZon Mul=ple Choice What is the exact output of this C++ code? int prod(1); for (int m = 1; m <= 9; m++) { prod *= m; m += 2; } cout << Total product is: << prod << endl; A. Total product is: 720 B. Total product is: 120 C. Total product is: 28 D. Total product is: 2 E. Total product is: 1 4/20/17 Matni, CS16, Sp17 9

Sample QuesZon Short- Answer Coding Write C++ code showing a funczon definizon of distance( ) which takes 4 int values x 1, x 0, y 1, and y 0 and returns a double data type that s equal to (x 1 - x 0 ) 2 + (y 1 - y 0 ) 2. Assume that the cmath lib has been imported. double distance(int x1, int x0, int y1, int y0) { double a = pow(x1 x0, 2); Note: double b = pow(y1 y0, 2); When I ask for code, that means not a double z = sqrt(a + b); complete program. return z; } Otherwise I d ask for a program. Also, this would be clear from the ques=on. 4/20/17 Matni, CS16, Sp17 10

Sample QuesZon Coding Syntax: Find The 10 Mistakes 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/20/17 Matni, CS16, Sp17 11

4/20/17 Matni, CS16, Sp17 12

Example of a Simple FuncZon in C++ #include <iostream> using namespace std; int sum2nums(int num1, int num2); int main ( ) { int a(3), b(5); int sum = sum2nums(a, b); cout << sum << endl; return 0; } int sum2nums(int num1, int num2) { return (num1 + num2); } Declara-on Call Defini-on 4/20/17 Matni, CS16, Sp17 13

Predefined FuncZons in C++ C++ comes with built- in libraries of predefined funczons Example: sqrt funczon (found in the library cmath) Computes and returns the square root of a number the_root = sqrt(9.0); The number 9 is called the argument Axer calculazon, the variable the_root will be equal to 3.0 Can variable the_root be either int or double? 4/20/17 Matni, CS16, Sp17 14

Other Predefined cmath FuncZons pow(x, y) - - - double value = pow(2, - 8); Returns 2-8, a double value (0.00390625) Arguments are of type double sin(x), cos(x), tan(x), etc - - - double value = sin(1.5708); Returns sin(π/2) (equal to 1) note it s in radians Argument is of type double 4/20/17 Matni, CS16, Sp17 15

Other Predefined cmath FuncZons abs(x) - - - int value = abs(- 8); Returns absolute value of argument x Return value is of type int Argument is of type int fabs(x) - - - double value = fabs( 8.0); Also returns absolute value of argument x Return value is of type double Argument is of type double 4/20/17 Matni, CS16, Sp17 16

Random Number GeneraZon: Step 1 Not true- random, but pseudo- random numbers. Must #include <cstdlib> #include <ctime> First, seed the random number generator only once srand(time(0)); //place inside main( ) -me( ) is a pre- defined funczon in the c-me library (it gives the current system Zme) It s used here because it generates a dis=nc=ve enough seed, so that rand( ) generates a good enough random number. 4/20/17 Matni, CS16, Sp17 17

Random Number GeneraZon: Step 2 Next, use the rand( ) funczon, which returns a random integer that is greater than or equal to 0 and less than RAND_MAX (a library- dependent value, but is at least 32767) int r = rand(); But what if you want to generate random numbers in other ranges? Example, between 1 and 6? 4/20/17 Matni, CS16, Sp17 18

Random Numbers Use % and + to scale to the number range you want For example to get a random number bounded from 1 to 6 to simulate rolling a six- sided die: int die = (rand( ) % 6) + 1; 4/20/17 Matni, CS16, Sp17 19

What Will This Program Print Out? #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main ( ) { srand(time(0)); int throw_times, die; cout << How many times shall we throw the die?!\n ; cin >> throw_times; for (int i=0; i < throw_times; i++) { die = (rand( ) % 6) + 1; cout << We threw a << die << endl; } return 0; } 4/20/17 Matni, CS16, Sp17 20

Type Casting Recall the problem with integer division in C++: int total_candy = 9, number_of_people = 4; double candy_per_person; candy_per_person = total_candy / number_of_people; candy_per_person = 2, not 2.25! A Type Cast produces a value of one type from another static_cast<double>(total_candy) produces a double representing the integer value of total_candy 4/20/17 Matni, CS16, Fa16 21

Type Cast Example int total_candy = 9, number_of_people = 4; double candy_per_person; candy_per_person = static_cast<double>(total_candy)/number_of_people; candy_per_person now is 2.25! The following would also work: candy_per_person = total_candy / static_cast<double>(number_of_people); Integer division occurs This, however, would not! before type cast! candy_per_person = static_cast<double> (total_candy / number_of_people); 4/20/17 Matni, CS16, Fa16 22

Question Can you determine the value of d? double d = 11 / 2; Integer division occurs before type cast! What about this value of d? double d = 11.0 / 2.0; 4/20/17 Matni, CS16, Fa16 23

TO DOs Finish reading Chapter 4 for Thursday s class Finish Homework6 (due Thursday 4/27) Finish Lab3 (due Monday 5/1) Prep for Lab4 next Wednesday (due Tuesday 5/2) Study for Midterm #1!!!! J Come see the prof. or the TAs during office hours if you have queszons 4/20/17 Matni, CS16, Sp17 24

4/20/17 Matni, CS16, Sp17 25