Comprehensive Programming Concepts: Loops, Arrays, Functions, and Memory

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/73

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

74 Terms

1
New cards

Repetition

A type of control structure that allows a block of code to be executed multiple times.

2
New cards

Loop Body

The block of statements executed each time the loop iterates.

3
New cards

Iteration of a Loop

A single execution of the loop body.

4
New cards

Loop Control Variable

Variables whose value determines whether a loop should execute another iteration or not.

5
New cards

File

A named portion of secondary memory used to permanently store data.

6
New cards

Output File

Files that have data written to them by a program.

7
New cards

Input File

Files that contain data to be read by a program.

8
New cards

1D Array

A list of elements of the same data type contiguous in main memory.

9
New cards

In Bounds Index (1D Array)

0 ≤ index ≤ size - 1

10
New cards

Out of Bounds Index (1D Array)

index < 0 or index ≥ size

11
New cards

Segmentation Fault

An error which occurs when an invalid location in memory has been accessed or edited.

12
New cards

Record

A collection of data, possibly of different types, fixed in number and sequence.

13
New cards

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.

14
New cards

2D Array

An array of arrays, storing elements in rows and columns like a table or grid.

15
New cards

User-Defined Function

A function the programmer creates to perform a specific task.

16
New cards

Parameters

Variables declared in a function header to act as inputs to receive values the function will process.

17
New cards

Return Value

The actual data (if any) sent back when a function completes.

18
New cards

Return Type

The kind of data (if any) a return value is.

19
New cards

Function Header

The first line of a function definition that specifies a function's return type, identifier, and parameters.

20
New cards

Function Body

The set of instructions (a block of code) that execute when a function is called.

21
New cards

Function Call

Tells the program to execute the code inside a function's body.

22
New cards

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.

23
New cards

Function Signature

A unique fingerprint made up of a function's identifier and types of parameters.

24
New cards

Scope

The region of a program where an identifier can be accessed or used.

25
New cards

Formal Parameters

Variables and constants defined in the function heading to be used in the function body.

26
New cards

Actual Parameters

Actual values passed to a function when it is called.

27
New cards

Local Scope

Identifiers declared inside a block of code that can be accessed only within the block in which they were created.

28
New cards

Global Scope

Identifiers created outside of a block of code that can be accessed anywhere within a program.

29
New cards

Value Parameter

Formal parameter that receives a copy of the value of the corresponding actual parameter.

30
New cards

Reference Parameter

Formal parameter that receives the address of the corresponding actual parameter.

31
New cards

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.

32
New cards

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.

33
New cards

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.

34
New cards

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.

35
New cards

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.

36
New cards

What is a loop control variable?

A variable whose value determines whether a loop continues or stops.

37
New cards

What happens if a loop control variable is not properly updated?

It may cause an infinite loop or a loop that never runs correctly.

38
New cards

How do infinite loops present themselves in programs at runtime?

The program appears frozen, continuously outputs data, never terminates, or becomes unresponsive.

39
New cards

In general, what are the valid indices of a 1D array?

0 through size − 1

40
New cards

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"};

41
New cards

What are the in-bounds indices of that array?

0, 1, 2

42
New cards

What error can happen if an invalid index is accessed in an array?

A segmentation fault.

43
New cards

In general, what does a segmentation fault mean?

It means the program accessed memory that it was not allowed to read or write.

44
New cards

What is a record of data?

A collection of related data fields grouped as one unit.

45
New cards

How can 1000 records of data be stored in a program?

They can be stored in an array, a struct array, or parallel arrays.

46
New cards

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.

47
New cards

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.

48
New cards

Create a 3x3 2D array in 1 line:

char arr[3][3] = {{'r','b','w'}, {'g','g','g'}, {'y','r','r'}};

49
New cards

When accessing a 2D array element what do you need?

Row index and column index.

50
New cards

Difference between a function header and function prototype?

A function header begins the actual function definition. A function prototype declares the function before use.

51
New cards

What happens if a function prototype is forgotten?

The compiler may not know the function exists and will throw an error.

52
New cards

What does a function prototype do?

Tells the compiler the return type, name, and parameters before the function is used.

53
New cards

What is a function signature?

The function name plus its parameter types (not names).

54
New cards

Provide signatures:

countOpenRides → countOpenRides(string[], bool[])

displayRides → displayRides(string[], bool[])

concatenate → concatenate(string, string)

area → area(double)

continue → continue()

55
New cards

Where does the code executed when the function is called go?

Inside the function body.

56
New cards

What are parameters?

Variables listed in a function header used to receive input values.

57
New cards

Difference between actual and formal parameters?

Actual parameters = values passed in the function call. Formal parameters = variables inside the function that receive the values.

58
New cards

Difference between value and reference parameters?

Value → receives a copy. Reference → receives the actual variable's address.

59
New cards

Difference between void and value-returning functions?

Void returns nothing. Value-returning returns a value using return.

60
New cards

How are functions used?

They are called by name, passing required parameters.

61
New cards

What is the point of writing functions?

To organize code, avoid repetition, make programs modular, and simplify debugging.

62
New cards

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;

}

63
New cards

Program 2 (fixed)

#include

#include

using namespace std;

int main()

{

ifstream iFile;

iFile.open("input.txt");

int variable;

iFile >> variable;

iFile.close();

return 0;

}

64
New cards

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;

}

65
New cards

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;

}

66
New cards

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;

}

67
New cards

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;

}

68
New cards

volume

double volume(double r)

{

return (4.0/3.0) 3. r r r;

}

69
New cards

swap

void swap(int arr[], int left, int right)

{

int temp = arr[left];

arr[left] = arr[right];

arr[right] = temp;

}

70
New cards

split

string split(string &toSplit)

{

int pos = toSplit.find(':');

string before = toSplit.substr(0, pos);

toSplit = toSplit.substr(pos + 1);

return before;

}

71
New cards

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;

}

72
New cards

concatenate

string concatenate(const string str1, const string str2)

{

return str1 + str2;

}

73
New cards

printStringToFile

void printStringToFile(const string toPrint)

{

ofstream oFile("output.txt");

oFile << toPrint;

oFile.close();

}

74
New cards

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;

}