L1: Questions // Side videos Flashcards

1
Q

In a do while loop, why do we return the variable n?

A

The variable can only exist within the scope defined by curly braces. For the sake of the do while, it is defined in “int n;” above do so that it can then be checked in while. We then return the value at the end of the function so that it can be used in the rest of the program, outside of the curly braces it was defined in.

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

In any f(), we declare a type before the function and in the parenthesis, what do these refer to? What would it mean if either of these types were defined as void?

A

If the function reads as “void function(int)”, we would be saying that the function gives no output, but takes an int as the input. Vice versa, “int function(void)” would say the function doesn’t take input but gives an int output.

To be clear: the input here refers to input from other lines of code. A function(void) can still take user input as a part of the function.

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

What is type unassigned int?

A

Unassigned is a type modifier that excludes values less than zero, giving the 32 bits int starts with exclusively to positive integers.

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

How do we define what floats can be declared?

A

32 bits are available, but there is no defined number of possible values. Only that we must define the precision based on available decimal places. The higher the left side of the decimal point, the fewer are available for the right side of the decimal place.

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

How do we increase the precision of a float?

A

Use a double instead.

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

What is the void type?

A

It is not a “data type”, simply a type. Since variables are data specific, they cannot be declared as void. This restricts void to being used with functions.

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

Improve the following code:

int x;
int y;
int z;

A

Reduce to one line:

int x, y, z;

When giving values to these later, do not redeclare the type (x = 2; // y = 4;)

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

Bool isn’t a type in C, declared in cs50.h. How are these parsed in C?

A

All nonzero value is true. Zero is always false. Because of this, we can use bool operators without defining the bool type.

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

define ||, &&, !x

A

They are not, and, not x respectively.

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

what is mutual exclusivity in conditional statements?

A

Nested if statements are mutually exclusive. Meeting one of the if statements nested does not exclude the other if statements from running if their conditions are also met.

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

When writing nested if statements, how will an else be handled?

A

The else that is declared at the end is exclusive to the if statement it follows, and will still run even if an earlier if statement is false.

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

What is a switch(x)?

A

Switches run starting from the conditional that meets the same value as whatever “case x: // printf(‘###’) // break;” The break statement is necessary to keep the switch from running all cases that follow, but can be omitted if wanting to run through to the (default:).

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

Define in steps 1, 2, and 3 what the following does:

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

A
  1. initiate counter variable (defining the type and value.
  2. evaluate the boolean value (run if true, quit if false)
  3. increment the value when running as true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

The loops while, do while, and for are best used in certain situations. Give an example for each.

A

While: runs as long as the condition is met. Good for a control flow when the duration is unknown (how long a player plays the game)

do-while: good to check inputs and run a code once.

for: good for defined numbers of runs on a set of code.

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

What OS is the CS50 IDE in?

A

Ubuntu. Is a linux distro, so is unix based and runs same CLI commands as Mac.

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