1/73
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Repetition
A type of control structure that allows a block of code to be executed multiple times.
Loop Body
The block of statements executed each time the loop iterates.
Iteration of a Loop
A single execution of the loop body.
Loop Control Variable
Variables whose value determines whether a loop should execute another iteration or not.
File
A named portion of secondary memory used to permanently store data.
Output File
Files that have data written to them by a program.
Input File
Files that contain data to be read by a program.
1D Array
A list of elements of the same data type contiguous in main memory.
In Bounds Index (1D Array)
0 ≤ index ≤ size - 1
Out of Bounds Index (1D Array)
index < 0 or index ≥ size
Segmentation Fault
An error which occurs when an invalid location in memory has been accessed or edited.
Record
A collection of data, possibly of different types, fixed in number and sequence.
Parallel Array
Two or more arrays of the same size used to store records, where each array holds a field of the individual records in the same index across the arrays.
2D Array
An array of arrays, storing elements in rows and columns like a table or grid.
User-Defined Function
A function the programmer creates to perform a specific task.
Parameters
Variables declared in a function header to act as inputs to receive values the function will process.
Return Value
The actual data (if any) sent back when a function completes.
Return Type
The kind of data (if any) a return value is.
Function Header
The first line of a function definition that specifies a function's return type, identifier, and parameters.
Function Body
The set of instructions (a block of code) that execute when a function is called.
Function Call
Tells the program to execute the code inside a function's body.
Function Prototype
A declaration that tells the compiler a function exists and how it should be used before the function is fully defined later in a program.
Function Signature
A unique fingerprint made up of a function's identifier and types of parameters.
Scope
The region of a program where an identifier can be accessed or used.
Formal Parameters
Variables and constants defined in the function heading to be used in the function body.
Actual Parameters
Actual values passed to a function when it is called.
Local Scope
Identifiers declared inside a block of code that can be accessed only within the block in which they were created.
Global Scope
Identifiers created outside of a block of code that can be accessed anywhere within a program.
Value Parameter
Formal parameter that receives a copy of the value of the corresponding actual parameter.
Reference Parameter
Formal parameter that receives the address of the corresponding actual parameter.
What are the 3 different types of loops and how do they differ?
while loop - checks condition first, may run 0 times. do...while loop - runs the body at least once, then checks condition. for loop - best when number of iterations is known, includes initialization, condition, update.
When is it best to use a while loop?
When you want a loop that might run 0 or more times and you don't know how many iterations beforehand.
When is it best to use a do...while loop?
When you need the loop body to run at least once before checking the condition.
When do you need the loop body to run at least once before checking the condition?
When you need the loop body to run at least once before checking the condition.
When is it best to use a for loop?
When you know the exact number of iterations or when using a loop control variable that updates in a predictable pattern.
What is a loop control variable?
A variable whose value determines whether a loop continues or stops.
What happens if a loop control variable is not properly updated?
It may cause an infinite loop or a loop that never runs correctly.
How do infinite loops present themselves in programs at runtime?
The program appears frozen, continuously outputs data, never terminates, or becomes unresponsive.
In general, what are the valid indices of a 1D array?
0 through size − 1
In 1 line of code create a 1D array of strings with luke skywalker, leia organa, obi-wan kenobi.
string arr[3] = {"luke skywalker", "leia organa", "obi-wan kenobi"};
What are the in-bounds indices of that array?
0, 1, 2
What error can happen if an invalid index is accessed in an array?
A segmentation fault.
In general, what does a segmentation fault mean?
It means the program accessed memory that it was not allowed to read or write.
What is a record of data?
A collection of related data fields grouped as one unit.
How can 1000 records of data be stored in a program?
They can be stored in an array, a struct array, or parallel arrays.
Explain how parallel arrays are created and maintained.
Multiple arrays of the same length are created, and each index position stores the fields for a single record. Index n in all arrays refers to the same logical item.
How does a 2D array differ from a 1D array?
A 2D array stores data in rows and columns. A 1D array stores data in a single list.
Create a 3x3 2D array in 1 line:
char arr[3][3] = {{'r','b','w'}, {'g','g','g'}, {'y','r','r'}};
When accessing a 2D array element what do you need?
Row index and column index.
Difference between a function header and function prototype?
A function header begins the actual function definition. A function prototype declares the function before use.
What happens if a function prototype is forgotten?
The compiler may not know the function exists and will throw an error.
What does a function prototype do?
Tells the compiler the return type, name, and parameters before the function is used.
What is a function signature?
The function name plus its parameter types (not names).
Provide signatures:
countOpenRides → countOpenRides(string[], bool[])
displayRides → displayRides(string[], bool[])
concatenate → concatenate(string, string)
area → area(double)
continue → continue()
Where does the code executed when the function is called go?
Inside the function body.
What are parameters?
Variables listed in a function header used to receive input values.
Difference between actual and formal parameters?
Actual parameters = values passed in the function call. Formal parameters = variables inside the function that receive the values.
Difference between value and reference parameters?
Value → receives a copy. Reference → receives the actual variable's address.
Difference between void and value-returning functions?
Void returns nothing. Value-returning returns a value using return.
How are functions used?
They are called by name, passing required parameters.
What is the point of writing functions?
To organize code, avoid repetition, make programs modular, and simplify debugging.
Program 1 (fixed)
#include
using namespace std;
int main()
{
int end = 0, i = 0;
do
{
cout << "End: ";
cin >> end;
if (cin.fail() || end < 0)
{
cout << "Invalid number\n";
cin.clear();
cin.ignore(256, '\n');
}
} while (cin.fail() || end < 0);
for (i = 0; i <= end; i++)
{
cout << i << endl;
}
return 0;
}
Program 2 (fixed)
#include
#include
using namespace std;
int main()
{
ifstream iFile;
iFile.open("input.txt");
int variable;
iFile >> variable;
iFile.close();
return 0;
}
Program 3 (fixed)
#include
using namespace std;
int main()
{
const int SIZE = 10;
int arr[SIZE] = {};
for (int i = 0; i < SIZE; i++)
cout << arr[i] << endl;
return 0;
}
Program 4 (fixed)
#include
using namespace std;
int main()
{
const int ROWS = 5, COLUMNS = 3;
int arr[ROWS][COLUMNS] = {};
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
cout << arr[i][j] << " ";
cout << endl;
}
return 0;
}
Program 5 (fixed)
#include
using namespace std;
void multiplyBy3AndPrint(int i)
{
int j = i * 3;
cout << i << " * 3 = " << j << endl;
}
int main()
{
int i = 5;
multiplyBy3AndPrint(i);
return 0;
}
readInt
int readInt(string prompt, int min, int max)
{
int value;
do
{
cout << prompt;
cin >> value;
if (cin.fail() || value < min || value > max)
{
cout << "Invalid input\n";
cin.clear();
cin.ignore(256, '\n');
}
} while (value < min || value > max);
return value;
}
volume
double volume(double r)
{
return (4.0/3.0) 3.
}
swap
void swap(int arr[], int left, int right)
{
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
split
string split(string &toSplit)
{
int pos = toSplit.find(':');
string before = toSplit.substr(0, pos);
toSplit = toSplit.substr(pos + 1);
return before;
}
getString
string getString(const string prompt)
{
string input;
do
{
cout << prompt;
getline(cin, input);
if (input.length() <= 0 || input.length() >= 30)
cout << "Error: invalid length\n";
} while (input.length() <= 0 || input.length() >= 30);
return input;
}
concatenate
string concatenate(const string str1, const string str2)
{
return str1 + str2;
}
printStringToFile
void printStringToFile(const string toPrint)
{
ofstream oFile("output.txt");
oFile << toPrint;
oFile.close();
}
FULL PRACTICE PROGRAM
#include
#include
#include
using namespace std;
string getString(const string prompt)
{
string input;
do
{
cout << prompt;
getline(cin, input);
if (input.length() <= 0 || input.length() >= 30)
cout << "Error: invalid length\n";
} while (input.length() <= 0 || input.length() >= 30);
return input;
}
string concatenate(const string str1, const string str2)
{
return str1 + str2;
}
void printStringToFile(const string toPrint)
{
ofstream oFile("output.txt");
oFile << toPrint;
oFile.close();
}
int main()
{
string a = getString("\nEnter a string: ");
string b = getString("\nEnter another string: ");
string c = concatenate(a, b);
printStringToFile(c);
return 0;
}