Download presentation
Presentation is loading. Please wait.
1
Writing and Testing Programs Drivers and Stubs Supplement to text
2
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)
3
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
4
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
5
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++
6
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
7
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).
8
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)
9
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
10
Writing the functions Write the algorithm for function max3.
11
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
12
Write prototype and definition for max3 prototype: int max3(int, int, int);
13
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; }
14
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.
15
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; }
16
Write algorithm for abs 1. if input parameter (x) is positive, return x 2. otherwise, return x * -1
17
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; }
18
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; }
19
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)
20
#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;
21
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
22
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; }
23
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
24
#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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.