CSC 221: Computer Programming I Fall 2001  top-down design  approach to problem solving, program design  focus on tasks, subtasks, …  reference parameters.

Slides:



Advertisements
Similar presentations
CSC 221: Computer Programming I Fall 2001  conditional repetition  repetition for: simulations, input verification, input sequences, …  while loops.
Advertisements

Parameter Passing Mechanisms Reference Parameters.
1 CSC 222: Computer Programming II Spring 2004  vectors  library, templated  advantages over arrays:  const-reference parameters  growable vectors.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Chapter 5 Functions.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
Program Design and Development
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
Computer Science 1620 Programming & Problem Solving.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
1 CSC 221: Computer Programming I Fall 2004 course overview  what did we set out to learn?  what did you actually learn?  where do you go from here?
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
High-Level Programming Languages: C++
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Parameter Passing Mechanisms Reference Parameters Read § §
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Fundamental Programming: Fundamental Programming Introduction to C++
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
CPS120: Introduction to Computer Science Functions.
Previously Repetition Structures While, Do-While, For.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Parameter Passing Mechanisms Reference Parameters § §
CPS120: Introduction to Computer Science Lecture 14 Functions.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
1 CS161 Introduction to Computer Science Topic #9.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
Input Validation 10/09/13. Input Validation with if Statements You, the C++ programmer, doing Quality Assurance (by hand!) C++ for Everyone by Cay Horstmann.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Lecture 12: Functions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
CSC 221: Computer Programming I Fall 2001  arrays  homogeneous collection of items, accessible via an index  initializing/traversing/displaying arrays.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
C++ Programming Lecture 12 Functions – Part IV
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Introduction to Computer Programming
User-defined Functions
User-defined Functions
Let’s all Repeat Together
Fundamental Programming
Repetition Statements (Loops) - 2
Control Statements Paritosh Srivastava.
Presentation transcript:

CSC 221: Computer Programming I Fall 2001  top-down design  approach to problem solving, program design  focus on tasks, subtasks, …  reference parameters  provide mechanism for functions to "return" more than one value  reference vs. value parameters

programmer's toolbox you now have an extensive collection of programming tools for solving problems  variables – for storing values  expressions – for performing computations  functions – for computational abstraction  if statements – for conditional execution if (1-way), if-else (2-way), cascading if-else (multi-way)  while loops – for conditional repetition counters & sums for keeping running totals for loop variant if you know the number of repetitions problem-solving is all about organizing the right tools for the job (1)identify the tools that would be useful (2)sketch out a solution in pseudo-code (i.e., organize the tools) (3)fill in the details to complete the code

top-down design a useful approach to problems is to break a problem into tasks  then break tasks into subtasks, subtasks into sub-subtasks, …  eventually, subtask is simple enough to implement idea: plan how program will fit together before you code the details for example, consider HW3 (Sprint plan comparison) TASK 1: find out if user wants specific plan or comparison of all plans TASK 2: find out which plan and display results for that plan (if specific) TASK 3: compare and display results of all plans (if comparison) for each task, can define a function to carry it out

Sprint example // sprint.cpp // // Computes phone bill using various Sprint plans. /////////////////////////////////////////////////// #include using namespace std; int GetUserOption(); void ComparePlans(); void SpecificPlans(); int main() { cout << "Welcome to Dave's Sprint plan guide." << endl; int userOption = GetUserOption(); if (userOption == 1) { SpecificPlan(); } else { ComparePlans(); } return 0; } /////////////////////////////////////////////// block-structure diagram

Sprint (cont.) GetUserOption handles all the details of reading the user's choice simple enough to be coded  display menu of options  repeatedly read user input until valid (1 or 2)  return user input int GetUserOption() // Returns: user input, either 1 (for specific plan) or // 2 (for a comparison of all plans) { cout << endl << "Are you interested in:" << endl << " (1) a specific plan, or" << endl << " (2) a comparison of all plans?" << endl << "Select an option: "; int userOption; cin >> userOption; while (userOption != 1 && userOption != 2) { cout << "Illegal option. Enter 1 or 2: "; cin >> userOption; } return userOption; } note: you were not required to do this for HW3 (hadn’t seen loops yet)

Sprint (cont.) SpecificPlan is too complex a task to start coding – break down into subtasks TASK 1: get plan number TASK 2: get minutes (day & night) TASK 3: compute bill using specified plan TASK 4: display bill

Sprint (cont.) void SpecificPlan() // Results: computes and displays bill using all Sprint plans. { int plan = GetPlanNumber(); int dayMinutes, nightMinutes; GetMinutes(dayMinutes, nightMinutes); cout << endl << "Your bill would be:" << endl; if (plan == 1) { DisplayBill(AnyNickel(dayMinutes+nightMinutes), "Nickel Anytime"); } else if (plan == 2) { DisplayBill(AnySense(dayMinutes+nightMinutes), "Sense Anytime"); } else if (plan == 3) { DisplayBill(Any500(dayMinutes+nightMinutes), "500 Anytime"); } else if (plan == 4) { DisplayBill(Any1000(dayMinutes+nightMinutes), "1000 Anytime"); } else if (plan == 5) { DisplayBill(NightsNickel(dayMinutes, nightMinutes), "Nickel Nights"); } else if (plan == 6) { DisplayBill(Nights1000(dayMinutes, nightMinutes), "1000 Nights"); } problem: we want to delegate the task of reading day & night minutes to a function – but functions can only return a single value (LATER)

