CS ch 5+7

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

If a loop does not have a way of stopping, it is called an _____ loop.

Infinite

The series of values inside the braces and separated with commas is called an

Initialization List

_____ _____ is the process of inspecting data given to a program by the user and determining if it is valid.

Input Validation

What does the size declarator do

It indicates the number of elements, or values, the array can hold

A loop that is inside another loop is called a

Nested loop

An _______ file is a file that data is written to. It is called an output file because the program stores output in it.

Output

What is the process called when trying to ignore parts of a file

Parsing

A ____ is a special value that marks the end of a list of values. When the user enters this, the loop terminates

Sentinel

C++ provides a set of simple unary operators designed just for incrementing and decrement- ing variables. The increment operator is ++, and the decrement operator is −−.

True

What is the output of the following code segment? for (int count = 0; count < 6; count++) cout << (count + count);

0246810

What is the output of the following code segment? int values[5], count; for (count = 0; count < 5; count++) values[count] = count + 1; for (count = 0; count < 5; count++) cout << values[count] << ' ';

1 2 3 4 5

What are the three elements of a count-controlled loop

1. It must initialize a counter variable to a starting value. 2. It must test the counter variable by comparing it to a maximum value. When the counter variable reaches its maximum value, the loop terminates. 3. It must update the counter variable during each iteration. This is usually done by incrementing the variable.

a = 2; b = 5; c = ++(a * b); cout << a << " " << b << " " << c;

Error bc operand of the increment and decrement operators must be an lvalue.

When a program needs to save data for later use, it writes the data in a ____. The data can then be read from the file at a later time.

File

A ___ ___ ____ is an object that is associated with a specific file and provides a way for the program to work with that file. It is called a "stream" object because a file can be thought of as a stream of data.

File stream object

in a 2-d array one ___ leave both brackets empty

cannot ! only the first one can be empty

The opposite of opening a file is _____ it

closing

A _____ loop executes as long as a particular condition exists. For example, an input validation loop executes as long as the input value is invalid. When you write a _____ loop, you have no way of knowing the number of times it will iterate.

conditional

In general, there are two categories of loops: _____ loops and ____-_____ loops.

conditional count-controlled

A _____ file contains data that has not been converted to text. Thus, you cannot view the contents of a binary file with a text editor

binary

The statement that is repeated is known as the ____ of the loop

body, It is also considered a conditionally executed statement, because it is executed only under the condition that the expression is true.

What statement causes a loop to terminate immediately?

break;

What does an infinite loop do

continues to repeat until the program is interrupted. Here is an example of an infinite loop: int number = 0; while (number < 5) { cout << "Hello\n"; } This is an infinite loop because it does not contain a statement that changes the value of the number variable. Each time the expression number < 5 is tested, number will contain the value 0.

Sometimes you know the exact number of iterations that a loop must perform. A loop that repeats a specific number of times is known as a _____ -____ loop

count-controlled

A ____ is a variable that is regularly incremented or decremented each time a loop iterates

counter

We should never change the loop ____ in the loop

counter

How to print an array

cout << (name of array)[i][j] << " "; cout << endl;

Notice that C++ allows you to spread the initialization list across multiple lines. Both of the following array definitions are equivalent

double coins[5] = {0.05, 0.1, 0.25, 0.5, 1.0}; double coins[5] = {0.05, 0.1, 0.25, 0.5, 1.0};

what is EOF

end of file

It's important to remember that when you call an ofstream object's open member function, the specified file will be cre- ated. If the specified file already exists, it will be _____, and a new file with the same name will be created.

erased

The while loop in the following program segment appears to execute 10 times, but the break statement causes it to stop after the fifth iteration. int count = 0; while (count++ < 10) { cout << count << endl; if (count == 5) break; }

example

Each operating system has its own rules for naming files. Many systems, including Windows, support the use of filename ________, which are short sequences of characters that appear at the end of a filename preceded by a period (known as a "dot")

extensions

When writing functions that accept multi-dimensional arrays as arguments, all but the first dimension must be explicitly stated in the parameter list.

facts

An arrays initilization list can have more values than the array has elements

false

Loops must contain within themselves a way to terminate. This means that something inside the loop must eventually make the test expression _____.

false

data validation is required when reading from a file

false

Files on a disk are identified by a

filename

The ___ loop is specifically designed to initialize, test, and update a counter variable. Here is the format of the for loop when it is used to repeat a single statement:

for

The ____ loop is ideal for performing a known number of iterations.

