A life change when it comes to code
How I better understood what is going on in all the code I’ve tried to type down.
I always find myself lost when trying to understand what is going on in the codes I’ve looked at — even when I typed it. Therefore, at the CS50 Harvard course, professor provided some strategies to better understand what is going on, instead of being stared and frustrated in front of the computer.
Let’s go to the tips:
1Debug is a life-changing tool — at least for me — providing you a step-by-step understanding of the code running the program in a slow manner. If there are some loops it is possible to see what is going on inside of it and the outcomes. In the CS50 course, the syntax is debug50 — your directory/debug50 ./the program you wanna run. Check this out in the code below.
#include <stdio.h>
#include <cs50.h>int main()
{int input, sum = 0, rep;
do{
input = get_int(“Test Data: “);
}
while (input < 1);
So far, this function asks to input an integer, not null and greater than one.
printf(“ The numbers availible for the sum are: %d \n”, input);
Here the number chosen is printed on the screen.
for (rep = 1; rep <= input; rep++)
{
printf(“%d “, rep);
sum = sum + rep;
}
In this part, the loop initiates in 1, and continuos until reach the value add before. Each loop it is added another number into to last one. For example, if starts into 1, next will be 2 and so forth.
In addition, the variable sum that starts in 0 adds up its own value to the number of repetition loop when throughout the loop.
This will loop will have the value input at the beginning typed down on the screen like 1 2 3 4… and also the sum of EACH value.
printf(“\n The sum of each numbers is: %d”, sum);
printf(“\n”);
// printf(“\n The sum of the input %d is : %d \n”, input,sum);}
Finally, it will be printed on the screen.
2 Style50 function helps out to fix how easy reading code was typed. Providing to the programmer suggestions to improve the style for better readable code. To use it, type style50 ./what program do you want.
3 Talk to yourself or a rubber duck — kakakakaka — to solve problems and understand what is going on into the codes. It is a human resource to do that.