Exam 2 Computer Science

5.0(1)
studied byStudied by 480 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/66

flashcard set

Earn XP

Description and Tags

For CS135 students and friends

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

67 Terms

1
New cards

File

An Area in secondary storage used to hold information.

C++ Standard Template Library includes a header file for file I/O: fstream

Header to only use input from a file: ifstream

Header to only use output to a file: ofstream

2
New cards

File I/O

knowt flashcard image
3
New cards

Strings Continued

knowt flashcard image
4
New cards

Command Line Arguments

A parameter supplied to the program when it is invoked.

5
New cards

Using Command Line Arguments

knowt flashcard image
6
New cards

List

A collection of components arranged one after another

7
New cards

(1D)Array

A list containing a collection of a fixed number of components of the same data type.

8
New cards

Arrays

knowt flashcard image
9
New cards

Accessing Information in Arrays

knowt flashcard image
10
New cards

Processing Arrays Types

1. Initializing an array

2. Reading data into an array

3. Printing an array

4. Finding the sum/average of an array

5. Finding the largest/smallest element in an array

11
New cards

Initializing an Array

knowt flashcard image
12
New cards

Reading Data Into an Array

knowt flashcard image
13
New cards

Printing an Array

knowt flashcard image
14
New cards

Finding the Sum/Average of an Array

knowt flashcard image
15
New cards

Finding the Min/Max of an Array

knowt flashcard image
16
New cards

In-Bound Index

An index that is 0 <= index <= arraySize -1.

17
New cards

Out-Of-Bounds Index

An index that is index < 0 or arraySize - 1 < index.

18
New cards

Array Out of Bounds

knowt flashcard image
19
New cards

Copying One Array Into Another Array

knowt flashcard image
20
New cards

Two-Dimensional Array

A collection of a fixed number of components arranged in rows and columns, wherein all the components are of the same data type.

21
New cards

2D Arrays

knowt flashcard image
22
New cards

Void Functions

Functions that do not have a return type. These functions do not return a value or use a return statement.

23
New cards

Value-Returning Functions

Functions that have a return type. These functions return a value of a specific data type using the return statement.

24
New cards

Void Functions Without Parameters

knowt flashcard image
25
New cards

Void Functions With Parameters

knowt flashcard image
26
New cards

Value-Returning Functions

knowt flashcard image
27
New cards

Formal Parameters

A variable declared in the function heading.

● The formal parameters are the parameters you can use only in your user-defined function.

● General syntax (Formal Parameter List): dataType identifier, dataType identifier, …

28
New cards

Actual Parameters

A variable or expression listed in a call to a function.

● The actual parameters are the parameters you can use only in your calling function.

● General syntax (Actual Parameter List): expression or variable, expression or variable,

29
New cards

Scope

Where in the program an identifier is accessible (visible).

30
New cards

Local Identifier

Identifiers declared within a function or block of code. These identifiers are not accessible outside of the function or block of code they are declared in.

31
New cards

Global Identifier

Identifiers declared outside of every function definition. These identifiers are accessible from any function in your program (but not without side effects).

32
New cards

Rules when accessing identifiers:

knowt flashcard image
33
New cards

Function Prototype

The function heading without the body of the function or identifiers of the formal parameters.

34
New cards

Function Prototype Page

knowt flashcard image
35
New cards

Value Parameters

A formal parameter that receives a copy of the value of the corresponding actual parameter.

36
New cards

Value Parameter Page

knowt flashcard image
37
New cards

Reference Parameters

A formal parameter that receives the address (memory location) of the corresponding actual parameter.

38
New cards

Reference Parameters Page

knowt flashcard image
39
New cards

Default Parameters

knowt flashcard image
40
New cards

Function Overloading

Creating several functions with the same name.

41
New cards

Function Overloading Page

knowt flashcard image
42
New cards

Control Structures

knowt flashcard image
43
New cards

While Looping

knowt flashcard image
44
New cards

Types of While Loops

knowt flashcard image
45
New cards

For Looping

knowt flashcard image
46
New cards

Do While Looping

knowt flashcard image
47
New cards

Break and continue

knowt flashcard image
48
New cards

Nested Control Structures

knowt flashcard image
49
New cards

Explain the difference between break and continue statements in a loop.

The break statement is used to exit a loop prematurely, whereas the continue statement is used to skip the rest of the loop's code and move to the next iteration.

50
New cards

What loop is best to use when you know the number of iterations?

For loops

51
New cards

What loop is best to use when you at least want one iteration?

Do ... while loop.

52
New cards

Why should we not use global variables in our program?

If you use a global variable throughout your program, it can be very hard to track down bugs with them. Problems caused by a global variable in one part of a program may be understood as problems in other parts of a program. Since the variable scope is larger, that means more code to surf through.

53
New cards

Given the following code snippet, what is the output after execution?

int n = 3;

for (int i = 1; i <= n; ++i) {

for (int j = 1; j <= i; ++j) {

cout << j << " ";

}

cout << endl;

}

1

1 2

1 2 3

54
New cards

In a few lines of code, verify if the file was open.

if(reader.is_open()){

cout<<"Valid file"«endl;

}

55
New cards

Explain the purpose of the library in C++.

Used for file input and output operations. It provides classes like ifstream (input file stream) and ofstream (output file stream) that enable reading from and writing to files

56
New cards

Create an 2D integer array named arr with 5 columns and 3 rows.

const int rows = 3;

const int columns = 5;

int arr[rows][columns];

57
New cards

With a few lines of code, print all elements of an array named values with 8 elements.

for (int i = 0; i < 8; ++i) {

cout << values[i] << " ";

}

58
New cards

Why is it important to be cautious about accessing elements beyond the bounds of an array in C++?

Accessing elements beyond the bounds of an array in C++ can lead to undefined behavior, including segmentation faults (seg faults). This occurs because memory outside the allocated array space may not belong to the program, causing unexpected results or crashes.

59
New cards

List the general steps of reading data from a text file into a one-dimensional array in C++.

1. Include Necessary Headers: #include <fstream>

2. Declare an Input File Stream

3. Open the File

4. Declare and Initialize the Array

5. Read Data from the File

6. Close the File

60
New cards

Provide the function signature for each function.

void acc(int id, char alpha, double cash);

int fire(float radius, float temperature = 60);

string ice(int temp = 31, bool shard = 29);

void bomb(int time[], int power, double radius);

void shift(int x, int pos[], double z);

acc(int, char, double)

fire(float, float) fire(float)

ice(int, bool) ice(int) ice()

bomb(int[], int, double)

shift(int, int[], double)

61
New cards

For a function: int findArea(int length, int width), provide an example that uses function overloading.

int findArea(int radius)

62
New cards

For the following program, correctly label the function prototype, body, call, and header.

1. Prototype

2. Call

3. Header

4. Body

<p>1. Prototype </p><p>2. Call </p><p>3. Header </p><p>4. Body</p>
63
New cards

Why are arrays always passed by reference to functions?

Arrays are a contiguous block of memory. Passing them by reference (by providing the memory address) avoids the overhead of copying the entire array, making function calls more efficient.

64
New cards
term image

(Ideas on how to approach this but not perfect)

<p>(Ideas on how to approach this but not perfect)</p>
65
New cards
term image

(Ideas on how to approach this but not perfect)

<p><span>(Ideas on how to approach this but not perfect)</span></p>
66
New cards
term image

(Ideas on how to approach this but not perfect)

<p><span>(Ideas on how to approach this but not perfect)</span></p>
67
New cards

Function Signature

The function name and formal parameter list data types. The signature does not include the return type of the function.