Writing and Testing Programs Drivers and Stubs Supplement to text.

Slides:



Advertisements
Similar presentations
Programming Functions: Passing Parameters by Reference.
Advertisements

Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {
1 Lab Session-IV CSIT121 Fall 2000 Some Questions Scope of Variables Top Down Design Problem The Solution Lab Exercises Lab Exercise for Demo.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Chapter 4 Summation.
Chapter 5 Functions. Function Subprograms (Functions) A function is a set of instructions that can be used again and again, each time on a possibly different.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
Computer Science 1620 Programming & Problem Solving.
1 Lecture 14:User-Definded function I Introduction to Computer Science Spring 2006.
Function Part II: Some ‘advanced’ concepts on functions.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Software Engineering 1 (Chap. 1) Object-Centered Design.
Programming Functions: Passing Parameters by Reference.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Programming Principles Chapter 1. Objectives Discuss the program design process. Introduce the Game of Life. Discuss object oriented design. – Information.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
CPS120: Introduction to Computer Science Functions.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
#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.
C++ (intro) Created by Hwansoo Han Edited by Ikjun Yeom.
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.
CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CHAPTER 6 USER-DEFINED FUNCTIONS Made By- Kartik Belwal.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Introduction to Functions.  A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Functions Jordi Cortadella Department of Computer Science.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
 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.
Problem Solving and Program Design. Problem Solving Process Define and analyze the problem. Develop a solution. Write down the solution steps in detail.
Chapter 6: User-Defined Functions I
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Chapter 5 Function Basics
More ‘concepts’ on Function
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
Lab 1 Introduction to C++.
Pass by Reference.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 6: User-Defined Functions I
Functions Imran Rashid CTO at ManiWeber Technologies.
Single-Result Functions & Modularity
Introduction to Functions
More ‘concepts’ on Function
Presentation transcript:

Writing and Testing Programs Drivers and Stubs Supplement to text

Write and Test Parts Do not write entire programs at a time Write the whole algorithm in English, pseudo- code Choose which part to implement Write that part Test that part Write another part (in another file). Test that part When done with all parts, integrate all parts (in one or many files)

How do I write one part? Choose a function to write Type it into a file (algorithm is already written) Write a main section to test just that function Compile and test

What about main? Write main Instead of writing each function, have “stubs” – functions with the correct prototype, header and return type (can be a constant), but don’t do any calculation Compile and Test main

Example Write a program that prints the product of the absolute value of 2 integers. Each of the 2 integers will be calculated by reading in 3 integers and choosing the largest (so a total of 6 integers will be entered). Write the algorithm – NOT C++

Algorithm 1. Read in 3 numbers 2. Get the largest of those. Call it a. 3. Get the absolute value of a. call it absa 4. Read in 3 more numbers. 5. Get the largest of those. Call it b. 6. Get the absolute value of b. Call it absb 7. Output the product of absa and absb

Algorithm (more detailed) 1. Read in 3 numbers Could have a separate function, but this is short, and we don’t know how to write a function to do this yet. cin >> int1 >> int2 >> int3 (with labels) 2. Get the largest of those. Call it a. We’ll call this function max3. Takes 3 integers as parameters. Returns the largest. 3. Get the absolute value of a. call it absa We’ll call this function abs (already written in some math library, but we’ll write out own).

Algorithm (continued) 4. Read in 3 more numbers. cin >> int1 >> int2 >> int3 (ok to call the same names as earlier) 5. Get the largest of those. Call it b. Call max3: b = max(int1, int2, int3) 6. Get the absolute value of b. Call it absb Call abs: absb = abs(b) 7. Output the product of absa and absb cout << absa * absb (with labels)

Next First, write and test max3.cc Second, write and test abs.cc Third, write and test main Fourth, after all is tested, modify main to use max3, abs, and test

Writing the functions Write the algorithm for function max3.

Algorithm for max3 Now, write the function prototype and function definition. 1. Compare int1 and int2. Store larger in largest 2. Compare largest and int3. Store larger in largest 3. Return largest

Write prototype and definition for max3 prototype: int max3(int, int, int);

max3 (in file max3.cc) int max3(int int1, int int2, int int3) { int largest; if (int1 < int2) largest = int2; else largest = int1; // if int1 > or = to int2 if (int3 > largest) largest = int3; return largest; }

Test max3 Write a driver main function whose sole purpose it to test a function calls the function with reasonable values (input w/cin or constants – advantage of cin is you can run multiple tests) prints the return value.

max3 driver #include #include "max3.cc" using namespace std; int max3(int, int, int); int main() { int int1, int2, int3; cout "; cin >> int1 >> int2 >> int3; cout << "The result of max3 called with " << int1 << ", " << int2 ", " << ", and " << int3 << " is " << max3(int1, int2, int3) << endl; }

Write algorithm for abs 1. if input parameter (x) is positive, return x 2. otherwise, return x * -1

Write prototype and definition for abs prototype: int abs(int); definition in file abs.cc: int abs(int x) { if (x >= 0) return x; else return x*-1; }

Write driver to test abs #include #include "abs.cc" using namespace std; int abs(int); int main() { int x; cout "; cin >> x; cout << "abs called with " << x << " returns " << abs(x) << endl; }

Write main function (algorithm below) 1. Read in 3 numbers cin >> int1 >> int2 >> int3 (with labels) 2. Get the largest of those. Call it a. Call max3. 3. Get the absolute value of a. call it absa Call abs 4. Read in 3 more numbers. cin >> int1 >> int2 >> int3 5. Get the largest of those. Call it b. Call max3: b = max(int1, int2, int3) 6. Get the absolute value of b. Call it absb Call abs: absb = abs(b) 7. Output the product of absa and absb cout << absa * absb (with labels)

#include // do NOT include max and abs using namespace std; int abs(int); int max(int, int, int); int main() {int int1, int2, int3, a, b, absa, absb; cout "; // 1 cin >> int1 >> int2 >> int3; a = max3(int1, int2, int3); // 2 // next cout debugging only cout << "Result of max3 call is: " << a << endl; absa = abs(a);// 3 // next cout for debugging only cout << "result of abs is " << absa << endl;

cout "; // 4 cin >> int1 >> int2 >> int3; b = max3(int1, int2, int3);// 5 // next cout debugging only cout << "Result of max3 call is: " << b << endl; absb = abs(b);// 6 // next cout for debugging only cout << "result of abs is " << absb << endl; cout << "The answer is " << absa * absb << endl; // 7 } // end main

int max3(int a, int b, int c) { cout << "in abs with parameters " << a << ", " << b << ", " << c < end; return 10; } int abs(int x) { cout << "in x with parameter " << x << endl; return x; }

Next First, write and test max3.cc Second, write and test abs.cc Third, write and test main Fourth, after all is tested, modify main to use max3, abs, and test Write the entire program modifying main and including max3 and abs

#include include "abs.cc" include "max3.cc" using namespace std; int abs(int); int max(int, int, int); int main() {int int1, int2, int3, a, b, absa, absb; cout "; // 1 cin >> int1 >> int2 >> int3; a = max3(int1, int2, int3); // 2 // cout << "Result of max3 call is: " << a << endl; absa = abs(a);// 3 // cout << "result of abs is " << absa << endl; cout "; // 4 cin >> int1 >> int2 >> int3; b = max3(int1, int2, int3);// 5 // cout << "Result of max3 call is: " << b << endl; absb = abs(b);// 6 //cout << "result of abs is " << absb << endl; cout << "The answer is " << absa * absb << endl; // 7 } // end main