Week 2 Arrays Flashcards

(15 cards)

1
Q

What would be another (longer) way to compile your code - e.g., make hello.c

A

clang -o hello hello.c

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

What does it mean to compile your code?

A

Turn source code into machine code

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

What 4 steps happen when you compile (‘make’) your code?

A
  1. Preprocessing - preprocessor directives (e.g., #include …) are copied and pasted from their libraries into your code file
  2. Compiling - code gets translated from C to assembly code - which contains low-level commands that tells the CPU what to do - and has corresponding changes to 0’s and 1’s
  3. Assembling - convert assembly code into 0’s and 1’s - so that when assembled the computer outputs binary
  4. Linking - links together the 0’s and 1’s from all the files - e.g., from hello.c, stdio.h and cs50.h
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is debugging and what are 3 debugging techniques?

A

Debugging is removing mistakes from your code

  1. Rubber duck debugging - rubber ducking
  2. printf
  3. debug50
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can rubber ducking help to debug your code?

A

Helps to sound out your own confusion - to try and trigger the lightbulb moment to figure out what the problem is

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

How can you use printf to debug your code?

A

Can use printf to print extra lines within your code to explain to yourself how it works
- E.g., printf an extra line within a loop to see if it working as it should at different stages of the program

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

What different ways can you use debug50 to debug your code?

A

You call debug50 in the command line: ./hello debug50

  • You set a breakpoint - e.g., where the code is failing - by clicking on the red circle along the sidebar
  • Use the VS code debugger to ‘step over’ lines of code to see how it acts
  • Use ‘step into’ to step into functions to see how it is acting by stepping over each line of code again
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is an array, and how are they declared?

A

A sequence of values, back to back or contiguous in memory - all of the same data type.

Use square brackets:
- type name size: e.g., int menu_prices[8]

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

How would you declare a 10 x 10 array, that we would picture as a grid?

A

e.g., bool battleship[10][10]

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

How can you fill an array?

A
  • With curly braces
  • Dont need to specify the size of the array if we are going to fill it - C can work that out
    bool truthtable[ ] = {false, true, true};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do we treat arrays / elements with regards to variables?

A
  • We can treat each individual element of arrays as variables
  • But in C you cannot treat entire arrays as variables

So to copy the contents of an array into a variable, you need to loop over element by element and set equal to a variable

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

How are arrays passed differently to variables

A

Arrays are passed by reference whereas variables in C are passed by value
- This means that the callee receives the array - not just a copy of it

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

What is the process of encryption?

A

The process of scrambling information in a reversible way

plaintext + key -> cipher -> ciphertext

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

What does scope mean?

A

Scope is a characteristic of a variable that defines from which functions that variable may be accessed

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

What are the two types of variable scopes?

A

Local variables: can only be accessed within the function in which they are created
- i.e. each function receives a copy of the variable - so if change in one function, it wont change it in another

Global variables: can be accessed by any function in the program
- are declared outside of a function

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