Sprint (cont.) int GetPlanNumber() // Returns: user input, integer between 1 and 6 { cout << endl << "Which plan are you interested in:" << endl << " (1) Sprint Nickel Anytime" << endl << " (2) Sprint Sense Anytime" << endl << " (3) Sprint 500 Anytime" << endl << " (4) Sprint 1000 Anytime" << endl << " (5) Sprint Nickel Nights" << endl << " (6) Sprint 1000 Nights" << endl << "Select an option: "; int plan; cin >> plan; while (plan 6) { cout << "Illegal option. Enter 1-6: "; cin >> plan; } return plan; } void DisplayBill(double cost, string planName) // Assumes: cost >= 0, planName is name of the billing plan // Results: displays the cost and name of the plan { cout << setiosflags(ios::fixed) << setprecision(2); cout << " $" << cost << " using Sprint " << planName << endl; }

Sprint (cont.) double AnyNickel(int totalMinutes) // Assumes: user spent totalMinutes on long distance calls // Returns: bill using Nickel Anytime plan { const double MONTHLY_FEE = 8.95; const double PER_MINUTE = 0.05; return MONTHLY_FEE + (PER_MINUTE * totalMinutes); } double Any500(int totalMinutes) // Assumes: user spent totalMinutes on long distance calls // Returns: bill using 500 Anytime plan { const double MONTHLY_FEE = 25.00; const double PER_MINUTE = 0.10; if (totalMinutes > 500) { return MONTHLY_FEE + (PER_MINUTE * (totalMinutes-500)); } else { return MONTHLY_FEE; } double Nights1000(int dayMinutes, int nightMinutes) // Assumes: number of long distance day and night minutes, resp. // Returns: bill using 1000 Nights plan { const double MONTHLY_FEE = 20.00; const double PER_MINUTE = 0.05; if (nightMinutes > 1000) { return MONTHLY_FEE + (PER_MINUTE * dayMinutes) + (PER_MINUTE * (nightMinutes-1000)); } else { return MONTHLY_FEE + (PER_MINUTE * dayMinutes) ; }

Sprint (cont.) similarly, ComparePlans can be broken down into subtasks TASK 1: get minutes (day & night) TASK 2: compute bill using all plans TASK 3: display bills

Sprint (cont.) void ComparePlans() // Returns: computes bill for user using different Sprint // plans and display the results { int dayMinutes, nightMinutes; GetMinutes(dayMinutes, nightMinutes); cout << endl << "Your bill would be:" << endl; DisplayBill(AnyNickel(dayMinutes+nightMinutes), "Nickel Anytime"); DisplayBill(AnySense(dayMinutes+nightMinutes), "Sense Anytime"); DisplayBill(Any500(dayMinutes+nightMinutes), "500 Anytime"); DisplayBill(Any1000(dayMinutes+nightMinutes), "1000 Anytime"); DisplayBill(NightsNickel(dayMinutes, nightMinutes), "Nickel Nights"); DisplayBill(Nights1000(dayMinutes, nightMinutes), "1000 Nights"); } note: we have already defined functions for the necessary tasks (due to overlap between SpecificPlans and ComparePlans  computational abstraction, code reuse

reference parameters top-down design involves breaking a problem into tasks  define a function for each task, divide into subtasks as necessary  must be able to pass information as needed between subtasks (functions)  can pass information into a function via parameters  can pass a single value back out as the function return value  what if a function needs to return more than one value? C++ provides an alternative method of parameter passing  reference parameters allow functions to pass values back to the calling environment  identified by placing '&' in between the parameter type and name in the function definition

Sprint (cont.) void GetMinutes(int & day, int & night) // Results: reads day and night minutes from user, returns via parameters { cout << endl; cout << "How many daytime (7am to 7pm) minutes did you foresee using in a month? "; cin >> day; while (day < 0) { cout << "You can't use negative minutes. Try again: "; cin >> day; } cout << "How many night (7pm to 7am) minutes did you foresee using in a month? "; cin >> night; while (day < 0) { cout << "You can't use negative minutes. Try again: "; cin >> night; } with reference parameters, changes made to the parameters in the function affect the argument in the function call... int dayMinutes, nightMinutes; GetMinutes(dayMinutes, nightMinutes);... view complete program

value vs. reference parameters by default, parameters are passed by-value  a copy of the argument is stored in the parameter (a local variable)  result: value passed in, no changes are passed out & implies by-reference  the parameter does not refer to a new piece of memory – it is an alias for the argument  result: changes to the parameter simultaneously change the argument void foo(int x) { x = 5; cout << x << endl; } int a = 3; foo(a); cout << a << endl; foo(3); note: argument can be any value void foo(int & x) { x = 5; cout << x << endl; } int a = 3; foo(a); cout << a << endl; note: argument must be a variable

diabolical example #include using namespace std; void Increment(int & x, int & y) { x++; y++; cout << "Inside: " << x << ", " << y << endl; } int main() { int a = 3, b = 7; Increment(a, b); cout << "Outside: " << a << ", " << b << endl; int q = 1; Increment(q, q); cout << "Outside: " << q << endl; return 0; }

top-down design summary design first, code later "The sooner you start coding, the longer it will take to finish." top-down design focuses on dividing complex problems into tasks  can further divide tasks into subtasks, …, until simple enough to code  natural to implement each task with a function  need to be able to pass information from one subtask (function) to another as needed  pass info into a function via (value) parameters  if need to pass one value out, do so via return value  if need to pass more than one value out, do so via reference parameters