2
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <math.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 };
char ALPHABET[] = { 'a' , 'b', 'c', 'd', 'e', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

int main(void) {
    string word1 = "dog";
    int score;
    int tile;
    int points;
    int index;
    int finish = strlen(word1);

    for (tile = 0, index = 0, score = 0; tile == finish; index++) {
        if (word1[tile] == ALPHABET[index]) {
            points = index;
            score = score + POINTS[points];
            tile++;
            index = -1;
        }
    }
    printf("%i\n", score);
}

This program prints a value of 0 instead of what I think should be 5 based on letter scores? Please give me some insight as to how I can accomplish this. Should I not be using an if statement inside a for loop?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
JLAc91
  • 35
  • 3
  • Hint: `word1[tile]`, which is an alphabetic character, has a corresponding ASCII code that is a number. 'a' = 61, for example, 'b'=62, and so on. So `word1[tile] - 'a'` gives an index that you can use to look up the tile value in `POINTS`. No need to iterate over ALPHABET. – Tony Jan 10 '21 at 08:24
  • Note that your program effectively has a `return 0;` at the end of `main()` — assuming you're using at least C99 (and only `main()` gets this treatment). You should be saying that it prints a value of 0 instead of 5. There is a lot of difference between the exit status of the program (which is the value returned to the shell by the program) and what it prints. – Jonathan Leffler Jan 13 '21 at 20:52

2 Answers2

3

Keep in mind that you are using zero based indexing.

Your for loop is using this:

tile == finish

It should be:

tile < finish

The length of “dog” is 3 so the index positions are 0 to 2.

This is at least part of your problems with the code.


As others have said, your loop needs a nested loop that iterates all of your alphabet letters. Once you find the match you can break out of that inner loop.


Also, you have:

'd', 2
'g', 4
'o', 3

Which totals 9. That is based on your code having two e characters.


The other issue is you reset index back to -1 so you are restarting at the beginning.

In summary:

  • Take on board the principle of zero based indexing for the bounds of your loop.
  • Use a nested for loop.
  • break out of the inner loop once you found the match.
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 1
    Thank you so much! I was always fighting with myself over which variable to increment because they both needed to increment. A nested loop was exactly the fix! The -1 was just me trying a last ditch effort before asking here. Thank you so much again. – JLAc91 Jan 10 '21 at 08:51
  • @JLAc91 Glad I was able to help you. Don't forget to "Accept" my answer with the "Tick" when you are ready to do so. – Andrew Truckle Jan 10 '21 at 09:08
0

The for loop test is incorrect: tile == finish is false at the first iteration of the loop, hence a score of 0.

Note that you should use two nested loops instead of hacking the loop index as coded:

#include <cs50.h>
#include <stdio.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 };
char ALPHABET[] = { 'a' , 'b', 'c', 'd', 'e', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

int main(void) {
    string word1 = "dog";
    int score;
    int tile;
    int index;
    int len = strlen(word1);

    score = 0;
    for (tile = 0; tile < len; tile++) {
        for (index = 0; index < 26; index++) {
            if (word1[tile] == ALPHABET[index]) {
                score = score + POINTS[index];
                break;
            }
        }
    }
    printf("%i\n", score);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189