26
Writing and Testing Programs Drivers and Stubs Supplement to text

Writing and Testing Programs Drivers and Stubs Supplement to text

  • View
    214

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Writing and Testing Programs Drivers and Stubs Supplement to text

Writing and Testing Programs

Drivers and StubsSupplement to text

Page 2: 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)

Page 3: Writing and Testing Programs Drivers and Stubs Supplement to text

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

Page 4: Writing and Testing Programs Drivers and Stubs Supplement to text

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

Page 5: Writing and Testing Programs Drivers and Stubs Supplement to text

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++

Page 6: Writing and Testing Programs Drivers and Stubs Supplement to text

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

Page 7: Writing and Testing Programs Drivers and Stubs Supplement to text

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).

Page 8: Writing and Testing Programs Drivers and Stubs Supplement to text

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)

Page 9: Writing and Testing Programs Drivers and Stubs Supplement to text

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

Page 10: Writing and Testing Programs Drivers and Stubs Supplement to text

Writing the functions

Write the algorithm for function max3.

Page 11: Writing and Testing Programs Drivers and Stubs Supplement to text

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

Page 12: Writing and Testing Programs Drivers and Stubs Supplement to text

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

Page 13: Writing and Testing Programs Drivers and Stubs Supplement to text

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;

}

Page 14: Writing and Testing Programs Drivers and Stubs Supplement to text

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.

Page 15: Writing and Testing Programs Drivers and Stubs Supplement to text

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;}

Page 16: Writing and Testing Programs Drivers and Stubs Supplement to text

Write algorithm for abs

1. if input parameter (x) is positive, return x

2. otherwise, return x * -1

Page 17: Writing and Testing Programs Drivers and Stubs Supplement to text

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;}

Page 18: Writing and Testing Programs Drivers and Stubs Supplement to text

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;}

Page 19: Writing and Testing Programs Drivers and Stubs Supplement to text

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)

Page 20: Writing and Testing Programs Drivers and Stubs Supplement to text

#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;

Page 21: Writing and Testing Programs Drivers and Stubs Supplement to text

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

Page 22: Writing and Testing Programs Drivers and Stubs Supplement to text

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;}

Page 23: Writing and Testing Programs Drivers and Stubs Supplement to text

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

Page 24: Writing and Testing Programs Drivers and Stubs Supplement to text

#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

Page 25: Writing and Testing Programs Drivers and Stubs Supplement to text
Page 26: Writing and Testing Programs Drivers and Stubs Supplement to text