LECTURE 1 Flashcards

1
Q

What qualities are considered when writing “good” code?

A

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.

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

What does IDE stand for? What is an IDE?

A

Integrated development environment. An IDE includes programs and features for writing code.

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

What is a CLI?

A

A CLI is a command-line interface used in terminals. Often, these are the most efficient ways to use a computer.

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

What is a compiler? Why do we need one?

A

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).

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

What is a function? What is an argument?

A

A function is a small action/verb that we use to do something in a program. Functions take arguments (inputs) and return a result.

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

What are the two kinds of output for a function?

A

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).

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

What is a library?

A

A set of code already written () from which we can call functions

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

Declare a variable that stores a string value after prompting one from the user, then stores that return value.

A

string answer = get_string(“What’s your name? “);

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

print “Hello, (user name)” after using get_string() to store the user’s name.

A
// 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);

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

what is a header file?

A

cs50.h is a header file that contains the library of code used (get_string() for example)

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

Why can’t we represent 4 billion as an int?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How would you represent 4 billion then if int is only 32 bits?

A

Use a different type. Long is similar to int but uses 64 bits.

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

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);
}
A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

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

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

What is syntactic sugar and how can it be used with the following code?

int counter = 0;
counter = counter + 1;

A

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++;

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

What is the logical or sign and how is it used?

A

|| indicates logical or. The lecture gave the example of prompting the user to answer the question “do you agree” with y or n. Anything typed that isn’t a char will repeat the question. However, if the user enters a Y instead of a y, the program simply cannot respond and quits. To fix this bug, use ||.

#include 
#include 
int main(void)
{
    char c = get_char("Do you agree? ");
    // Check whether agreed
    if (c == 'Y' || c == 'y')
    {
        printf("Agreed.\n");
    }
    else if (c == 'N' || c == 'n')
    {
        printf("Not agreed.\n");
    }
}
17
Q

Write an infinite loop that prints “hello world”.

A

while (true)
{
printf(“hello, world\n”);
}

The condition “check if true” will never change, and this is an infinite loop.

18
Q

Write a code that loops for a specified number of times. What should the starting value be and why?

A
int i = 0;
while (i < 50)
{
    printf("hello, world\n");
    i++;
}

We start at 0 and run to, but not including 50. This is considered best practice and while alternatives (i = 1; while (i <= 50)) or (i = 50… i–) are correct, they become harder to implement in multiple ways and ultimately confuse the reader of the code.

19
Q

What is a better, more elegant way to write repeat “x amount of times” without a while loop?

A

for (int i = 0; i < 50; i++)
{
printf(“hello, world\n”);
}

The for loop includes all conditions in one line if code -> elegant.

20
Q

include

Improve this code:

int main(void)
{
    printf("meow\n");
    printf("meow\n");
    printf("meow\n");
}
A

include

int main(void)
{
    for (int i = 0; i < 3; i++)
    {
        printf("meow\n");
    }
}

Or, better is:

void meow(void) {
  printf("meow\n");
}
int main(void)
{
    for (int i = 0; i < 3; i++)
    {
        meow();
    }
}
21
Q

include

What is wrong with the following code:

void meow(void) {
  printf("meow\n");
}
int main(void)
{
    for (int i = 0; i < 3; i++)
    {
        meow();
    }
}
A

include

Conventionally, main should be the first function in the program to improve readability. This will impact longer code as it would be harder to read with the main buried.

void meow(void);

int main(void)
{
    for (int i = 0; i < 3; i++)
    {
        meow();
    }
}
void meow(void)
{
    printf("meow\n");
}

We prototype the function meow using “void meow(void);” this tells C that there is a defined function meow, adding it to the scope for main.

22
Q

include

Rewrite the following so that meow() takes an input n that has type int and is used to define the number of times meow is printed in a line. Create a new line and repeat the process 3 times to give the following result:

“meow meow meow
meow meow meow
meow meow meow”

void meow(void);

int main(void)
{
    for (int i = 0; i < 3; i++)
    {
        meow();
    }
}
void meow(void)
{
    printf("meow\n");
}
A

include

void meow(int n);

int main(void)
{
    meow(3);
}
void meow(int n)
{
    for (int i = 0; i < n; i++)
    {
        printf("meow\n");
    }
}

This abstraction allows more to be done with meow -> better design.

23
Q

Write code that prompts the user for a positive integer but ONLY a positive integer. Have it re-prompt if the int is negative or 0.

A
#include 
#include 

int get_positive_int(void);

int main(void)
{
    int i = get_positive_int();
    printf("%i\n", i);
}
// Prompt user for positive integer
int get_positive_int(void)
{
    int n;
    do
    {
        n = get_int("Positive Integer: ");
    }
    while (n < 1);
    return n;
}

Fir the do while, int is declared without assigning a value (done in do) so that n exists within the scope of do AND while.

24
Q

What is floating point imprecision?

A

With finite memory for computers to use, they are unable to find infinite answers. Computers work on approximations, so something like 1/10 for x/y, printf(“%.50f\n”, x / y); we get 0.10000000149… A little more than 0.1.

This is the closest value the computer can store for 1/10, as it cannot store all infinite real numbers.

25
Q

What is integer overflow.

A

When working with a limited number of bits, the computer has no way to store carry-bits. (add 1 to 111, should get 1000. But, if only 3 bits available, gives 000 since there’s no space for the carry.

Y2K occurred when using 2 bits for the date (95, 96… 99) integer overflow was going to store the year 2000 as 00 (the assumed start of the set for 1900’s, and wouldn’t know if 1900 or 2000.)