daniel_lawson9999

Daniel Lawson Pokemon (name printer)

Sep 25th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <string.h>
  4. #include <math.h>
  5. //Daniel Lawson
  6. int main(void){
  7.     string pokemon[5]; //initialize a an array
  8.     int longest; //keep track of the longest size of a string
  9.     for(int i= 0; i<5;i++){//ask for a pokemon and store it 5 times
  10.         printf("Give me a pokemon: "); //ask for a pokemon
  11.         pokemon[i] = get_string(); //store the pokemon
  12.         longest = (int)fmax((double)strlen(pokemon[i]),(double)longest);//set longest to the size of the longest string (further explanation of this semi-confusing line bellow)
  13.     }
  14.     for(int i=0;i<5;i++){//look through the array
  15.         if(strlen(pokemon[i])==longest){//if the name of the pokemon is one of the longest
  16.             printf("%s,",pokemon[i]);//print it
  17.         }
  18.     }
  19.     printf(" I choose you!\n");
  20.  
  21. }
  22.  
  23. /* explanation of line 12
  24. longest = (int)fmax((double)strlen(pokemon[i]),(double)longest);
  25.  
  26. at each iteration of the loop it simply sets longest equal to the maximum of longest, and the maximum size of the current string
  27. so the logic is just longest = max(length of current string,longest)
  28. but... obviously there's more
  29. in the math.h, the only function for max is fmax which accepts two doubles or floats and returns a floating point number so...
  30. we need to type cast each of our integers with (double)
  31. we also need to type cast what the function returns (int)
  32. */
  33. /* output example:
  34. Give me a Pokemon: Butterfree
  35. Give me a Pokemon: Clefairy
  36. Give me a Pokemon: Diglett
  37. Give me a Pokemon: Growlithe
  38. Give me a Pokemon: Rapidash
  39. Butterfree, I choose you!
  40.  
  41. other example:
  42. Give me a Pokemon: Butterfree
  43. Give me a Pokemon: Putterfree
  44. Give me a Pokemon: Diglett
  45. Give me a Pokemon: Growlithe
  46. Give me a Pokemon: Rapidash
  47. Butterfree,Putterfree I choose you!
  48. */
Advertisement
Add Comment
Please, Sign In to add comment