Chapter 4 Flashcards

1
Q

Explain the While Loop

A

Format:
while (condition)
{
statement A; // runs while the condition is true until condition is no longer true (called an iteration)
}
statement B; // the statement that is run once the condition is false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain the Do-While Loop.

A

Format:
do
{
statements;
}
while (condition);

Unlike while loops, do-while loops check the condition at the end and decide whether or not to repeat the loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain the For Loop

A

Format:
for (initialization; condition; update;)
{
statements;
}

Executes the initialization, loops through the code as long as the condition is true, and performs the update after each loop

For loops are great for when you want your loop to repeat a set amount of times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a local variable?

A

A local variable is any variable declared inside of a loop. They only exist inside of that loop and cannot be accessed outside of the loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a global variable?

A

A global variable are variables that can be accessed anywhere in the entire program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When should you use a while loop?

A

You should use a while loop when you know your loop isn’t going to run a fixed number of times or if the stopping condition is more complicated than a simple comparison.

Note: loops are interchangeable to accomplish the same purpose, however some loops are easier to use.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When should you use a do-while loop?

A

You should use a do-while loop when your loop needs to run at least once.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When should you use a for loop?

A

You should use a for loop when you know ahead of time that your loop will run a fixed number of times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In a for loop, what is the difference between i = 0 vs i = 1?

A

i = 0 means it starts at 0, while i = 1 means it starts at 1. This will affect your output which is why sometimes people may run into an off by one error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a nested loop?

A

A nested loop is one loop inside of another.

Ex:
int i = 1, j;
while (i <= 3)
{
j = 1;
while (j <= i)
{
cout &laquo_space;i &laquo_space;”,” &laquo_space;j &laquo_space;“ - ”;
j++; }
i++;
cout &laquo_space;endl;
}

*An example of what a nested loop may be used for is creating rows and columns (i = the row, j = column)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the purpose of a break statement?

A

A break statement is used to break out of the loop

Note: a break statement cannot be used to break out of multiple nested loops. It only jumps out of the inner loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the purpose of the continue statement?

A

A continue statement is used to skip the remaining part of the loop and continues to the next iteration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the <random> library used for in C++ ?</random>

A

The <random> library in C++ allows us to use a random number generator</random>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is rand() and what is its function?

A
  • rand() is used to generate random integers
  • it generates a random integer between 0 and RAND_MAX
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What library do you need to include to use RAND_MAX?

A

include <cstdlib></cstdlib>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is RAND_MAX?

A

RAND_MAX generates the largest integer that rand() can generate.

EX: random integer between [0, RAND_MAX]
- int r = rand();

EX: random integer between [0, 10]
- int r = rand () % 11; // rmb position starts at 0 so using the number 11 gives us [0, 10].

EX: random integer in the range [a, b]
- int r = a + rand() % (b - a + 1);
// we use b - a + 1 because we are not sure if a starts at 0 as it could be any number.

17
Q

What are pseudorandom number generators?

A

Pseudorandom numbers are not completely random. They are initialized using an argument passed seed. We can call srand ()

18
Q

How do you generate different sequences of random numbers?

A

You use a different seed at each run time!

18
Q

What library is used to define time (nullptr) and what is its function?

A

<ctime> is used to define time (nullptr) and gives an unsigned integer based on system time.
</ctime>

19
Q

How do you generate a different seed at each run time?

A

srand(static_cast<int> (time (nullptr) ) );</int>

When different seeds are provided, different random sequences are generated

20
Q

What is a sentinel value?

A

A sentinel value indicates the end of a data set, but is not part of the data.

Ex:
cout &laquo_space;“Enter values, Q to quit: “; bool done = false;
while (!done)
{
cin&raquo_space; value;
if (cin.fail())
{ done = true; } else
{ //Process value… }
}

The program will only exit the loop if cin receives a fail input (in this example a failed input would be anything besides Q)

21
Q

Write a program that computes the sum.

A

double total = 0;
double input;
while ( cin&raquo_space; input )
{
total = total + input;
}

*Keep a running total: a variable to which you add each input
value.
* Initialize total with 0
* While-loop is terminated if input is not a valid number
* (cin&raquo_space; input) returns a bool value to its caller: true (successful reading) or false (failure reading)

22
Q

Write a program that computes the average.

A

double total = 0; int count = 0; double input;
while (cin&raquo_space; input) {
total = total + input;
count++;
}
double average = 0;
if ( count > 0 ) { average = total / count; }

*Keep a total and a count of all values
*Initialize total and count with 0
*Count how many numbers with variable count

23
Q

Write a program to find the minimum or maximum.

A

double largest;
cin&raquo_space; largest;
double input;
while (cin&raquo_space; input)
{
if (input > largest)
{
largest = input;
}
}
cout &laquo_space;”Largest number is ” &laquo_space;largest &laquo_space;endl;

*To find the largest (smallest) value, update the largest (smallest)
value seen so far whenever you see a larger (smaller) one.

24
Q

Write a program to count the number of inputs that match a target value.

A

double target = 100, input;
int count = 0;
while (cin&raquo_space; input)
{
if (input == target)
{ count++;}
}
cout &laquo_space;“The number of matches is ” &laquo_space;count;

*Increase count only if a match is found
*Count how many matches with variable count (Use count++)

25
Q

Write a program that compares adjacent values. Suppose you wantt o check whether a sequence contains adjacent duplicates such as 1 7 2 9 9 4 9 Q. What’s the output if input is 1 7 7 7 2 2 9 Q?

A

double input, previous;
cin&raquo_space; previous;
while ( cin&raquo_space; input )
{
if (input == previous)
{
cout &laquo_space;“Duplicate neighbors”
&laquo_space;endl;
}
previous = input;
}

*To compare adjacent inputs, store the preceding input in a variable
*If the input is 1 7 7 7 2 2 9 Q, the code will print Duplicate neighbors three times.