Week 2 Arrays Flashcards
(15 cards)
What would be another (longer) way to compile your code - e.g., make hello.c
clang -o hello hello.c
What does it mean to compile your code?
Turn source code into machine code
What 4 steps happen when you compile (‘make’) your code?
- Preprocessing - preprocessor directives (e.g., #include …) are copied and pasted from their libraries into your code file
- 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
- Assembling - convert assembly code into 0’s and 1’s - so that when assembled the computer outputs binary
- Linking - links together the 0’s and 1’s from all the files - e.g., from hello.c, stdio.h and cs50.h
What is debugging and what are 3 debugging techniques?
Debugging is removing mistakes from your code
- Rubber duck debugging - rubber ducking
- printf
- debug50
How can rubber ducking help to debug your code?
Helps to sound out your own confusion - to try and trigger the lightbulb moment to figure out what the problem is
How can you use printf to debug your code?
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
What different ways can you use debug50 to debug your code?
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
What is an array, and how are they declared?
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 would you declare a 10 x 10 array, that we would picture as a grid?
e.g., bool battleship[10][10]
How can you fill an array?
- 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 do we treat arrays / elements with regards to variables?
- 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 are arrays passed differently to variables
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
What is the process of encryption?
The process of scrambling information in a reversible way
plaintext + key -> cipher -> ciphertext
What does scope mean?
Scope is a characteristic of a variable that defines from which functions that variable may be accessed
What are the two types of variable scopes?
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