for

Format of for loop

for (initialization; test; update) statement;

File stream. Objects of this data type can be used to open files for reading, writing, or both

fstream

What header file is required when working with files

fstream

how to format getline when reading from a file?

getline (input ,line)

The following statement stores the integer 30 in hours[3].

hours[3] = 30;

Input file stream. You create an object of this data type when you want to open an existing file and read data from it

ifstream

What type of file stream object is used to read data from a file?

ifstream

When opening a file, which of the following is syntactically correct: ifstream input.open("stats.txt"); ifstream input("stats.txt"); input.open("stats.txt");

ifstream input("stats.txt"); input.open("stats.txt"); both r valid

It is possible to define a file stream object and open a file in one statement.

ifstream inputFile("Customers.txt");

The first expression inside the parenthesis is the

initialization expression It is normally used to initialize a counter variable to its starting value. This is the first action performed by the loop, and it is only done once.

An ______ file is a file that data is read from. It is called an input file because the program gets input from the file.

input

if from a file and trying to ignore, do

input.ignore( ); input >> x1

Assuming input File is an if stream object, the following statement shows the >> operator reading data from the file into the variable name:

inputFile >> name;

Example of closing a file

inputFile.close();

Definition of an array of integers

int days[6]; name of array: days Size declarator: 6

An array's size declarator must be a constant _____ expression with a value greater than zero.

integer

In a nested loop, the break statement only ____ the loop it is placed in.

interrupts

Each repetition of a loop is known as an

iteration

It's important to note that if an array is partially initialized, the uninitialized elements will be set to zero. The uninitialized elements of a string array will contain empty strings. This is true even if the array is defined locally

know it

The subscript of the last element in the array is one ____ that the total elements

less

the number variable is referred to as the ____ ______ variable because it controls the number of times that the loop iterates.

loop control

The first line of the for loop is the

loop header

The first line shown in the format is called the

loop header, consist of keywords

The first arguement, when passing in an array is the

name of the array

the dot "." is an

object

Output file stream. You create an object of this data type when you want to create a file and write data to it.

ofstream

What type of file stream object is used to write data to a file?

ofstream

Given the following code segment, identify the correct syntax to close the file. ofstream outFile("printout.txt");

outFile.close();

Assuming outputFile is an ofstream object, the following statement demonstrates using the << operator to write a string literal to a file:

outputFile << "I love C++ programming\n";

What is a loop

part of a program that repeats.

The do-while loop is a _______ loop, which means its expression is tested after each iteration.

post-loop

The while loop is known as a _____ loop, which means it tests its expression before each iteration

pretest

Bc the for loop tests its test expression before it performs an iteration, it is a _____ loop

pretest It is possible to write a for loop in such a way that it will never iterate. Here is an example: for (count = 11; count <= 10; count++) cout << "Hello" << endl; Because the variable count is initialized to a value that makes the test expression false from the beginning, this loop terminates as soon as it begins.

The process of retrieving data from a file is known as _______ data from the file. When a piece of data is read from a file, it is copied from the file into a variable in RAM

reading

The while loop can be used to create input routines that _____ until acceptable data is entered.

repeat

The do-while loop is a good choice for ____ a menu.

repeating

A ___ is a sum of numbers that accumulates with each iteration of a loop.

running total

By using the ____ subscript, you can build relationships between data stored in two or more arrays

same

C++ does not limit the amount of dimensions that an array has. IT is possible to create arrays with multiple dimensions, to model data that occurs in multiple sets

true

C++ does not prevent you from overwriting an array's bound This means you can write programs with subscripts that go beyond the boundaries of a particular array. Program 7-5 demonstrates this capability.

true

It can be either a literal, as in the previous example, or a named constant, as shown in the following: const int NUM_DAYS = 6; int days[NUM_DAYS];

true

Arrays may be initialized when they are defined

true

How do you pronounce the following expression: num--

"num minus minus"

How do you pronounce the following expression: num++

"num plus plus"

Programs that calculate the total of a series of numbers typically use what two elements

(1) A loop that reads each number in the series. (2) A variable that accumulates the total of the numbers as they are read.

What are the two types of files?

-A text file -Binary FIle

Name 2 reasons why closing a file is important

-Most operating systems temporarily store data in a file buffer before it is written to a file. A file buffer is a small "holding section" of memory that file-bound data is first written to. When the buffer is filled, all the data stored there is written to the file. This technique improves the system's performance. 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.

