Section 3 Flashcards

1
Q

Create an array with no content and 4 items that can go in it

A

string cars[4];

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

for-each Loop through a string array

A

// Create an array of strings
string cars[5] = {“Volvo”, “BMW”, “Ford”, “Mazda”, “Tesla”};

// Loop through strings
for (string car : cars) {
cout &laquo_space;car &laquo_space;“\n”;
}

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

Do you have to add a number for how many items are in your array?

A

No

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

Get the byte size of an array

A

int myNumbers[5] = {10, 20, 30, 40, 50};
cout &laquo_space;sizeof(myNumbers);

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

How do you get the number of items in an array?

A

Take the bytes size and divide by the size of the first element

int myNumbers[5] = {10, 20, 30, 40, 50};
int getArrayLength = sizeof(myNumbers) / sizeof(myNumbers[0]);
cout &laquo_space;getArrayLength;

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

for loop through an array the proper way

A

int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < sizeof(myNumbers) / sizeof(myNumbers[0]); i++) {
cout &laquo_space;myNumbers[i] &laquo_space;“\n”;
}

or

int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i : myNumbers) {
cout &laquo_space;i &laquo_space;“\n”;
}

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

What is a multidimensional array?

Create one and print the last letter of the first array

A

Multiple arrays in one
specify the amount of arrays in your main array, followed by the elements they have in them

string letters[2][4] = {
{ “A”, “B”, “C”, “D” },
{ “E”, “F”, “G”, “H” }
};
cout &laquo_space;letters[0][3]

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

What are structs?

A

Structures
Group several related variables into one place. Each variable in a struct is a member
can contain multiple data types

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

Create a struct with two different data type variables with no value. place the struct in a variable named “myStructure”

Next, give them a value

Next print them

A

// Create a structure variable called myStructure
struct {
int myNum;
string myString;
} myStructure;

// Assign values to members of myStructure
myStructure.myNum = 1;
myStructure.myString = “Hello World!”;

// Print members of myStructure
cout &laquo_space;myStructure.myNum &laquo_space;“\n”;
cout &laquo_space;myStructure.myString &laquo_space;“\n”;

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

Create a structure that contains three variables:
brand
model
year

Create two variables that contain the structure.

Assign values to all variables

print everything

A

struct {
string brand;
string model;
int year;
} myCar1, myCar2; // We can add variables by separating them with a comma here

// Put data into the first structure
myCar1.brand = “BMW”;
myCar1.model = “X5”;
myCar1.year = 1999;

// Put data into the second structure
myCar2.brand = “Ford”;
myCar2.model = “Mustang”;
myCar2.year = 1969;

// Print the structure members
cout &laquo_space;myCar1.brand &laquo_space;” “ &laquo_space;myCar1.model &laquo_space;” “ &laquo_space;myCar1.year &laquo_space;“\n”;
cout &laquo_space;myCar2.brand &laquo_space;” “ &laquo_space;myCar2.model &laquo_space;” “ &laquo_space;myCar2.year &laquo_space;“\n”;

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

Create a struct with the name mydataType

Declare a variable that uses the struct by it’s datatype

A

// Declare a structure named “car”
struct car {
string brand;
string model;
int year;
};

int main() {
// Create a car structure and store it in myCar1;
car myCar1;
myCar1.brand = “BMW”;
myCar1.model = “X5”;
myCar1.year = 1999;

// Create another car structure and store it in myCar2;
car myCar2;
myCar2.brand = “Ford”;
myCar2.model = “Mustang”;
myCar2.year = 1969;

// Print the structure members
cout &laquo_space;myCar1.brand &laquo_space;” “ &laquo_space;myCar1.model &laquo_space;” “ &laquo_space;myCar1.year &laquo_space;“\n”;
cout &laquo_space;myCar2.brand &laquo_space;” “ &laquo_space;myCar2.model &laquo_space;” “ &laquo_space;myCar2.year &laquo_space;“\n”;

return 0;
}

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

What is an enum?

A

Group of constants, sounds like a tuple

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

Create an enum list and then access it

A

include <iostream></iostream>

using namespace std;

