# Week 2: Things actually make sense.

Week 2 delved into the process of **debugging and “debug50”**. Using our debugger in VScode to slowly and piece by piece work through a problem. Particularly marking your breakpoints and using this function to see exactly what is happening step by step in your code.

We also touched on “**Rubber Duck Debugging” **which essentially looks at speaking out your code, process and thinking. Using audible language to hear exactly what is going on and how right but more particularly, wrong, your code is. Just like reading a paragraph out loud can help to process the words, the same logic applies for code when writing. I’ve found this particularly helpful — especially pairing this with the use of VScode debugging to talk through and understand exactly what is going on.

`for (int = 1; i < 3; i++);`

This for loop is the code that particularly clicked with me today. It allows for iterating through and counting up until a nominated limit, in this case 3. Today we looked at **arrays** and how they can be utilised. One great example is taking 3 scores from 3 students and looking how we can take these 3 scores, combine them and find an average. We ran through several ways to do this — from a basic lens to a more optimised and concise method, using the formula above.

The most basic way we can look at this is:

- Defining that we are after 3 numbers
- Listing the three students/scores
- Assigning it’s number to the score
- Adding them all up and dividing by 3.

Here is how it looks:

#include <stdio.h>int main (void);

{ int scores[3]; scores[0] = 73;

scores[1] = 44;

scores[2] = 33; printf("Average score: %f\n", score0 + score 1 + score2 / 3);

}

As you can see, this is a very basic but long method. The issue here is that we have actually hard coded the scores into this. So if we were to take some other students scores, we only have these input and would have to change the code on this one. So here would be our next ‘better’ version which could then take in some seperate scores:

int main (void)

int scores[3]; scores[0] = get_int("Score: ");

scores[1] = get_int("Score: ");

scores[2] = get_int("Score: ");printf("Average score: %f\n", score0 + score 1 + score2) / 3;}

This works, but we are limited to only 3 students and scores now. Here is my favourite way we can now take in a number of students, and prompt all their scores to calculate the average. This uses the certain algorithm mentioned at the top of this post that I’ve come to enjoy using and understanding.

int main (void)

{

int n = get_int("How many scores?: ");

int score[n]; for (int = i; i < n; i++)

{

scores[n] = get_int("Score: ");

}

printf("Average Score: %f\n", (score1 + score2 + score3) / 3.0)

This is a more concise and logical way to do this. My only concern is when it comes to adding all the scores up together in our printf line. Given we are opening up the number of scores to the user, we need to potentially build a way to dynamically count and add the number and sum of scores together.