Tag: solutions

  • The Solution to CS50 labs 5 & psets 5 problems (2022)

    The Solution to CS50 labs 5 & psets 5 problems (2022)

    Disclaimer: These answers are only for educational purposes only. Please do not use them for cheating. Cheating doesn’t do any good for you!

    In this post, I’m giving you my solutions to CS50 lab 5 and problem set 5 problems. Hope these solutions will help you to better understand the problems and be a guide for your solutions too.

    The Solution to CS50 labs 5 Inheritance Problem (2022)

    For this problem, we have to write a code that simulate the inheritance of blood types of each member of a family. So, a C file called inheritance.c is provided for us. And the code in this file is not complete. We have to complete the code using the given instructions in the manual.

    Here is my solution to this problem,

    // Simulate genetic inheritance of blood type
    
    #include <stdbool.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    // Each person has two parents and two alleles
    typedef struct person
    {
        struct person *parents[2];
        char alleles[2];
    }
    person;
    
    const int GENERATIONS = 3;
    const int INDENT_LENGTH = 4;
    
    person *create_family(int generations);
    void print_family(person *p, int generation);
    void free_family(person *p);
    char random_allele();
    
    int main(void)
    {
        // Seed random number generator
        srand(time(0));
    
        // Create a new family with three generations
        person *p = create_family(GENERATIONS);
    
        // Print family tree of blood types
        print_family(p, 0);
    
        // Free memory
        free_family(p);
    }
    
    // Create a new individual with `generations`
    person *create_family(int generations)
    {
        // TODO: Allocate memory for new person
        person *n = malloc(sizeof(person));
        if (n == NULL)
        {
            return NULL;
        }
    
        // If there are still generations left to create
        if (generations > 1)
        {
            // Create two new parents for current person by recursively calling create_family
            person *parent0 = create_family(generations - 1);
            person *parent1 = create_family(generations - 1);
    
            // TODO: Set parent pointers for current person
            n->parents[0] = parent0;
            n->parents[1] = parent1;
    
            // TODO: Randomly assign current person's alleles based on the alleles of their parents
            n->alleles[0] = n->parents[0]->alleles[rand() % 2];
            n->alleles[1] = n->parents[1]->alleles[rand() % 2];
        }
    
        // If there are no generations left to create
        else
        {
            // TODO: Set parent pointers to NULL
            n->parents[0] = NULL;
            n->parents[1] = NULL;
    
            // TODO: Randomly assign alleles
            n->alleles[0] = random_allele();
            n->alleles[1] = random_allele();
    
        }
    
        // TODO: Return newly created person
        return n;
    }
    
    // Free `p` and all ancestors of `p`.
    void free_family(person *p)
    {
        // TODO: Handle base case
        if (p == NULL)
        {
            return;
        }
    
        // TODO: Free parents recursively
        free_family(p->parents[0]);
        free_family(p->parents[1]);
    
        // TODO: Free child
        free(p);
    
    }
    
    // Print each family member and their alleles.
    void print_family(person *p, int generation)
    {
        // Handle base case
        if (p == NULL)
        {
            return;
        }
    
        // Print indentation
        for (int i = 0; i < generation * INDENT_LENGTH; i++)
        {
            printf(" ");
        }
    
        // Print person
        if (generation == 0)
        {
            printf("Child (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]);
        }
        else if (generation == 1)
        {
            printf("Parent (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]);
        }
        else
        {
            for (int i = 0; i < generation - 2; i++)
            {
                printf("Great-");
            }
            printf("Grandparent (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]);
        }
    
        // Print parents of current generation
        print_family(p->parents[0], generation + 1);
        print_family(p->parents[1], generation + 1);
    }
    
    // Randomly chooses a blood type allele.
    char random_allele()
    {
        int r = rand() % 3;
        if (r == 0)
        {
            return 'A';
        }
        else if (r == 1)
        {
            return 'B';
        }
        else
        {
            return 'O';
        }
    }

    The Solution to CS50 psets 5 Speller problem (2022)

    For this problem, we have to implement a C program that spell-checks a file using hash tables. Bunch of files are provided for us for this problem set. But we just have to make changes only to the dictionary.c file. We have to use hash tables for this problem. All the required guide is given to us in the website.

    So, here is what my solution looks like to this problem.

    // Implements a dictionary's functionality
    
    #include <ctype.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <strings.h>
    #include <cs50.h>
    
    #include "dictionary.h"
    
    // Represents a node in a hash table
    typedef struct node
    {
        char word[LENGTH + 1];
        struct node *next;
    }
    node;
    
    // TODO: Choose number of buckets in hash table
    const unsigned int N = (LENGTH + 1) *'z';
    
    int total_words = 0;
    
    // Hash table
    node *table[N];
    
    // Returns true if word is in dictionary, else false
    bool check(const char *word)
    {
        // TODO
        // Obtain the hash index
        int index = hash(word);
    
        node *cursor = table[index];
        while (cursor != NULL)
        {
            if (strcasecmp(cursor->word, word) == 0)
            {
                return true;
            }
            cursor = cursor->next;
        }
    
        return false;
    }
    
    // Hashes word to a number
    unsigned int hash(const char *word)
    {
        // TODO: Improve this hash function
        int sum = 0;
        for (int i = 0; i < strlen(word); i++)
        {
            sum += tolower(word[i]);
        }
        return (sum % N);
    }
    
    // Loads dictionary into memory, returning true if successful, else false
    bool load(const char *dictionary)
    {
        // TODO
        // open the dictionary
        FILE *file = fopen(dictionary, "r");
        if (file == NULL)
        {
            return false;
        }
    
        // read one word at a time from file
        char word[LENGTH + 1];
    
        while (fscanf(file, "%s", word) != EOF)
        {
            // create a new node
            node *n = malloc(sizeof(node));
            if (n == NULL)
            {
                return false;
            }
    
            // Copy over the word to the node
            strcpy(n->word, word);
            n->next = NULL;
    
            // Obtain hashing index
            int index = hash(word);
            if (table[index] == NULL)
            {
                table[index] = n;
            }
            else
            {
                n->next = table[index];
                table[index] = n;
            }
            total_words++;
        }
        fclose(file);
        return true;
    }
    
    // Returns number of words in dictionary if loaded, else 0 if not yet loaded
    unsigned int size(void)
    {
        // TODO
        return total_words;
    }
    
    // Unloads dictionary from memory, returning true if successful, else false
    bool unload(void)
    {
        // TODO
        // Free the linked list
        for (int i = 0; i < N; i++)
        {
            node *head = table[i];
            node *cursor = head;
            node *tmp = head;
    
            while (cursor != NULL)
            {
                cursor = cursor->next;
                free(tmp);
                tmp = cursor;
            }
        }
        return true;
    }

    Hope this will be helpful for you.

  • The solution to CS50 Psets 1 mario Problems (2022)

    The solution to CS50 Psets 1 mario Problems (2022)

    CS50 psets 1 Mario -less

    CS50 psets 1 Mario

    In this problem cs50 psets 1 Mario -less, we have to recreate the right-aligned pyramid that we saw at the end of World 1-1 in Nintendo’s Super Mario Brothers using C language. We have to do this using the (#) symbol.

    We have to write a program that allows the user to decide just how tall the pyramid should be by prompting them for a positive integer value for the height of the pyramid between 1 and 8.

    You may also like to read: The solution to CS50 Psets 2 Caesar Problem (2022)

    So the output should be like this,

    $ ./mario
    Height: 8
           #
          ##
         ###
        ####
       #####
      ######
     #######
    ########
    
    $ ./mario
    Height: 4
       #
      ##
     ###
    ####
    
    $ ./mario
    Height: 2
     #
    ##
    
    $ ./mario
    Height: 1
    #

    Also if the user does not input a positive integer value between 1 and 8 the program should re-prompt the user until they input the correct values.

    $ ./mario
    Height: -1
    Height: 0
    Height: 42
    Height: 50
    Height: 4
       #
      ##
     ###
    ####

    This is the solution that I created for this problem,

    #include <cs50.h>
    #include <stdio.h>
    
    int main(void)
    {
        int h;
    
        do
        {
            h = get_int("Height: ");
        }
        while (h < 1 || h > 8);
    
        for (int i = 1; i <= h; i++){
                for (int k = i; k < h; k++)
                {
                    printf(" ");
                }
                for (int j=1; j<=i; j++)
                {
                    printf("#");
                }
                printf("\n");
        }
    }

    Mario-more

    CS50 psets 1 Mario

    In this problem, we have to create the below structure using the c language. So the problem should prompt the user to get the height as in the above problem. and then output an adjacent pyramid using (#) symbols

    So here is my solution for this problem,

    #include <cs50.h>
    #include <stdio.h>
    
    int main(void)
    {
        int h;
    
        do {
            h = get_int("Height: ");
        }
        while (h < 1 || h > 8);
    
        for (int i = 1; i <= h; i++){
                for (int k = i; k < h; k++)
                {
                    printf(" ");
                }
                for (int j=1; j<=i; j++)
                {
                    printf("#");
                }
                printf("  ");
                for (int l=1; l<=i; l++)
                {
                    printf("#");
                }
                printf("\n");
        }
    }

    My website: dasunsucharith.me