View
214
Download
1
Tags:
Embed Size (px)
Citation preview
Writing and Testing Programs
Drivers and StubsSupplement 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 numbers2. Get the largest of those. Call it a.3. Get the absolute value of a. call it absa4. Read in 3 more numbers. 5. Get the largest of those. Call it b.6. Get the absolute value of b. Call it
absb7. 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 max3prototype: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 int2if (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 <iostream>#include "max3.cc"
using namespace std;int max3(int, int, int);
int main(){ int int1, int2, int3; cout << “Enter 3 integers to test max3> "; 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 absprototype: 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 <iostream>#include "abs.cc"using namespace std;int abs(int);int main(){ int x; cout << "Enter an integer to test abs> "; cin >> x; cout << "abs called with " << x << " returns " << abs(x) << endl;}
Write main function (algorithm below)
1. Read in 3 numberscin >> 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 <iostream>// do NOT include max and absusing namespace std;int abs(int);int max(int, int, int);int main(){int int1, int2, int3, a, b, absa, absb; cout << "Enter three integers> "; // 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 onlycout << "result of abs is " << absa << endl;
cout << "Enter three more integers> "; // 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 onlycout << "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 <iostream>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 << "Enter three integers> "; // 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 << "Enter three more integers> "; // 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