Click here to Skip to main content
15,915,742 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
My code is working, but it is extremely inefficient. For simple input "A!b" it takes about 2 minutes to get output. I know that I have do_while inside of for loops with a lot of steps, but to be honest, it took me forever to get even there, and I would highly appreciate any suggestions.
C++
#include <ctype.h> #include <cs50.h> #include <stdio.h> #include <string.h>

int main(void) { //take user`s input string word = get_string("Player: ");

// change to uppercase all letters // int letters_to_caps(void)

for(int i=0; i<strlen(word);i++)
{
    if(word[i]>='a'&&word[i]<='z')
     word[i]=toupper(word[i]);
}
// how many point to give for each letter 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};

// calculate score for each word for player // int calculate_score_for_word(void)

int wordScore=0;
for(int i=0;i<strlen(word);i++)
{
    int n=65;
    do 
    {
        if(word[i]>='A' && word[i]<='Z')
        {
            n++;
            if (word[i]==n)
            wordScore+=POINTS[i];
        }
        else
        word[i]=n;
    }
    while(word[i]!=n);
    
}
printf("%i\n",wordScore);
}


What I have tried:

That is my best result so far.
Posted
Updated 7-Jul-21 0:04am
v2
Comments
jeron1 6-Jul-21 13:54pm    
Can you step through the second for loop with your example input, the first character is 'A' (65 or 0x41) your line

if (word[i] == n)

what is the value of n the first time this line gets executed?
Greg Utas 6-Jul-21 14:14pm    
What is the purpose of this n? I can't see one! It looks like a leftover from comparing characters by their integral values rather than by using character literals. What happens if you take all references to n out? To me it is a miracle that this code works at all.
Igor Shevelev 6-Jul-21 14:43pm    
Thank you @Greg Utas, really appreciate your help!!! Code is running instantly now. I will look through it and try to understand how it works.
P.S. could you explain to me what you mean here "What happens if you take all references to n out?'
P.S.S. also I am a bit confused by your edit with a range of 0.25.
Thank again for your code, I understand my mistake now! The solution is so simple and elegant when I look at it now.
Greg Utas 6-Jul-21 15:11pm    
I couldn't understand what n was doing, so I meant remove it from the code completely. When editing the code, I realized it was doing something, but I'm still not sure I understand what, or even want to. :)

When I first posted the code, it just had wordScore += POINTS[word[i]]. The problem with that is that it indexes POINTS with 'A' to 'Z', which are 65 to 90 in ASCII. This will cause the program to crash (or produce the wrong answer) because valid indices for POINTS are 0 to 25. It is therefore necessary to subtract 'A' from word[i] when indexing POINTS.

1 solution

Try this. I took n out and changed the line containing wordScore+=.

EDIT: But forgot to bring word[i] into the range 0..25 instead of 'A'..'Z'. :)
C
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 calc_score(const char* word)
{
   for(int i = 0; i < strlen(word); i++)
   {
      if(word[i] >= 'a' && word[i] <= 'z')
      { 
         word[i] = toupper(word[i]);
      }
   }

   int wordScore = 0;

   for(int i = 0; i < strlen(word); i++)
   {
      if(word[i] >= 'A' && word[i] <= 'Z')
      {
         wordScore += POINTS[word[i] - 'A'];
      }
   }

   return wordScore;
}
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900