The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

What's wrong with my code?

DtotheGDtotheG Registered User regular
edited December 2007 in Help / Advice Forum
What's wrong with my code?

I'm supposed to list the 5 names in alphabetical order. However, whenever it's printed, I get a garble of crap.

<code>
#include "stdio.h"
#include "math.h"
#include "string.h"

void main()
{
char name[5][10]= {"Lemmy", "Gabe", "Tycho", "Paul", "Sylvia"};
int x, y;
char temp[10]; /*Dummy to switch names*/
for (x=0; x<5; x++)
{
for (y=0; y<10; y++)
{
if(strcmp(name[x],name[x+1])>0)
{
strcpy(name[x],temp);
strcpy(name[x],name[x+1]);
strcpy(name[x+1],temp);
}
else
{
}

}
}
for (x=0;x<5;x++)
{
printf("%s", name[x]);
}
}
</code>

DtotheG on

Posts

  • urahonkyurahonky Cynical Old Man Registered User regular
    edited December 2007
    strcmp compares two strings, if I'm not mistaken... Right?

    So saying strcmp(name[x],name[x+1]) is just going to test to see if each of the names are the same or different. If they're the same it will be 1(true), if they are different you're going to get a 0 (false). Since none of the names are the same the output for the if statement is going to stay at 0, and completely skips it.

    What you want to do is test each letter in the character string (that's how I would do it, but others may have a better suggestion).

    Test the first letter in the first name against the first letter in the second name.

    So: name[0][0] would = "L" and name[1][0] = "G", and test which is greater or less. Then switch when necessary. Hope this makes sense. Someone else will probably make a better post, but hopefully I got you thinkin.

    urahonky on
  • SmasherSmasher Starting to get dizzy Registered User regular
    edited December 2007
    You have name[x] and temp switched in the first strcpy in the inner loop. You were copying from temp into name[x], and since temp is uninitialized you naturally get garbage.

    Also, your loop conditions are a little off (although technically they work in this case). You're not iterating through the letters of each word, so there's no reason for y to go up to 10. What you want to have happen is have x go from index 0 to #strings - 2, and inside that loop have y go from 0 to #strings - 1.

    Smasher on
  • SmasherSmasher Starting to get dizzy Registered User regular
    edited December 2007
    urahonky wrote: »
    strcmp compares two strings, if I'm not mistaken... Right?

    So saying strcmp(name[x],name[x+1]) is just going to test to see if each of the names are the same or different. If they're the same it will be 1(true), if they are different you're going to get a 0 (false). Since none of the names are the same the output for the if statement is going to stay at 0, and completely skips it.

    What you want to do is test each letter in the character string (that's how I would do it, but others may have a better suggestion).

    Test the first letter in the first name against the first letter in the second name.

    So: name[0][0] would = "L" and name[1][0] = "G", and test which is greater or less. Then switch when necessary. Hope this makes sense. Someone else will probably make a better post, but hopefully I got you thinkin.

    No, that's not how strcmp works. It returns 0 if the strings are equal, a number greater than 0 if str1 is lexigraphically greater than str2, and a number less than 0 if str2 is lexigraphically greater than str1.

    There's another problem I missed before; In addition to the changes I made before, you either need to make y your outer loop and x your inner loop, or replace all the occurances of x in the inner loop with y.

    Smasher on
  • ASimPersonASimPerson Cold... ... and hard.Registered User regular
    edited December 2007
    Also, if you're using a static initializer list for the that array of strings, you could just do:
    char *names[] = {"Lemmy", "Gabe", "Tycho", "Paul", "Sylvia"};
    

    ASimPerson on
  • ecco the dolphinecco the dolphin Registered User regular
    edited December 2007
    ASimPerson wrote: »
    Also, if you're using a static initializer list for the that array of strings, you could just do:
    char *names[] = {"Lemmy", "Gabe", "Tycho", "Paul", "Sylvia"};
    

    Not recommended in this application, as the OP expects his char * to point to non-const char[10] arrays (I assume this based on the strcpy/temp variable). I believe char *names[] will give you an array of pointers to variable length const strings.

    ecco the dolphin on
    Penny Arcade Developers at PADev.net.
Sign In or Register to comment.