Midterm Flashcards
Linux Command:
- make a new directory
- list files in current directory
- change directory
- display content of file to terminal
- make a new directory: mkdir
- list files in current directory: ls
- change directory: cd
- display content of file to terminal: cat
What do you need in the declaration of an array?
- Type of elements
- Name of array
- Size of array
type name[SIZE];
How do you initialise an array?
specify 1+ values inside { }
int nums[3] = {1, 2, 3};
How do you access a specific index of an array?
arr[index]
Conditional statements
manipulate control flow of program based on the results of BOOLEAN expressions
While loops
As long as (condition) is met, do (thing)
while(x > 0) {
{
always initialize conditions outside of loop
For loops
do (thing) for (number) times
for (int x = 0; x < 10; x += 1) {
}
Functions
What are some important features?
a chunk of code that does something
FEATURES: parameters, argument, declaration, definition, body, return type
What is the difference between a function declaration and a function definition?
DECLARATION: tells the compiler about the existence of a function and its parameters
DEFINITION: provides the implementation of the function
What do we need for file i/o?
- FILE: named collection of data stored in memory
- STREAM: way of reading from and writing to files in a program -> potentially infinite sequence of data
Steps to read from a file
What’s the numonic?
ORC
OPEN the file: connect a filestream to file
READ from file: using cin»_space;
CLOSE: clean up
What operators can we use to read in information from a file?
- Read the next number in the file (until whitespace)
int num;
cin»_space; num; - Read a whole line (until newline character: \n), when you care about whitespace
char next_char;
infile.get(next_char);
What is a pointer?
a variable that stores the ADDRESS of another variable
Pointers enable pass-by reference: function can modify variables outside its scope via pointers to those variables
If we want to modify variables, should we pass them by value or reference?
pass by REFERENCE
any modifications happen to the actual variable (changed throughout the code) not the copy
What is pass by value?
pass by value: copy of the variable gets passed into function
What is pass by reference?
pass by reference: pass variable’s ADDRESS in memory
we have direct access to the original variable, not a copy
*
- DEREFERENCE: access variable itself
- DECLARE pointer
&
ADDRESS of a variable
What is the heap?
- a place to store dynamic memory
- don’t need to know the size
- Allocate using new and delete
- Use VALGRIND to check freed all heap memory
What must we do when using the HEAP?
- use POINTERS (ie. pointer in the stack points to the array in the heap)
- allocate memory using NEW
- free memory using DELETE
Car *car_array = new Car[3];
How can we check that we have freed all of our heap memory?
use valgrind
What is a struct?
package different types of data