Exam 2 Flashcards

(33 cards)

1
Q

can you change the size of an array?

A

no

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

what do arrays do?

A

access!

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

what is an element

A

item in an array

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

how do you declare an array

A

dataType arrayName[numElements];

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

how do you initialize an array

A

int myArray[3] = {5, 7, 9};

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

how do you call an array

A

FunctionName(arrayName, numElements);

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

when a function is used in a parameter, what changes?

A

use brackets
FunctionName(arrayName[], numElements) {}

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

what does const do

A

use const in parameter to prevent a function from modifying an array
ex:
PrintArray(const double userNums[], int numVals)

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

examples of when to use const

A
  • when sorting an array
  • when reordering an array
    *both reordering elements
    *element reversal doesn’t have to modify the array, but it’s good practice to use const in this case
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what do you use when swapping elements in an array?

A

A TEMP VALUE! –> 1. temp, 2. swap, 3 temp
ex:
int temp;
temp = array[0];
array[0] = array[size - 1];
array[size - 1] = temp;
*swaps first and last elments

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

main features of 2D arrays

A

array[row][col]

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

how do you initialize a 2D array

A

int numVals[2][3] = {{22, 33, 44,}, {49, 99, 34}};

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

what is a character array

A

an array that sorts items in a string into characters into elements

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

what is the null character

A

‘\0’
when a string is shorter than the array, this character indicates the end of the string and STOPS PRINTING

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

how do you declare a char array

A

char firstName[10] = “Henry”;

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

what are the important things to remember about char arrays

A

**STRING CANNOT EXCEED ARRAY
**
you cannot reassign a char array later, but you can copy

17
Q

what do pass by reference and pass by value concern?

A

how arguments are passed into functions and if they can be changed

18
Q

pass by value

A

default in c++
argument’s are copied into local variables; you are altering copies of the original variable

19
Q

pass by reference

A

use &
- refers directly to the argument variable’s memory location
- whatever you do to the parameter, you do to the argument
- use when outputs are related

ex:
int main {
int x = 1;
foo(x);
cout &laquo_space;x;
}
void foo(int& p) {
p = 8;
}
*output will be 8!

20
Q

what is ofstream

A

output file stream
- reads to files
- way to output data to screen, file, etc

21
Q

what is ifstream

A

input file stream
- reading from files

22
Q

cin

A
  • predefined istream object associated with a system’s standard input (computer keyboard)
23
Q

cout

A
  • predefined ostream object (ostream cout;)
  • cout returns a reference to the ostream that called the operator
24
Q

ostream

A

output stream
class that supports output

25
istream
input stream class that supports input
26
ifstream steps
1. declare stream 2. open file and associate it with input stream 3. use stream to read from file (>>) 4. close the file
27
ofstream steps
1. declare stream 2. open file 3. use stream to write to file (<<) 4. close the file
28
eof() function purpose
returns true if the previous stream operation reached the end of the file
29
fail() function purpose
returns true if the previous stream operation had an error
30
getline() function purpose
reads a line of input
31
good() function purpose
returns true if no stream errors occurred *after file is open, use this function to ensure that the stream is in an error free state
32
bad() function purpose
indicates error occurred while reading/writing stream, and stream is bad
33
return 1
use to terminate the program