My approach to solving Programming exercises using C language in Week 2 of Harvard University’s CS50x.
Some Context
During Week 2 of CS50, I was introduced to Arrays and how they are just a bunch of contiguous blocks of memory inside a computer. They pretty much act as a series of variables that are indexed together to hold data of the same type.
Every week of CS50 is focused on a Computer Science topic and has a main lecture along with its Problem Set (Pset for short) which as the name suggests is just a bunch of problems that you need to solve. Some of the problems in a Pset can be optional while some are required to continue the course further.
This week was the very first time I implemented my own functions and arrays in a programming language. Harvard made me do a set of Programming exercises in C to introduce us to the concept of Arrays (hence the title of the week).
Below I have shared in detail my approach to solving the main Problems I solved out of the Pset 2 of CS50x.
Scrabble
Problem Description
This first problem in Pset 2 is based on the real game of scrabble where players have to make words. Each letter is tied to a numeric score and a player with the better score wins.
The goal of this problem was to write a program in C that prompts the user for two string values in the form of Player 1 and Player 2. Once the user enters the two string values, the program computes scores for both users based on the score set provided in the problem description.
The score set tells us what numeric weight each letter of the English Alphabet holds. We have to use that to the calculate score for each player. After calculating the score, the program decides which player has the better score and prints the winner.
My Solution
The way I solved this problem is first I stored the ruleset in a char array starting from index 0. I prompted the user for two string inputs ( one for Player 1 and Player 2). Then I created three functions to do the main operations for this problem.
The three main functions are down below:
score
– This function calculates the score for a usersani
– Short for sanitize does as it sounds. Removes unnecessary values from the user input (spacing, punctuation characters)upper
– It converts all characters to Uppercase so the input to score is uniform
After the main functions, I added the final conditional to decide and print the winner.
Because of Harvard’s Ethics policy, I can’t share the exact code I submitted for my Psets. That’s why, I am sharing obfuscated version of the code I submitted for Problem Set 2.
Below is my code for ‘Scrabble’:
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
int score(string k);
void upper(string k);
void sani(string k);
string p1 = 0;
string p2 = 0;
int p1score = 0;
int p2score = 0;
int ans = 0;
int j = 0;
int i = 0;
int scoreset[26] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};
int main(void)
{
p1 = get_string("Player 1: ");
p2 = get_string("Player 2: ");
//Converts all alphabets to Uppercase
upper(p1);
upper(p2);
//Sanitizes any numbers or symbols
sani(p1);
sani(p2);
// Computes scores for both users
p1score = score(p1);
p2score = score(p2);
// Decides and Prints the Winner
if (p1score > p2score)
printf("Player 1 wins!\n");
else
if (p2score > p1score)
printf("Player 2 wins!\n");
else
printf("Tie!\n");
}
int score(string k)
{
// Exact Code hidden so Harvard doesn't ban me :)
}
void upper(string k)
{
for(i = 0; i < strlen(k); i++)
{
k[i] = toupper(k[i]);
}
}
void sani(string k)
{
// Exact Code hidden so Harvard doesn't ban me :)
}
Readability
Problem Description
Readability was the second optional problem in Pset 2. This was a really fun problem for me (though a bit frustrating at times). I liked it because I was creating a really basic version of a program that has a real world application.
The goal here was to write a program that takes a string input and prints the reading grade of the text according to the Coleman-Liau index. If the reading level is below 1 the program would literally mention ‘Before Grade 1’ and if it’s greater than 16 the program would literally print out ‘After Grade 16’.
However, if the reading grade is any number between 1 and 16, the program would print out the grade.
My Solution
My solution here again was to divide the program into different functions. I created four main functions inside the readability.c
file that would handle the main tasks.
My program would store the user input inside a string variable called text
. Then I went on and created four functions:
- The first function
getletters()
would return the number of alphabetical letters intext
. - The second function
getwords()
would return the number of words intext
. - The third function
getsentences()
would return the sentences intext
. - The fourth function
printgrade()
decides and prints the winner on screen.
The user string is stored in the string variable called text
. We just have to accurately figure out the letters, words and sentences in the text
and put that to the Coleman Liau index formula, Rounding the result we get the score.
Lastly I just put a conditional to print a string when the reading level is Below 1 and when the reading level is After 1. If both cases are false, the program would print out the reading level.
I like this functional approach where I make different functions as it helps me organize the whole program in mind and focus on the individual problems one by one.
Below is obfuscated version of my code for ‘Readability’:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <math.h>
float getletters(string k);
float getwords(string k);
float getsentences(string k);
void printgrade(void);
string text = 0;
float letters = 0;
float words = 0;
float sentences = 0;
float grade = 0;
float textlen = 0;
float L = 0;
float S = 0;
int graderound = 0;
int main(void)
{
text = get_string("Text: ");
textlen = strlen(text);
letters = getletters(text);
words = getwords(text);
sentences = getsentences(text);
L = (letters / words) * 100;
S = (sentences / words) * 100;
grade = 0.0588 * L - 0.296 * S - 15.8;
graderound = round(grade);
printgrade();
}
float getletters(string k)
{
float j = 0;
for(int i = 0; i < textlen; i++)
{
if((text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z'))
j++;
}
return j;
}
float getwords(string k)
{
// Exact Code hidden so Harvard doesn't ban me :)
}
float getsentences(string k)
{
// Exact Code hidden so Harvard doesn't ban me :)
}
void printgrade(void)
{
if(graderound < 1)
printf("Before Grade 1\n");
else
if(graderound >= 16)
printf("Grade 16+\n");
else
printf("Grade %i\n", graderound);
}
Caesar
Problem Description
In the mandatory problem of Pset 2 – ‘Caesar’ we have to implement the Caesar cipher where we use a numeric key to rotate the alphabets in the user string upto a specific number of times to conceal the original message.
The program has to take the numeric key as a command-line argument from the user and then ask for the plaintext input. Once user enters the plaintext, the program ciphers it and prints the ciphertext to the user.
My Solution
I created two main functions for this program:
onlydigits
– This function takes a string input and makes sure all of the characters are just numbers and nothing else.cipher
– This is the main function that takes in the plaintext and returns the ciphertext
The problem description wants us to print out an error if the user inputs any non-positive integer value as the command-line argument for the program. So, the onlydigits
function does that for us. The cipher function has two portions, One that processes the uppercase letters and the other that processes the lowercase letters. It ciphers them based on the key and hands them out as a string that is ciphered.
This is my code for ‘Caesar’:
#include<stdio.h>
#include<cs50.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
int onlydigits(string k);
string cipher(string k);
int digits = 0;
string plain = 0;
int key = 0;
int main(int argc, string argv[]) {
if(argc != 2) {
printf("Usage: ./caesar key\n");
return 1;
}
if(onlydigits(argv[1]) > 0) {
printf("Invalid Key\n");
return 1;
}
key = atoi(argv[1]);
plain = get_string("plaintext: ");
cipher(plain);
printf("ciphertext: %s\n", plain);
}
int onlydigits(string k) {
digits = 0;
for(int i = 0; i < strlen(k); i++) {
if(isdigit(k[i]) == 0) {
digits++;
}
}
return digits;
}
string cipher(string k) {
// Exact Code hidden so Harvard doesn't ban me :)
}
So this marks the end of Week 2 of CS50x, I do have an optional problem which I would do after ending the course or when I feel like. I’ll have it updated down below in this post.