Chapter 4 Flashcards
(26 cards)
Explain the While Loop
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
Explain the Do-While Loop.
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.
Explain the For Loop
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.
What is a local variable?
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.
What is a global variable?
A global variable are variables that can be accessed anywhere in the entire program.
When should you use a while loop?
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.
When should you use a do-while loop?
You should use a do-while loop when your loop needs to run at least once.
When should you use a for loop?
You should use a for loop when you know ahead of time that your loop will run a fixed number of times
In a for loop, what is the difference between i = 0 vs i = 1?
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.
What is a nested loop?
A nested loop is one loop inside of another.
Ex:
int i = 1, j;
while (i <= 3)
{
j = 1;
while (j <= i)
{
cout «_space;i «_space;”,” «_space;j «_space;“ - ”;
j++; }
i++;
cout «_space;endl;
}
*An example of what a nested loop may be used for is creating rows and columns (i = the row, j = column)
What is the purpose of a break statement?
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.
What is the purpose of the continue statement?
A continue statement is used to skip the remaining part of the loop and continues to the next iteration.
What is the <random> library used for in C++ ?</random>
The <random> library in C++ allows us to use a random number generator</random>
What is rand() and what is its function?
- rand() is used to generate random integers
- it generates a random integer between 0 and RAND_MAX
What library do you need to include to use RAND_MAX?
include <cstdlib></cstdlib>
What is RAND_MAX?
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.
What are pseudorandom number generators?
Pseudorandom numbers are not completely random. They are initialized using an argument passed seed. We can call srand ()
How do you generate different sequences of random numbers?
You use a different seed at each run time!
What library is used to define time (nullptr) and what is its function?
<ctime> is used to define time (nullptr) and gives an unsigned integer based on system time.
</ctime>
How do you generate a different seed at each run time?
srand(static_cast<int> (time (nullptr) ) );</int>
When different seeds are provided, different random sequences are generated
What is a sentinel value?
A sentinel value indicates the end of a data set, but is not part of the data.
Ex:
cout «_space;“Enter values, Q to quit: “; bool done = false;
while (!done)
{
cin»_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)
Write a program that computes the sum.
double total = 0;
double input;
while ( cin»_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»_space; input) returns a bool value to its caller: true (successful reading) or false (failure reading)
Write a program that computes the average.
double total = 0; int count = 0; double input;
while (cin»_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
Write a program to find the minimum or maximum.
double largest;
cin»_space; largest;
double input;
while (cin»_space; input)
{
if (input > largest)
{
largest = input;
}
}
cout «_space;”Largest number is ” «_space;largest «_space;endl;
*To find the largest (smallest) value, update the largest (smallest)
value seen so far whenever you see a larger (smaller) one.