More loops Ch

Similar documents
Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Highlights. - Making threads. - Waiting for threads. - Review (classes, pointers, inheritance)

Introduction to Programming (Java) 4/12

Announcements. Lab 1 this week! Homework posted Thursday -Due next Thursday at 11:55pm

Understand Execution of a Program

Announcements. Homework 0 out! Coming soon: 1. remote access 2. programming instructions 3. lab 1 (graded!) this week

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

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

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

CSCI Wednesdays: 1:25-2:15 Keller Thursdays: 4:00-4:50 Akerman 211

QUIZ Friends class Y;

Announcements. Lab 1 this week! Homework posted Wednesday (late)

CSCE 206: Structured Programming in C++

Functions BCA-105. Few Facts About Functions:

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

Chapter 5 C Functions

Announcements. HW0 is posted on schedule, due next Friday at 9pm (pretty easy)

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Lecture 5: Methods CS2301

Methods. Contents Anatomy of a Method How to design a Method Static methods Additional Reading. Anatomy of a Method

Programming Language. Functions. Eng. Anis Nazer First Semester

Announcements. HW 0 due tonight (check that it compiles on CSE labs) (make sure you submit the code part)

CS113: Lecture 4. Topics: Functions. Function Activation Records

bool bool - either true or false

LECTURE 5 Control Structures Part 2

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Lab Instructor : Jean Lai

Outline. Functions. Functions. Predefined Functions. Example. Example. Predefined functions User-defined functions Actual parameters Formal parameters

Project Compiler. CS031 TA Help Session November 28, 2011

QUIZ. What are 3 differences between C and C++ const variables?

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

CSE143 Exam with answers MIDTERM #1, 1/26/2001 Problem numbering may differ from the test as given.

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

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

M.CS201 Programming language

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Problem Solving with C++

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

a data type is Types

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

Chapter 1 - What s in a program?

Functions. Systems Programming Concepts

Appendix: Common Errors

Chapter 5 Errors. Hyunyoung Lee. Based on slides by Bjarne Stroustrup.

Errors. Lecture 6. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

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

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

ITP 342 Mobile App Dev. Fundamentals

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

CIS 130 Exam #2 Review Suggestions

PIC 10A Objects/Classes

C++ Functions. Last Week. Areas for Discussion. Program Structure. Last Week Introduction to Functions Program Structure and Functions

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

Chapter 4. Procedural Abstraction and Functions That Return a Value

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

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

About this exam review

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

Assertions, pre/postconditions

Chapter 1. Section 1.4 Subprograms or functions. CS 50 - Hathairat Rattanasook

Functions and an Introduction to Recursion Pearson Education, Inc. All rights reserved.

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Computer Science II Lecture 1 Introduction and Background

Chapter 5 Errors. Bjarne Stroustrup

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

Scientific Computing

All about flow control

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren

C++ and OO. l C++ classes and OO. l More Examples. l HW2. C++ for C Programmers by Ira Pohl

Objectives. In this chapter, you will:

QUIZ. What is wrong with this code that uses default arguments?

Memory, Arrays, and Parameters

C Functions. 5.2 Program Modules in C

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

EL6483: Brief Overview of C Programming Language

CS201 - Introduction to Programming Glossary By

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

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

Functions! Objectives! 1E3! Topic 9! programming! n This topic should allow students to! n Read chapter 6 of the textbook now.!

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

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Lecture 04 FUNCTIONS AND ARRAYS

Chapter 2 - Control Structures

COMP322 - Introduction to C++ Lecture 01 - Introduction

Unit 6. Thinking with Functions

Functions and Recursion

C++ For Science and Engineering Lecture 12

Lesson 2 Variables and I/O

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

Lecture 5 Tao Wang 1

Your first C++ program

Chapter 3 - Functions

Introduction to Programming

Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)

Transcription:

More loops Ch 3.3-3.4

Announcements Quiz next week! -Covers up to (and including) HW1 (week 1-3) -Topics: cout/cin, types, scope, if/else, etc.

Review: Loops We put a loop around code that we want to run more than once If we have an easy sequence (0, 1, 2,... 10) of values we want to go over, for loop is nice Otherwise, the while loop is a bit more general and is typically more useful if we are asking the user to control the loop

