LECTURE 1 Flashcards
What qualities are considered when writing “good” code?
Correctness, or whether our code works correctly, as intended.
Design, or a subjective measure of how well-written our code is, based on how efficient it is and how elegant or logically readable it is, without unnecessary repetition.
Style, or how aesthetically formatted our code is, in terms of consistent indentation and other placement of symbols. Differences in style don’t affect the correctness or meaning of our code, but affect how readable it is visually.
What does IDE stand for? What is an IDE?
Integrated development environment. An IDE includes programs and features for writing code.
What is a CLI?
A CLI is a command-line interface used in terminals. Often, these are the most efficient ways to use a computer.
What is a compiler? Why do we need one?
It is a program that converts source code into machine language (exclusively made from binary). It is needed so that a human language that has been optimized for translation (C), can be compiled (translated into Machine Language).
What is a function? What is an argument?
A function is a small action/verb that we use to do something in a program. Functions take arguments (inputs) and return a result.
What are the two kinds of output for a function?
The side effect (printing on the screen) and the return values (a value that is passed back to our program that we can use or store for later).
What is a library?
A set of code already written () from which we can call functions
Declare a variable that stores a string value after prompting one from the user, then stores that return value.
string answer = get_string(“What’s your name? “);
print “Hello, (user name)” after using get_string() to store the user’s name.
// the %s is required to indicate the format of // whats being added to "hello" (, answer) // \n adds a new line so that $ moves down
printf(“hello, %s\n”, answer);
what is a header file?
cs50.h is a header file that contains the library of code used (get_string() for example)
Why can’t we represent 4 billion as an int?
Because int is a 32 bit type in C, and 4 billion is beyond the limits of 32 bits (int includes positive and negative numbers. 32 bits allows about 4 billion possibilities, but half are occupied by negative 2 billion, the other by positive 2 billion.
How would you represent 4 billion then if int is only 32 bits?
Use a different type. Long is similar to int but uses 64 bits.
What will the following code return if x = 1 and y = 2?
#include #include
int main(void) { // Get numbers from user int x = get_int("x: "); int y = get_int("y: ");
// Divide x by y float z = x / y; printf("%f\n", z); }
The result, though assigned the type float, will be truncated because that line will be executed right to left, and the truncated int value 0 will be converted to the float 0.0000.
How can we change the following code to get an appropriate answer?
#include #include
int main(void) { // Get numbers from user int x = get_int("x: "); int y = get_int("y: ");
// Divide x by y float z = x / y; printf("%f\n", z); }
> > > 0.00000
Use type casting to convert x and y to floats before dividing them.
#include #include
int main(void) { // Get numbers from user int x = get_int("x: "); int y = get_int("y: ");
// Divide x by y float z = (float)x / (float)y; printf("%f\n", z); }
> > > 0.50000
What is syntactic sugar and how can it be used with the following code?
int counter = 0;
counter = counter + 1;
Syntactic sugar is a way to write code that makes more visual sense without adding functionality.
Two codes are equivalent to the example.
int counter = 0;
counter = counter + 1;
int counter = 0;
counter++;