CMPSC 121 - Book Questions - Chapter 5

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

14. Why should a program close a file when it's finished using it?

Here are two reasons a program should close files when it is finished using them: · Closing a file causes any unsaved data that may still be held in a buffer to be saved to its file. This means the data will be in the file if you need to read it later in the same program. · Some operating systems limit the number of files that may be open at one time. When a program closes files that are no longer being used, it will not deplete more of the operating system's resources than necessary.

13. What data type do you use when you want to create a file stream object that can read data from a file?

ifstream

16. To ____ a value means to increase it by one, and co ____ a value means to decrease it by one.

increment, decrement

23. A loop that does not have a way of stopping is a(n) ____ loop.

infinite or endless

32. A loop that is inside another is called a(n) ____ loop.

nested

12. What data type do you use when you want to create a file stream object that can write data to a file?

ofstream

18 When the increment or decrement operator is placed after the operand (or to the operand's right), the operator is being used in ____ mode.

postfix

22. A loop that evaluates its test expression after each repetition is a(n) ____ loop.

posttest

29. The and ____ loops will not iterate at all if their test expressions are false to start with.

while and for

5.1 What will the following program segments display? A) x = 2; y = x++; cout << x << y;

5.1 A) 32

5.10 Write a for loop that displays all of the odd numbers, 1 through 49.

5.10 for (int count = 1; count <= 49; count += 2) cout << count << endl;

5.11 Write a for loop that displays every fifth number, 0 through 100.

5.11 for (int count = 0; count <= 100; count += 5) cout << count << endl;

5.12 Write a for loop that repeats seven times, asking the user to enter a number. The loop should also calculate the sum of the numbers entered.

5.12 int number, total = 0; for (int count = 0; count < 7; count++) { cout << "Enter a number: "; cin >> number; total += number; }

5.13 In the following program segment, which variable is the counter variable and which is the accumulator? int a , x, y = 0 ; for (x = O; x < 10; x++) { cout << "Enter a number: "; cin >> a; Y += a; cout << "The sum of those numbers is "<< y << endl ;

5.13 The variable x is the counter variable. The variable y is the accumulator.

5.14 Why should you be careful when choosing a sentinel value?

5.14 You must be sure to choose a sentinel value that could not be mistaken as a member of the data list.

5.15 How would you modify Program 5-13 so any negative value is a sentinel?

5.15 Change the while loop to read: while (points >= 0)

5.16 What is an output file? What is an input file?

5.16 An output file is a file that data is written to. An input file is a file that data is read from.

5.17 What three steps must be taken when a file is used by a program?

5.17 1. Open the file 2. Process the file 3. Close the file

5.18 What is the difference between a text file and a binary file?

5.18 A text file contains data that has been encoded as text, using a scheme such ASCII or Unicode. Even if the file contains numbers, those numbers are stored in the file as a series of characters. As a result, the file may be opened and viewed in a text editor such as Notepad. A binary file contains data that has not been converted to text. As a consequence, you cannot view the contents of a binary file with a text editor.

5.19 What is the difference between sequential access and random access?

5.19 When you work with a sequential access file, you access data from the beginning of the file to the end of the file. When you work with a random access file, you can jump directly to any piece of data in the file without reading the data that comes before it.

5.2 Write an input validation loop that asks the user to enter a number in the range of 10 through 25.

5.2 int number; cout << "Enter a number in the range 10 through 25: "; cin >> number; while (number < 10 || number > 25) { cout << "Error! The number must be in the range " << "of 10 through 25. Enter a valid number: "; cin >> number; }

5.20 What type of file stream object do you create if you want to write data to a file?

5.20 ofstream

5.21 What type of file stream object do you create if you want to read data from a file?

5.21 ifstream

5.22 Write a short program that uses a for loop to write the numbers 1 through 10 to a file.

5.22 ofstream outputFile("num.txt"); for (int num = 1; num <= 10; num++) outputFile << num << endl; outputFile.close();

5.23 Write a short program that opens the file created by the program you wrote for Checkpoint 5.22, reads all of the numbers from the file, and displays them.

5.23 ifstream inputFile("num.txt"); int num; while (inputFile >> num) cout << num << endl; inputFile.close();

5.3 Write an input validation loop that asks the user to enter 'Y', ' y', 'N', or 'n'.

5.3 char letter; cout << "Enter Y for yes or N for no: "; cin >> letter; while (letter != 'Y' && letter != 'y' && letter != 'N' && letter != 'n') { cout << "Error! Enter either Y or N: "; cin >> letter; }

5.4 Write an input validation loop that asks the user to enter "Yes" or "No".

5.4 string input; cout << "Enter Yes or No: "; cin >> input; while ( (input != "Yes") && (input != "No") ) { cout << "Error! Enter either Yes or No: "; cin >> input; }

5.5 What will the follow ing program segments display? A) int count = 10; do { cout << "Hello World\n"; count++; } while (count < 1); B) int v = 10; do cout << v << end1; while ( v < 5 ) ; C) int count = 0, number = 0, 1 i mit = 4; do { number += 2; count++; } while (count< limit); cout <<number<<" "<<count<< endl;