These subscripts are used as an index to pinpoint a specific element within an array, and always begins at index

0

When a file is used by a program, what three steps must be take

1. Open the file—Opening a file creates a connection between the file and the program. Opening an output file usually creates the file on the disk and allows the program to write data to it. Opening an input file allows the program to read data from the file. 2. Process the file—Data is either written to the file (if it is an output file) or read from the file (if it is an input file). 3. Close the file—After the program is finished using the file, the file must be closed. Closing a file disconnects the file from the program.

Given the following array definition: int values[] = {2, 6, 10, 14}; what will the code segment below display? x = 2; cout << values[++x];

14

What would be the valid subscript values in a four-element array of doubles? Select all that apply. Arrays begin at index 0

2 0 3 1

a = 2; b = 5; c = a * b++; cout << a << " " << b << " " << c; In the statement c = a * b++, c is assigned the value of a times b, which is 10. The variable b is then incremented. The cout statement will display

2 6 10

A ___ array is like several identical arrays put together. It is useful for storing multiple sets of data.

2-dimensional

How many elements are in the following array? double sales [6][4];

24

double score[8][4]

8 = the number of rows 4 = the number of columns

Before data can be written to or read from a file, the following 2 steps must happen

Before data can be written to or read from a file, the following things must happen: • A file stream object must be created • The file must be opened and linked to the file stream object. The following code shows an example of opening a file for input (reading). ifstream inputFile; inputFile.open("Customers.txt");

What statement causes a loop to terminate early?

Break

The individual elements of an array are assigned unique _____. These _____ are used to access the elements.

Subscripts

What are the two important parts of the while loop

The while loop has two important parts: (1) an expression that is tested for a true or false value, and (2) a statement or block that is repeated as long as the expression is true

For example, the following loop asks for a number in the range of 1 through 100: cout << "Enter a number in the range 1-100: "; cin >> number; while (number < 1 || number > 100) { cout << "ERROR: Enter a value in the range 1-100: "; cin >> number; }

This code first allows the user to enter a number. This takes place just before the loop. If the input is valid, the loop will not execute. If the input is invalid, however, the loop will display an error message and require the user to enter another number. The loop will continue to execute until the user enters a valid number.

Difference between prefix and postfix? num = 4; cout << num++; vs num = 4; cout << ++num;

This cout statement is doing two things: (1) displaying the value of num, and (2) incrementing num. But which happens first? cout will display a different value if num is incremented first than if num is incremented last. The answer depends on the mode of the increment operator. Postfix mode causes the increment to happen after the value of the variable is used in the expression. In the example, cout will display 4, then num will be incremented to 5. Prefix mode, however, causes the increment to happen first.In the following statements, num will be incremented to 5, then cout will display 5:

What does increment mean?

To increment a value means to increase it by one

if (x++ > 10) cout << "x is greater than 10.\n"; Two operations are happening in this if statement: (1) The value in x is tested to determine if it is greater than 10, and (2) x is incremented. Because the increment operator is used in postfix mode, the comparison happens first. Since 10 is not greater than 10, the cout 5.1 The Increment and Decrement Operators 231 232 Chapter 5 Loops and Files statement won't execute. If the mode of the increment operator is changed, however, the if statement will compare 11 to 10, and the cout statement will execute: x = 10; if (++x > 10) cout << "x is greater than 10.\n";

True

Ex: The value 1 was chosen for the sentinel in this program because it is not possible for a team to score negative points. Notice that this program performs a priming read in line 18 to get the first value. This makes it possible for the loop to immediately terminate if the user enters 1 as the first value. Also note that the sentinel value is not included in the running total.

True shit

The variable used to keep the running total is called an _____

accumulator

An ____ allows you to store and work with multiple values of the same data type.

array

Although a program's files are _______ closed when the program shuts down, it is a good programming practice to write statements that close them.

automatically

Format of a do-while

do statement; while (expression);

but a __ ____ will always run

do while

It's also possible to create an infinite loop by accidentally placing a _____ after the first line of the while loop.

semicolon The semicolon at the end of the first line is assumed to be a null statement and disconnects the while statement from the block that comes after it. To the compiler, this loop looks like: while (number < 5); This while loop will forever execute the null statement, which does nothing. The program will appear to have "gone into space" because there is nothing to display screen output or show activity.

The ____ of an array can be calculated by multiplying the size of an individual element by the number of elements in the array

size

the second arguement is the

size of the array

