Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cs50.h>
- #include <string.h>
- #include <math.h>
- //Daniel Lawson
- int main(void){
- string pokemon[5]; //initialize a an array
- int longest; //keep track of the longest size of a string
- for(int i= 0; i<5;i++){//ask for a pokemon and store it 5 times
- printf("Give me a pokemon: "); //ask for a pokemon
- pokemon[i] = get_string(); //store the pokemon
- 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)
- }
- for(int i=0;i<5;i++){//look through the array
- if(strlen(pokemon[i])==longest){//if the name of the pokemon is one of the longest
- printf("%s,",pokemon[i]);//print it
- }
- }
- printf(" I choose you!\n");
- }
- /* explanation of line 12
- longest = (int)fmax((double)strlen(pokemon[i]),(double)longest);
- at each iteration of the loop it simply sets longest equal to the maximum of longest, and the maximum size of the current string
- so the logic is just longest = max(length of current string,longest)
- but... obviously there's more
- in the math.h, the only function for max is fmax which accepts two doubles or floats and returns a floating point number so...
- we need to type cast each of our integers with (double)
- we also need to type cast what the function returns (int)
- */
- /* output example:
- Give me a Pokemon: Butterfree
- Give me a Pokemon: Clefairy
- Give me a Pokemon: Diglett
- Give me a Pokemon: Growlithe
- Give me a Pokemon: Rapidash
- Butterfree, I choose you!
- other example:
- Give me a Pokemon: Butterfree
- Give me a Pokemon: Putterfree
- Give me a Pokemon: Diglett
- Give me a Pokemon: Growlithe
- Give me a Pokemon: Rapidash
- Butterfree,Putterfree I choose you!
- */
Advertisement
Add Comment
Please, Sign In to add comment