Chapter 4

PROGRAM INPUT AND THE

SOFTWARE DESIGN PROCESS

Programming Warm-Up Exercises

2. cin.get(ch1); _ or cin >> ch1;

cin.get(ch2);

cin.get(ch3);

5. Given the declarations

char ch1, ch2, ch3, ch4, ch5, ch6, ch7;

int n1, n2, n3, n4, n5, n6;

float x1, x2, x3;

here is one possibility:

cin >> ch1 >> n1 >> x1 >> ch2 >> n2;

cin >> x2 >> ch3 >> ch4 >> x3 >> n3;

cin >> ch5 >> n4 >> ch6 >> n5 >> ch7 >> n6;

There are many other ways of organizing and textually formatting the input statements. Here is a solution using just one input statement:

cin >> ch1 >> n1 >> x1 >> ch2 >> n2

>> x2 >> ch3 >> ch4 >> x3 >> n3

>> ch5 >> n4 >> ch6 >> n5 >> ch7 >> n6;

6. If we use nine variables, we need only one input statement and one output statement:

inFile >> int1 >> int2 >> int3 >> int4 >> int5

>> int6 >> int7 >> int8 >> int9;

cout << int1 << ' ' << int2 << ' ' << int3 << endl

<< int4 << ' ' << int5 << ' ' << int6 << endl

<< int7 << ' ' << int8 << ' ' << int9 << endl;

 

Alternatively, writing each integer as it is read in requires only one variable but nine separate input statements and nine separate output statements.

7. Interactive segment for novice user:

cout << "Enter your age: ";

cin >> age;

cout << "Enter your height: ";

cin >> height;

cout << "Enter your weight: ";

cin >> weight;

cout << "Enter the initial of your first name: ";

cin >> first;

cout << "Enter the initial of your last name: ";

cin >> last;

Interactive segment for experienced user:

cout << "Enter age, height, and weight: ";

cin >> age >> height >> weight;

cout << "Enter initials: ";

cin >> first >> last;

  1. The algorithm for entering and running a program will vary greatly from system to

system. The example here is for Turbo C++ for DOS on a PC with a hard disk.

Main Module

Turn computer on

Start the Turbo system

Enter program

Run program

Start the Turbo system

Type "tc" and press Return

Enter program

Press F3

Enter the name of your program (with .cpp at the end) in the dialog box, and press Return

Type the program into the editor

Use the cursor, Backspace, and Delete keys to make corrections

Run program

Press F2 to save the file

Press Ctrl/F9 to run the program

If an error message appears, note what it says, and then press any key. Otherwise, enter data as needed and observe any output, until the program stops running

Press Alt/X to exit the Turbo system

11. Main Module

Open files

Read three coefficients from input file

Calculate two floating point solutions

Write solutions to output file

Open files

Open file inQuad for input

Open file outQuad for output

Read three coefficients from input file

Read a from inQuad

Read b from inQuad

Read c from inQuad

Calculate two floating point solutions

Set solution1 = (–b + sqrt(b*b – 4.0*a*c)) / (2.0 * a)

Set solution2 = (–b – sqrt(b*b – 4.0*a*c)) / (2.0 * a)

Write solutions to output file

Write solution1 to outQuad

Write ' ' to outQuad

Write solution2 to outQuad

ANSWERS TO QUESTIONS

True/False

    1. False 4. True 7. True 10. False 13. True
    2. True 5. True 8. True 11. False 14. True
    3. False 6. True 9. True 12. True 15. True

Multiple Choice

    1. e 19. e 22. b 25. a 28. c 31. e
    2. e 20. a 23. c 26. c 29. e 32. d
    3. a 21. c 24. c 27. b 30. d 33. c

Fill-In

    1. >>
    2. whitespace
    3. / n
    4. ifstream
    5. ofstream
    6. fail
    7. prompt (input prompt)
    8. echo printing
    9. top-down design (structured design, stepwise refinement, modular programming)
    10. pseudocode
    11. module
    12. functional equivalence
    13. functional cohesion
    14. flat (inline)
    15. module structure chart (solution tree)
    16. object-oriented design (OOD)
    17. class
    18. self-documenting