Step 1: Perform the initialization expression. Step 2: Evaluate the test expression. If it is true, go to Step 3.Otherwise, terminate the loop. for (count = 0; count < 5; count++) cout << "Hello" << endl; Step 3: Execute the body of the loop. Step 4: Perform the update expression, then go back to Step 2.

step

The getline function reads the entire line into a ____ variable

string

The second expression is called the

test expression This is an expression that controls the execution of the loop. As long as this expression is true, the body of the for loop will repeat. The for loop is a pretest loop, so it evaluates the test expression before each iteration.

A _____ file contains data that has been encoded as text, using a scheme such as 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

text

How does the while loop work

the expression is tested, and if it is true, the statement is executed. Then, the expression is tested again. If it is true, the statement is executed. This cycle repeats until the expression is false.

What is postfix mode?

the operator is placed after the variable

a = 2; b = 5; c = a * ++b; cout << a << " " << b << " " << c;

the variable b would be incremented before it was multiplied by a. In this case c would be assigned the value of 2 times 6, so the cout statement would display 2 6 12

What does decrement mean?

to decrement a value means to decrease it by one

Understand the difference between the array size declarator and a subscript. The number inside the brackets of an array definition is the size declarator. The number inside the brackets of an assignment statement or any statement that works with the contents of an array is a subscript.

tru

All three areas in the for loop ( ; ; ) can be left empty, they are optional

true

An important characteristic of the while loop is that the loop will never iterate if the test expression is false to start with. If you want to be sure that a while loop executes the first time, you must initialize the relevant data in such a way that the test expression starts out as true.

true

It is possible to execute more than one statement in the initialization expression and the update expression. When using multiple statements in either of these expressions, simply separate the statements with commas Connecting multiple statements with commas works well in the initialization and update expressions, but do not try to connect multiple expressions this way in the test expression. If you wish to combine multiple expressions in the test expression, you must use the && or || operators.

true

Notice that in the function prototype, empty brackets appear after the data type of the array parameter. This indicates that showValues accepts the address of an array of integers.

true

Sometimes it's useful to store related data in two or more arrays. It's especially useful when the related data is of unlike types

true

The amount of memory used by an array depends on the array's data type and the number of elements

true

The do-while loop must be terminated with a semicolon.

true

This means it does not test its expression until it has completed an iteration. As a result, the do-while loop always performs at least one itera- tion, even if the expression is false to begin with. This differs from the behavior of a while loop, which you will recall is a pretest loop.

true

To pass an array as an argument to a function, pass the name of the array.

true

When an array is being initialized, C++ does not require a value for every element, t's possible to only initialize part of an array, such as: int numbers[7] = {1, 2, 4, 8};

true

When an entire array is passed to a function, it is not passed by value, but passed by reference.

true

You should use the do-while loop when you want to make sure the loop executes at least once.

true

initializing a number is very important

true int number = 0;

The third expression is called the

update expression It executes at the end of each iteration. Typically, this is a statement that increments the loop's counter variable.

Which of the following prototypes represents a function that will allow an array of any size to be passed into the function?

void fillArray(char []);

The operators may also work in prefix mode, which is where...

where the operator is placed before the variable name EX: ++num; −−num;

A ____ loop is not guaranteed to run

while

The ____ loop is especially useful for validating input. If an invalid value is entered, a loop can require that the user reenter it as many times as necessary

while

Mostly nested loops are either going to be

while do while and for loops

What is the general format of a while loop

while (expression) statement;

If you wish the while loop to repeat a block of statements, its format is

while (expression) { statement; statement; // Place as many statements here // as necessary. }

When a two-d array is passed to a funcion, the parameter type must contain a size declerator for the number of columns

why the second bracket cannot be empty

Programmers usually refer to the process of saving data in a file as ____ _____ to the file When a piece of data is written to a file, it is copied from a variable in RAM to the file.

writing data

To pass an array as an argument to a function, pass the name of the array

yea


Set pelajaran terkait

RHIT Practice Exam 1 - 150 Questions

View Set

Common Logical Fallacies: Examples

View Set

Infection Review PREP U ( NUR2 TEST 2)

View Set

Microeconomics Chapter 8 Study Guide

View Set

Questions for The Help (a.k.a. Niceville)

View Set

Postoperative Exercises: Diaphragmatic Breathing and Controlled Coughing

View Set

Chapter 4: Legal and Ethical Aspects of Nursing

View Set

Advanced Psychological Statistics Final

View Set