CS50 Lecture 2- Arrays

and more…

Volkan
4 min readSep 25, 2020

Lecture Summary

Lecture starts with Compliers. As David said, we look underneath the hood. He explains, what happened when you write “make something” on the command line. And explained what are compliers and how they works.

Another topic is debugging. He showed some ways to debugging code. Those are using printf() function to get some output from code. Another method is using breakpoints to check code line by line. And finally, he talked about rubber duck debugging. Which is about explain code to yourself loudly to understand where and how the problem occurs.

He then explains how computer uses memories and talked about arrays. Lastly he showed some use cases about ASCII table.

Rubber Duck Debugging is a method for Debug your Code

4 Clean Code Advice

In the lecture David emphasized 4 advice for writing clean code.

  1. Never repeat yourself. Also not for assignments.
// Instead Of Writing 3 for Array Size and for Loop Test Part #include <stdio.h>
#include <cs50.h>
int main(void){ int scores[3]; for (int i = 0; i < 3; i++)
{
scores[i] = get_int("Score: ");
}
...
}
// Create a CONSTANT for Array Size and for Loop Test Part#include <stdio.h>
#include <cs50.h>
const TOTAL = 3;int main(void){
int scores[TOTAL];
for (int i = 0; i < TOTAL; i++)
{
scores[i] = get_int("Score: ");
}
...
}

Why we Do This : In case we need to update size of array we just update only constant variable.

2. No Hardcoding if possible.

//Instead of adding elements one by one to array.#include <cs50.h>
#include <stdio.h>

int main(void)
{
int scores[3];
scores[0] = get_int("Score: ");
scores[1] = get_int("Score: ");
scores[2] = get_int("Score: ");

// Print average
printf("Average: %f\n", (scores[0] + scores[1] + scores[2]) / 3.0);
}
// Use loops to make your code dynamic.#include <cs50.h>
#include <stdio.h>

int main(void)
{
int scores[3];
for (int i = 0; i < 3; i++)
{
scores[i] = get_int("Score: ");
}

// Print average
printf("Average: %f\n", (scores[0] + scores[1] + scores[2]) / 3.0);
}

Why we Do This : Instead of adding another line of code, in case you want to add another element to array, just increase the size of array.

3. Assign different works to different functions.

// Instead Of Doing Average calculation inside main function#include <stdio.h>
#include <cs50.h>
const TOTAL = 3;int main(void){
int scores[TOTAL];
for (int i = 0; i < TOTAL; i++)
{
scores[i] = get_int("Score: ");
}
int sum = 0;
for (int i = 0; i < length; i++)
{
sum += array[i];
}
int average = sum / (float) length;
print("Average : %f \n", average);
}// Separate Average function and Call it from main function#include <stdio.h>
#include <cs50.h>
float average(int length, int array[]);
const TOTAL = 3;
int main(void){
int scores[TOTAL];
for (int i = 0; i < TOTAL; i++)
{
scores[i] = get_int("Score: ");
}

print("Average : %f \n", average(TOTAL, scores));
}float average(int length, int array[])
{
int sum = 0;
for (int i = 0; i < length; i++)
{
sum += array[i];
}
return sum / (float) length;
}

Why we Do This : To tidy up our code. If you do everything inside your main function, readability of your code will be decrease and it will be hard to follow during debug session.

4. In Loops; avoid using functions in test part for instance; for(int i = 0; i < strlen(s); i++)

// Instead of adding function to for loop test part.#include <cs50.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
string s = get_string("Input: ");
printf("Output: ");
for (int i = 0; i < strlen(s); i++)
{
printf("%c", s[i]);
}
printf("\n");
}
// Assign return of the function in a variable.#include <cs50.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
string s = get_string("Input: ");
printf("Output: ");
int length = strlen(s);
for (int i = 0; i < length; i++)
{
printf("%c", s[i]);
}
printf("\n");
}

Why we Do This : If we add function to for loop it will do the same calculation every iteration which is an additional work for computer.

Problem Sets and Lab

On Lab, you will create an application to simulate the game Scrabble (sort of). There are 2 players write words sequentially, then ou will decide which word has the highest point, according to the value table for each letters. This Lab prepare you to the problem set that you will use ASCII Table to solve problems.

There are 3 problem on Problem Set 2. First, you will decide readability of a text according to Coleman-Liau index. And the other 2 problem is about cipher.

My solutions for the lab and problem set is on the link below. Hope to see you on Week 3. Happy Coding.

Github : https://github.com/volkansahn/CS50-Fall2020.

--

--