5.5 A) Hello World B) 10 C) 8 4

5.6 Name the three expressions that appear inside the parentheses in the for loop's header.

5.6 Initialization, test, and update.

5.7 You want to write a for loop that displays "I love ro program" 50 times. Assume you will use a counter variable named count. A) What initialization expression will you use? B) What rest expression will you use? C) What update expression will you use? D ) Write the loop.

5.7 A) int count = 1; B) count <= 50; C) count++ D) for (int count = 1; count <= 50; count++) cout << "I love to program\n";

5.8 What will the following program segments display? A) for (int count= O; count< 6; count++) cout << (count+ count) ; B) for (int value= -5; value< 5; value++) cout << value; C) int x; for ( X = 5; X <= 14; X += 3) cout << x << endl; cout << x << endl;

5.8 A) 0246810 B) -5-4-3-2-101234 C) 5 8 11 14 17

5.9 Write a for loop that displays your name 10 times.

5.9 for (int count = 1; count <= 10; count++) cout << "Chris Coder\n";

8. Why is it critical that counter variables be properly initialized?

A counter variable is used to control or keep track of the number of times a loop iterates. In a loop, the counter is usually incremented or decremented. If the counter variable is not properly initialized, it will not hold the correct number.

15. What is a file's read position? Where is the read position when a file is first opened for reading?

A file's read position marks the location of the next byte that will be read from the file. When an input file is opened, its read position is initially set to the first byte in the file.

2. Describe the difference between pretest loops and posttest loops.

A pretest loop tests its condition before each iteration. A posttest loop tests its condition after each iteration. A posttest loop will always execute at least once.

9. Why is it critical that accumulator variables be properly initialized?

An accumulator is used to keep a running total of numbers. In a loop, a value is usually added to the current value of the accumulator. If it is not properly initialized, it will not contain the correct total.

5.1 What will the following program segments display? B) x = 2; y = ++x; cout << x << y;

B) 33

10. Why should you be careful not to place a statement in the body of a for loop that changes the value of the loop's counter variable?

Because the for loop has an update expression that normally changes the contents of a counter. If you have code inside that loop that also changes the counter, the loop will not be able to properly control the value of the counter.

3. Why are the statements in the body of a loop called conditionally executed statements?

Because they are only executed when a condition is true.

1. Why should you indent the statements in the body of a loop?

By indenting the statements, you make them stand out from the surrounding code. This helps you to identify at a glance the statements that are conditionally executed by a loop.

5.1 What will the following program segments display? C) x = 2 ; y = 4; cout << x++ << --y;

C) 23

5.1 What will the following program segments display? D) x = 2; y = 2 • x++; cout << x << y ;

D) 34

5.1 What will the following program segments display? E) x = 99; if (x++ < 100) cout "It is true!\n"; else cout << "It is false!\n";

E) It is true!

5.1 What will the following program segments display? F) x = 0; if (++x) cout << "It is true!\n"; else cout << "It is false!\n";

F) It is true!

6. Which loop should you use in situations where you wish the loop to repeat until the test expression is false, but the loop should execute at least one time?

The do-while loop.

7. Which loop should you use when you know the number of required iterations?

The for loop.

4. What is the difference between the while loop and the do-while loop?

The while loop is a pretest loop and the do-while loop is a posttest loop.

5. Which loop should you use in situations where you wish the loop to repeat until the test expression is false, and the loop should not execute if the test expression is false to begin with?

The while loop.

26. A(n) ____ is a variable that is initialized to some starting value, usually zero, then has numbers added to it in each iteration of a loop.

accumulator

19. The statement or block that is repeated is known as the ____ of the loop.

body

33. The _____ statement causes a loop to terminate immediately.

break

34. The ____ statement causes a loop to skip the remaining statements in the current iteration.

continue

24. A(n) ____ is a variable that "counts" the number of times a loop repeats.

counter

28. The loop always iterates at least once.

do-while

30. The ____ loop is ideal for situations that require a counter.

for

11. What header file do you need to include in a program that performs file operations?

fstream

31. Inside the for loop's parentheses, the first expression is the ____ , the second expression is the ____ , and the third expression is the ___ _

initialization, test, update

20. Each repetition of a loop is known as a(n) _ _ _ _

iteration

17. When the increment OF decrement operator is placed before the operand (or to the operand's left), the operator is being used in _ ___ mode.

prefix

21. A loop that evaluates its test expression before each repetition is a(n) ____ loop.

pretest

25. A(n) ____ is a sum of numbers that accumulates with each iteration of a loop.

running total

27. A(n) _ _ __ is a special value that marks the end of a series of values.

sentinel


Kaugnay na mga set ng pag-aaral

Vocabulary - List 6 Deductive and Inductive Arguments

View Set

(2) Function and purpose of translators

View Set

Anatomy and Physiology 2 Cumulative Final 1-50

View Set