enum Level {
LOW,
MEDIUM,
HIGH
};

int main() {
enum Level myVar = MEDIUM;
cout &laquo_space;myVar;
return 0;
}

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

Change the values of LOW MEDIUM and HIGH in an enum

A

enum Level {
LOW = 25,
MEDIUM = 50,
HIGH = 75
};

int main() {
enum Level myVar = MEDIUM;
cout &laquo_space;myVar;
return 0;
}

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

What is a reference variable?

Create one

A

It references another variable

string food = “Pizza”;
string &meal = food;

cout &laquo_space;food &laquo_space;“\n”; // Outputs Pizza
cout &laquo_space;meal &laquo_space;“\n”; // Outputs Pizza

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

What is a memory address?

Create a variable and get it’s memory address

A

Location where variable is stored on the computer.

string food = “Pizza”;

cout &laquo_space;&food; // Outputs 0x6dfed4

17
Q

What is a pointer?

Create one

A

Variable that stores it’s memory address as its value.
You create one with the “*’ operator.

string food = “Pizza”; // A food variable of type string

string* ptr = &food; // A pointer variable, with the name ptr, that stores the address of food

// Output the value of food (Pizza)
cout &laquo_space;food &laquo_space;“\n”;

// Output the memory address of food (0x6dfed4)
cout &laquo_space;&food &laquo_space;“\n”;

// Output the memory address of food with the pointer (0x6dfed4)
cout &laquo_space;ptr &laquo_space;“\n”;

18
Q

What are three ways of declaring a pointer?

A

string* mystring; // Preferred
string *mystring;
string * mystring;

19
Q

use the DEREFERENCE OPERATOR to get the value of a variable from the pointer

A

string food = “Pizza”; // Variable declaration
string* ptr = &food; // Pointer declaration

// Reference: Output the memory address of food with the pointer (0x6dfed4)
cout &laquo_space;ptr &laquo_space;“\n”;

// Dereference: Output the value of food with the pointer (Pizza)
cout &laquo_space;*ptr &laquo_space;“\n”;

*
When used in declaration (string* ptr), it creates a pointer variable.
When not used in declaration, it act as a dereference operator.

20
Q

What happens if you change a pointer value using a dereference operator?

A

It will change the original variable’s value as well

string food = “Pizza”;
string* ptr = &food;

// Output the value of food (Pizza)
cout &laquo_space;food &laquo_space;“\n”;

// Output the memory address of food (0x6dfed4)
cout &laquo_space;&food &laquo_space;“\n”;

// Access the memory address of food and output its value (Pizza)
cout &laquo_space;*ptr &laquo_space;“\n”;

// Change the value of the pointer
*ptr = “Hamburger”;

// Output the new value of the pointer (Hamburger)
cout &laquo_space;*ptr &laquo_space;“\n”;

// Output the new value of the food variable (Hamburger)
cout &laquo_space;food &laquo_space;“\n”;

21
Q

Declare a function and then call it

A

void myFunction() {
cout &laquo_space;“I just got executed!”;
}

int main() {
myFunction(); // call the function
return 0;
}

void - this means the function doesn’t return a value

body - inside of the function

22
Q

What are the two sections of a function called?

A

Declaration - return type, name of function and parameters

Definition - body

23
Q

Can you put a function below the main function?

Can you declare a function and give it a definition later?

A

No

Yes

void myFunction();

// The main method
int main() {
myFunction(); // call the function
return 0;
}

// Function definition
void myFunction() {
cout &laquo_space;“I just got executed!”;
}

24
Q

Create a function that takes a name as an argument (give it a parameter)
Have that function add a last name and then print to screen

A

void myFunction(string fname) {
cout &laquo_space;fname &laquo_space;” Refsnes\n”;
}

int main() {
myFunction(“Liam”);
myFunction(“Jenny”);
myFunction(“Anja”);
return 0;
}

25
Q

Give a function a default parameter

A

void myFunction(string country = “Norway”) {
cout &laquo_space;country &laquo_space;“\n”;
}

int main() {
myFunction(“Sweden”);
myFunction(“India”);
myFunction();
myFunction(“USA”);
return 0;
}