CS50 Week 1: An Introduction to C Programming

Week 1 C

Programming is an essential skill in today's world. Whether you're developing software, building websites, or working with data, programming provides a powerful set of tools for solving problems and creating innovative solutions. In CS50's week 1, students are introduced to the C programming language, a powerful language used in a wide range of applications, from operating systems to game development.

Lecture

CS50's Week 1 lecture is an engaging and informative introduction to computer programming, it introduces us C(Programming Language), and helps gradually transition from scratch from week 0 to C in week 1, so as to gradually adapt its syntax. The lecture also covers a variety of programming concepts, including variables, conditionals (if-else statements), loops (while, for, do-while loops), data types (int, float, string, char, etc.), comments, and constants. These are all fundamental concepts that are essential for any beginner to understand when learning to code. It also introduces us a popular development environment called VS code. How to compile our source code into machine readable machine code, and how to run it. The lecture also taught some very useful CLI commands and showed how CLI is better than GUI at times.

Lab 1

This week's lab gives a task to write a C program to find how many years it will take for a given initial population of llamas to grow to a given final or end population of llamas. If let we have a population of n llamas, each year, n / 3 new llamas are born, and n / 4 llamas pass away. I calculated the number of years by using a loop to increment the value of start_size until it becomes greater than or equal to end_size accordingly and simultaneously incrementing number of years. And to take values for start_size and end_size, I used a do-while loop as this . For start size the condition was start_size < 9 and for end_size it was end_size < start_size, then after calculating the number of years, I just printed it on the screen using printf.

Problem Set 1

For this week's problem set, there were three problems as follows:

Hello

The Hello problem is to convert a given hello world code to accept user's name as input and print hello, name. To start with the following code is given

#include <stdio.h>

int main(void)
{    
printf("hello, world\n");
}

To accept user's name we can use the cs50 library's get_string() function which returns a string, And to print it format specifier for string can be used in the printf function. This was a comparatively easy problem.

Mario

This problem gives two options, one is easier and another is somewhat harder relative to each other. They called it less comfortable and more comfortable respectively. For the time being, I went with the less comfortable in which you have to implement the below pyramid from mario using ASCII art in C. mario pyramid To solve this problem we have to accept user's input for the height of the pyramid in the range of 1 and 8 inclusive. For this we can use a do-while loop so that we can prompt the user at least once, And then we can use the nested loops as demonstrated in the lecture to obtain the final result.

Cash

As the mario problem this thrid problem also have to options for less comfortable as well as for more comfortable, it is the less comfortable one. In this problem, we have to accept the number of cents as input and calculate the minimum number of coins needed for change for the given cents. We have 4 types coins 25cent quarters, 10cent dimes, 5cent nickels, and 1cent pennies. So we have implement the greedy algorithm to calculate the minimum number of coins. I went with using a for loop kind of as follows

for (int coins = 0; cents >= coin_val; coins++)
{
    cents -= coin_val;
}

Here i initialised coins variable to 0 and then by using a for loop decrement the value of cents by the value of the given coin(e.g. for quarter it is 25) and with each iteration increment the value of coins by 1 unless value of cents is less than the given coin's value.

Conclusion

In conclusion, CS50's Week 1 is an excellent introduction to computer programming and the C programming language. The lectures and labs cover fundamental concepts that are essential for any beginner to understand when learning to code. The problem sets provide a challenging yet rewarding experience to apply the newly learned concepts in practice. Overall, this week has provided a strong foundation to build upon and continue to develop programming skills.