{"id":169,"date":"2024-02-26T00:50:05","date_gmt":"2024-02-25T16:50:05","guid":{"rendered":"http:\/\/tobykskgd.life\/?p=169"},"modified":"2024-02-26T00:50:07","modified_gmt":"2024-02-25T16:50:07","slug":"07","status":"publish","type":"post","link":"https:\/\/tobykskgd.life\/index.php\/07\/","title":{"rendered":"[CS50\u4f5c\u4e1a2023]Week2 &#8211; Arrays"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/tobykskgd.life\/wp-content\/uploads\/2024\/02\/\u5c4f\u5e55\u622a\u56fe-2024-02-26-003148-1.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"974\" height=\"532\" data-original=\"https:\/\/tobykskgd.life\/wp-content\/uploads\/2024\/02\/\u5c4f\u5e55\u622a\u56fe-2024-02-26-003148-1.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-171\"  sizes=\"auto, (max-width: 974px) 100vw, 974px\" \/><\/div><\/figure>\n\n\n\n<p><a href=\"https:\/\/cs50.harvard.edu\/x\/2023\/problems\/2\/#week-2-practice-problems\">Week 2 Practice Problems<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/cs50.harvard.edu\/x\/2023\/problems\/2\/hours\/\">Hours<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;cs50.h>\r\n#include &lt;ctype.h>\r\n#include &lt;stdio.h>\r\n\r\nfloat calc_hours(int hours&#91;], int weeks, char output);\r\n\r\nint main(void)\r\n{\r\n    int weeks = get_int(\"Number of weeks taking CS50: \");\r\n    int hours&#91;weeks];\r\n\r\n    for (int i = 0; i &lt; weeks; i++)\r\n    {\r\n        hours&#91;i] = get_int(\"Week %i HW Hours: \", i);\r\n    }\r\n\r\n    char output;\r\n    do\r\n    {\r\n        output = toupper(get_char(\"Enter T for total hours, A for average hours per week: \"));\r\n    }\r\n    while (output != 'T' &amp;&amp; output != 'A');\r\n\r\n    printf(\"%.1f hours\\n\", calc_hours(hours, weeks, output));\r\n}\r\n\r\n\/\/ TODO: complete the calc_hours function\r\nfloat calc_hours(int hours&#91;], int weeks, char output)\r\n{\r\n    float num = 0;\r\n    if (output == 'T')\r\n    {\r\n        for (int i = 0; i &lt; weeks; i++)\r\n        {\r\n            num += hours&#91;i];\r\n        }\r\n    }\r\n\r\n    if (output == 'A')\r\n    {\r\n        for (int j = 0; j &lt; weeks; j++)\r\n        {\r\n            num += hours&#91;j];\r\n        }\r\n        num = num \/ weeks;\r\n    }\r\n    return num;\r\n}<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/cs50.harvard.edu\/x\/2023\/problems\/2\/no-vowels\/#n0-v0w3ls\">N0 V0w3ls<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Write a function to replace vowels with numbers\r\n\/\/ Get practice with strings\r\n\/\/ Get practice with command line\r\n\/\/ Get practice with switch\r\n\r\n#include &lt;cs50.h>\r\n#include &lt;stdio.h>\r\n#include &lt;string.h>\r\n\r\nchar replace(char c);\r\n\r\nint main(int argc, string argv&#91;])\r\n{\r\n    if (argc != 2)\r\n    {\r\n        printf(\"Usage: .\/no-vowels word\\n\");\r\n        return 1;\r\n    }\r\n\r\n    int i = 0;\r\n    do\r\n    {\r\n        printf(\"%c\", replace(argv&#91;1]&#91;i]));\r\n        i++;\r\n    }\r\n    while (i &lt; strlen(argv&#91;1]));\r\n    printf(\"\\n\");\r\n    return 0;\r\n}\r\n\r\nchar replace(char c)\r\n{\r\n    switch (c)\r\n    {\r\n        case 'a':\r\n            return '6';\r\n            break;\r\n        case 'e':\r\n            return '3';\r\n            break;\r\n        case 'i':\r\n            return '1';\r\n            break;\r\n        case 'o':\r\n            return '0';\r\n            break;\r\n        default:\r\n            return c;\r\n            break;\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/cs50.harvard.edu\/x\/2023\/problems\/2\/password\/#password\">Password<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Check that a password has at least one lowercase letter, uppercase letter, number and symbol\r\n\/\/ Practice iterating through a string\r\n\/\/ Practice using the ctype library\r\n\r\n#include &lt;cs50.h>\r\n#include &lt;ctype.h>\r\n#include &lt;stdio.h>\r\n#include &lt;string.h>\r\n\r\nbool valid(string password);\r\n\r\nint main(void)\r\n{\r\n    string password = get_string(\"Enter your password: \");\r\n    if (valid(password))\r\n    {\r\n        printf(\"Your password is valid!\\n\");\r\n    }\r\n    else\r\n    {\r\n        printf(\"Your password needs at least one uppercase letter, lowercase letter, number and symbol\\n\");\r\n    }\r\n}\r\n\r\n\/\/ TODO: Complete the Boolean function below\r\nbool valid(string password)\r\n{\r\n    bool upper = false, lower = false, number = false, symbol = false;\r\n    for (int i = 0, n = strlen(password); i &lt; n; i++)\r\n    {\r\n        if (isupper(password&#91;i]))\r\n        {\r\n            upper = true;\r\n        }\r\n        else if (islower(password&#91;i]))\r\n        {\r\n            lower = true;\r\n        }\r\n        else if (isdigit(password&#91;i]))\r\n        {\r\n            number = true;\r\n        }\r\n        else if (ispunct(password&#91;i]))\r\n        {\r\n            symbol = true;\r\n        }\r\n    }\r\n    if (upper == true &amp;&amp; lower == true &amp;&amp; number == true &amp;&amp; symbol == true)\r\n    {\r\n        return true;\r\n    }\r\n    else\r\n    {\r\n        return false;\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/cs50.harvard.edu\/x\/2023\/labs\/2\/#lab-2-scrabble\">Lab 2: Scrabble<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;cs50.h>\r\n#include &lt;ctype.h>\r\n#include &lt;stdio.h>\r\n#include &lt;string.h>\r\n\r\n\/\/ Points assigned to each letter of the alphabet\r\nint POINTS&#91;] = {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};\r\n\r\nint compute_score(string word);\r\n\r\nint main(void)\r\n{\r\n    \/\/ Get input words from both players\r\n    string word1 = get_string(\"Player 1: \");\r\n    string word2 = get_string(\"Player 2: \");\r\n\r\n    \/\/ Score both words\r\n    int score1 = compute_score(word1);\r\n    int score2 = compute_score(word2);\r\n\r\n    \/\/ TODO: Print the winner\r\n    if (score1 > score2)\r\n    {\r\n        printf(\"Player 1 wins!\");\r\n    }\r\n    else if (score1 &lt; score2)\r\n    {\r\n        printf(\"Player 2 wins!\");\r\n    }\r\n    else\r\n    {\r\n        printf(\"Tie!\");\r\n    }\r\n}\r\n\r\nint compute_score(string word)\r\n{\r\n    \/\/ TODO: Compute and return score for string\r\n    int score = 0;\r\n    for (int i = 0; i &lt; strlen(word); i++)\r\n    {\r\n        if (islower(word&#91;i]))\r\n        {\r\n            score += POINTS&#91;word&#91;i] - 97];\r\n        }\r\n        else if (isupper(word&#91;i]))\r\n        {\r\n            score += POINTS&#91;word&#91;i] - 65];\r\n        }\r\n    }\r\n    return score;\r\n}<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/cs50.harvard.edu\/x\/2023\/psets\/2\/#problem-set-2\">Problem Set 2<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/cs50.harvard.edu\/x\/2023\/psets\/2\/readability\/\">Readability<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;cs50.h>\r\n#include &lt;ctype.h>\r\n#include &lt;math.h>\r\n#include &lt;stdio.h>\r\n#include &lt;string.h>\r\n\r\nint count_letters(string text);\r\nint count_words(string text);\r\nint count_sentences(string text);\r\n\r\nint main(void)\r\n{\r\n    string text = get_string(\"Text: \");\r\n    int letters = count_letters(text);\r\n    int words = count_words(text);\r\n    int sentences = count_sentences(text);\r\n    float L = (float) letters \/ words * 100;\r\n    float S = (float) sentences \/ words * 100;\r\n    int index = round(0.0588 * L - 0.296 * S - 15.8);\r\n    if (index &lt; 1)\r\n    {\r\n        printf(\"Before Grade 1\\n\");\r\n    }\r\n    else if (index >= 1 &amp;&amp; index &lt; 16)\r\n    {\r\n        printf(\"Grade %i\\n\", index);\r\n    }\r\n    else if (index >= 16)\r\n    {\r\n        printf(\"Grade 16+\\n\");\r\n    }\r\n}\r\n\r\nint count_letters(string text)\r\n{\r\n    int all = strlen(text);\r\n    int letters = 0;\r\n    for (int i = 0; i &lt; all; i++)\r\n    {\r\n        if (isalpha(text&#91;i]))\r\n        {\r\n            letters += 1;\r\n        }\r\n    }\r\n    return letters;\r\n}\r\n\r\nint count_words(string text)\r\n{\r\n    int all = strlen(text), words = 1;\r\n    for (int i = 0; i &lt; all; i++)\r\n    {\r\n        if (isspace(text&#91;i]))\r\n        {\r\n            words += 1;\r\n        }\r\n    }\r\n    return words;\r\n}\r\n\r\nint count_sentences(string text)\r\n{\r\n    int all = strlen(text), sentences = 0;\r\n    for (int i = 0; i &lt; all; i++)\r\n    {\r\n        if (text&#91;i] == '.' || text&#91;i] == '!' || text&#91;i] == '?')\r\n        {\r\n            sentences += 1;\r\n        }\r\n    }\r\n    return sentences;\r\n}<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/cs50.harvard.edu\/x\/2023\/psets\/2\/wordle50\/\">Wordle50<\/a>, if feeling more comfortable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;cs50.h>\r\n#include &lt;ctype.h>\r\n#include &lt;stdio.h>\r\n#include &lt;stdlib.h>\r\n#include &lt;string.h>\r\n#include &lt;time.h>\r\n\r\n\/\/ each of our text files contains 1000 words\r\n#define LISTSIZE 1000\r\n\r\n\/\/ values for colors and score (EXACT == right letter, right place; CLOSE == right letter, wrong place; WRONG == wrong letter)\r\n#define EXACT 2\r\n#define CLOSE 1\r\n#define WRONG 0\r\n\r\n\/\/ ANSI color codes for boxed in letters\r\n#define GREEN \"\\e&#91;38;2;255;255;255;1m\\e&#91;48;2;106;170;100;1m\"\r\n#define YELLOW \"\\e&#91;38;2;255;255;255;1m\\e&#91;48;2;201;180;88;1m\"\r\n#define RED \"\\e&#91;38;2;255;255;255;1m\\e&#91;48;2;220;20;60;1m\"\r\n#define RESET \"\\e&#91;0;39m\"\r\n\r\n\/\/ user-defined function prototypes\r\nstring get_guess(int wordsize);\r\nint check_word(string guess, int wordsize, int status&#91;], string choice);\r\nvoid print_word(string guess, int wordsize, int status&#91;]);\r\n\r\nint main(int argc, string argv&#91;])\r\n{\r\n    \/\/ ensure proper usage\r\n    \/\/ TODO #1\r\n    int wordsize = 0;\r\n\r\n    if (argc != 2)\r\n    {\r\n        printf(\"Usage: .\/wordle wordsize\\n\");\r\n        return 1;\r\n    }\r\n\r\n    \/\/ ensure argv&#91;1] is either 5, 6, 7, or 8 and store that value in wordsize instead\r\n    \/\/ TODO #2\r\n    else if (argv&#91;1]&#91;0] == '5' || argv&#91;1]&#91;0] == '6' || argv&#91;1]&#91;0] == '7' || argv&#91;1]&#91;0] == '8')\r\n    {\r\n        wordsize = (int) argv&#91;1]&#91;0] - '0';\r\n    }\r\n    else\r\n    {\r\n        printf(\"Error: wordsize must be either 5, 6, 7, or 8\\n\");\r\n        return 1;\r\n    }\r\n\r\n    \/\/ open correct file, each file has exactly LISTSIZE words\r\n    char wl_filename&#91;6];\r\n    sprintf(wl_filename, \"%i.txt\", wordsize);\r\n    FILE *wordlist = fopen(wl_filename, \"r\");\r\n    if (wordlist == NULL)\r\n    {\r\n        printf(\"Error opening file %s.\\n\", wl_filename);\r\n        return 1;\r\n    }\r\n\r\n    \/\/ load word file into an array of size LISTSIZE\r\n    char options&#91;LISTSIZE]&#91;wordsize + 1];\r\n\r\n    for (int i = 0; i &lt; LISTSIZE; i++)\r\n    {\r\n        fscanf(wordlist, \"%s\", options&#91;i]);\r\n    }\r\n\r\n    \/\/ pseudorandomly select a word for this game\r\n    srand(time(NULL));\r\n    string choice = options&#91;rand() % LISTSIZE];\r\n\r\n    \/\/ allow one more guess than the length of the word\r\n    int guesses = wordsize + 1;\r\n    bool won = false;\r\n\r\n    \/\/ print greeting, using ANSI color codes to demonstrate\r\n    printf(GREEN \"This is WORDLE50\" RESET \"\\n\");\r\n    printf(\"You have %i tries to guess the %i-letter word I'm thinking of\\n\", guesses, wordsize);\r\n\r\n    \/\/ main game loop, one iteration for each guess\r\n    for (int i = 0; i &lt; guesses; i++)\r\n    {\r\n        \/\/ obtain user's guess\r\n        string guess = get_guess(wordsize);\r\n\r\n        \/\/ array to hold guess status, initially set to zero\r\n        int status&#91;wordsize];\r\n\r\n        \/\/ set all elements of status array initially to 0, aka WRONG\r\n        \/\/ TODO #4\r\n        for (int j = 0; j &lt; wordsize; j++)\r\n        {\r\n            status&#91;j] = WRONG;\r\n        }\r\n\r\n        \/\/ Calculate score for the guess\r\n        int score = check_word(guess, wordsize, status, choice);\r\n\r\n        printf(\"Guess %i: \", i + 1);\r\n\r\n        \/\/ Print the guess\r\n        print_word(guess, wordsize, status);\r\n\r\n        \/\/ if they guessed it exactly right, set terminate loop\r\n        if (score == EXACT * wordsize)\r\n        {\r\n            won = true;\r\n            break;\r\n        }\r\n    }\r\n\r\n    \/\/ Print the game's result\r\n    \/\/ TODO #7\r\n    if (won)\r\n    {\r\n        printf(\"You won!\\n\");\r\n    }\r\n    else\r\n    {\r\n        printf(\"%s\\n\", choice);\r\n    }\r\n\r\n    \/\/ that's all folks!\r\n    return 0;\r\n}\r\n\r\nstring get_guess(int wordsize)\r\n{\r\n    string guess = \"\";\r\n    int number = 0;\r\n\r\n    \/\/ ensure users actually provide a guess that is the correct length\r\n    \/\/ TODO #3\r\n    do\r\n    {\r\n        guess = get_string(\"Input a %i-letter word: \", wordsize);\r\n    }\r\n    while (strlen(guess) != wordsize);\r\n    for (int i = 0; i &lt; strlen(guess); i++)\r\n    {\r\n        if (isalnum(guess&#91;i]) == 0)\r\n        {\r\n            get_guess(wordsize);\r\n            break;\r\n        }\r\n        else if (isdigit(guess&#91;i]))\r\n        {\r\n            get_guess(wordsize);\r\n            break;\r\n        }\r\n        else if (isupper(guess&#91;i]))\r\n        {\r\n            guess&#91;i] = tolower(guess&#91;i]);\r\n        }\r\n    }\r\n    return guess;\r\n}\r\n\r\nint check_word(string guess, int wordsize, int status&#91;], string choice)\r\n{\r\n    int score = 0;\r\n\r\n    \/\/ compare guess to choice and score points as appropriate, storing points in status\r\n    \/\/ TODO #5\r\n\r\n    \/\/ HINTS\r\n    \/\/ iterate over each letter of the guess\r\n    \/\/ iterate over each letter of the choice\r\n    \/\/ compare the current guess letter to the current choice letter\r\n    \/\/ if they're the same position in the word, score EXACT points (green) and break so you don't compare that letter further\r\n    \/\/ if it's in the word, but not the right spot, score CLOSE point (yellow)\r\n    \/\/ keep track of the total score by adding each individual letter's score from above\r\n    for (int i = 0; i &lt; wordsize; i++)\r\n    {\r\n        char gu = guess&#91;i];\r\n        if (guess&#91;i] == choice&#91;i])\r\n        {\r\n            status&#91;i] = EXACT;\r\n        }\r\n        else\r\n        {\r\n            for (int j = 0; j &lt; wordsize; j++)\r\n            {\r\n                if (status&#91;j] != EXACT)\r\n                {\r\n                    if (gu == choice&#91;j])\r\n                    {\r\n                        status&#91;i] = CLOSE;\r\n                    }\r\n                }\r\n            }\r\n        }\r\n    }\r\n    for (int m = 0; m &lt; wordsize; m++)\r\n    {\r\n        score += status&#91;m];\r\n    }\r\n    return score;\r\n}\r\n\r\nvoid print_word(string guess, int wordsize, int status&#91;])\r\n{\r\n    \/\/ print word character-for-character with correct color coding, then reset terminal font to normal\r\n    \/\/ TODO #6\r\n    for (int i = 0; i &lt; wordsize; i++)\r\n    {\r\n        if (status&#91;i] == EXACT)\r\n        {\r\n            printf(GREEN \"%c\" RESET, guess&#91;i]);\r\n        }\r\n        else if (status&#91;i] == CLOSE)\r\n        {\r\n            printf(YELLOW \"%c\" RESET, guess&#91;i]);\r\n        }\r\n        else\r\n        {\r\n            printf(RED \"%c\" RESET, guess&#91;i]);\r\n        }\r\n    }\r\n\r\n    printf(\"\\n\");\r\n    return;\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Week 2 Practice Problems Hours: N0 V0w3ls: Password: La [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[14,15],"class_list":["post-169","post","type-post","status-publish","format-standard","hentry","category-cs50","tag-cs50","tag-homework"],"_links":{"self":[{"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/posts\/169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/comments?post=169"}],"version-history":[{"count":1,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/posts\/169\/revisions"}],"predecessor-version":[{"id":172,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/posts\/169\/revisions\/172"}],"wp:attachment":[{"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/media?parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/categories?post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/tags?post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}