Review: Loops Write a program that asks the user to input a value, then show the sum from 1 to that value in the following format: Find the sum from 1 to what value? 5 1+2+3+4+5 = 15

Nested for loop Now modify the code so it shows all sums less than or equal to the entered values, as such: Find the sum from 1 to what value? 4 1 = 1 1+2 = 3 1+2+3 = 6 1+2+3+4 = 10

Nested for loop Like nested if statements, we can also make nested loops (which can cause headaches) It might help to think of each loop as an added dimensions: 1 loop = 1 dimension (line/ruler) 2 loops = 2 dimensions (plane/square/area) 3 loops = 3 dimensions (volume/cube)... (See: nestedloop.cpp)

Nested for loop Ask the user for a size of matrix, then show the identity matrix for that dimension: What 100 010 001 000 size? 0 0 0 1 4

Overview Peripheral file I/O op. overload recursion pointers Advanced dynamic memory Very Useful scope string classes functions loop types if/else ops arrays Essentials

Functions Ch 4-5

Functions So far we have been writing code inside main() without understanding some parts of it copy paste this, else computer throws fit Why zero? Dunno what this does but I can forget it and computer doesn't care

Functions Can think of methods as packaging multiple commands into one

Functions An analogy might be a wallet/purse If you want to pay someone, it is easier to find your cash/card/check if organized

Functions (Side note: you want to keep functions as simple as possible... if you try to use them to do too many things, they get bulky and harder to use)

Functions We have used functions before, such as sqrt(), pow() or possibly round() You can also create your own similar to creating variables by: (1) declaring the function (2) defining what the function does (See: sayhi.cpp)

Functions Function declaration (put before main or any other definition) Function definition

Functions Functions, like variables, have types (int, double, char, etc.) We call them the return value, as it is what the function will become after being finished For example: sqrt(4) will become 2.0 (double) when it is finished (See: addition.cpp)

Functions function header return type (whole line) parameters (order matters!) return statement body The return statement value must be the same as the return type (or convertible) (See addition2.cpp)

Functions You can actually have multiple functions with the same name, as long as the arguments are different either by: - a different amount of arguments - different types of arguments This is called overloading a function (See overloading.cpp)

Functions You can make functions return type void, but not variables (an empty variable? ehh...) This means nothing is returned, so you will get an error if you say: void x();... then... int y = x(); // x not an int! or anything! void functions might just print out something

Functions (See maze.cpp)

Functions It is important to note that the code will resume after the function call where it was used For example, sqrt(4) will return the value 2.0 where it was used and the rest of your code will continue Where does the maze code return to?

Functions Multiple function uses/calls create a stack much like pancakes: every time you use a function, it will add another pancake When you return, the top pancake is removed main() is the bottom pancake

Functions How to make the person run? (See: runforest.cpp)

Functions You can also use functions that return bool types in an if statement or loop This is commonly used if you have complex logic as it is normally easier to write a function that have a very complex bool expression (See: findprime.cpp)

scope (See: sillyswap.cpp) Typically the value of variables is copied and not given access to the real value This is similar to moodle, the score you see for grades cannot change the score I give you!

scope Blocks (inside { }) of code can only see variables from their parent blocks You can also make global variables outside of all blocks (almost as if your whole program has a start and end brace around it) (See: globalvariable.cpp)

scope You can give away your memory location by using call by reference with functions This will share the variable between the two functions, namely the function that is using the references (&) can modify the value (See: callbyreferenceswap.cpp)

scope We will talk more about the difference between a variable's memory location and value later For now, a memory location (or reference) will give a function full access to modify the value

References When memory does not actually hold the value of an object, but instead holds information about the actual location... (See: changeint.java)... this is called a reference

References If you use a normal function (call by value) then you will essentially make a photo copy of the variables (makes 2 variables, does not effect other) If you use call-by-reference, you have only one variable, but share it between you two This is similar to a website link, if two people follow the link they end up in the same page

Debugging -Test small pieces of code at a time -Add cout statements to see values in loops (and to localize error in general) -Test code on inputs you know the answer