CS50x threads to aide as a supplementary resource › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 2: [Arrays] – Functions, Variable and Scope, Debugging, Arrays, and Command Line Arguments › Scrabble lab problem: Connecting ASCII values of uppercase characters with POINTS[]
- This topic is empty.
-
AuthorPosts
-
September 18, 2022 at 6:26 am #592
#include <stdio.h> #include <cs50.h> #include <ctype.h> #include <string.h> // Points assigned to each letter of the alphabet int POINTS[] = {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 compute_score(string word); int main(void) { // Get input words from both players string word1 = get_string("Player 1: "); string word2 = get_string("Player 2: "); // Score both words int score1 = compute_score(word1); int score2 = compute_score(word2); printf("%s\n",word1); printf("%s",word2); // TODO: Print the winner } int compute_score(string word) { // TODO: Compute and return score for string int x = strlen(word); int y = 0; for (y = 0; y < x; y++) { word[y] = toupper(word[y]); } int counter = 0; char t; for (counter = 0; counter < x; counter++) { t = word[0]; printf("%i",t); } //word[0] - 65 = POINTS[] return t; }
With the above code, entered smallcase/uppercase characters are converted to uppercase. Also, able to extract ASCII values of entered characters.
Now, the task is to deduct 65 from each ASCII values of entered character by user and to find some kind of association with POINTS[].
Here is my tentative code which is functionally/syntactically perhaps incorrect:
word[0] – 65 = POINTS[] //0 denotes the first character of word entered by user.
What I intend to do is to deduct 65 from each uppercase character so that the same matches with the index value of POINTS[]. Once that done, value of each index will be used to count the score of entered word by user.
Tips on how to proceed will be helpful.
REVISED CODE:
#include <stdio.h> #include <cs50.h> #include <ctype.h> #include <string.h> // Points assigned to each letter of the alphabet int POINTS[] = {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 compute_score(string word); int main(void) { // Get input words from both players string word1 = get_string("Player 1: "); string word2 = get_string("Player 2: "); // Score both words int score1 = compute_score(word1); int score2 = compute_score(word2); printf("%s\n",word1); printf("%s",word2); // TODO: Print the winner } int compute_score(string word) { // TODO: Compute and return score for string int x = strlen(word); int y = 0; int score = 0; for (y = 0; y < x; y++) { word[y] = toupper(word[y]) - 65; score = score + POINTS[word[y]]; } return x; }
So the problem is in line 36. Help appreciated how to correct.
Thanks in advance!
Reply
https://edstem.org/us/courses/176/discussion/1783399[learn_press_profile]
-
AuthorPosts
- You must be logged in to reply to this topic.