Thread: Sorting strings into alphabetical order

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    Question Sorting strings into alphabetical order

    Hello,
    I am new here just thought I could get some help I have wrote this program and it all works fine but I get this crap in runtime:

    "
    In alphabetical order, the names are:
    `� N�
    "

    like I said it works and prints them all in alphabetical but if someone can explain why this ........ is printing in my program would be great.

    Here is my code:

    insert
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define NUM 11      /* number in the list */
    main()
    {
      int i, sorted, iter=0;
      long swaps = 0;
      char names[NUM][80];
      char temp[80];
    
      printf("enter (%i-1) names:\n", NUM);
      
      for (i = 0; i < 10; i++)
        scanf("%s", &names[i][80]);
      
      /* Bubble sort: */
      puts("in order, the names are:");
    
      do {
          sorted = 1;
           for (i=1; i<10; i++)
           {
             if (strcmp(names[i-1], names[i]) > 0)
             {
               sorted = 0;
               strcpy(temp, names[i-1]);
               strcpy(names[i-1], names[i]);
               strcpy(names[i], temp);
               swaps++;
             }
           }
           for (i=9; i>0; i--)
           {
             if (strcmp(names[i-1], names[i]) > 0)
             {
               sorted = 0;
               strcpy(temp, names[i-1]);
               strcpy(names[i-1], names[i]);
               strcpy(names[i], temp);
               swaps++;
             }
           }
           iter++;
         } while (!sorted);
      
      /* Output sorted list: */
      
      puts("\nIn alphabetical order, the names are:");     
      for (i = 0; i < NUM; i++)
        printf("%s\n", names[i]);
      printf("\n%li swaps were performed.\n", swaps);
      printf("\n%i iterations were performed.\n", iter);     
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You've defined NUM as 11 ... when inputting you are only taking 10 strings but when printing you print NUM (11) strings... The last string will be filled with random garbage.

    If you're going to define a number of elements... you should use it everywhere.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    There are a few problems:
    1. NUM should be 10 since you only want 10 names. No magic numbers like 9, 10 or 11. Use NUM-1, NUM or NUM+1.
    2. Your scanf is wrong. &names[i][80] is probably the next slot after the one you want. You should be using &names[i][0], or more simply, names[i].
    3. names only needs 10 elements, so when you make NUM 10, don't declare names[NUM+1][80].

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    2
    Thank you code resolved used gets instead of scanf a bit dangerous but should work also got rid of the [80] and changed NUM to 10

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Use fgets with stdin instead. It's an easy conversion, the right thing to do, and a good habit to form.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I've never seen a bubble sort like that... going through the entire array from beginning to end and then end-to-beginning. You need to review how bubble sort works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Array of Strings
    By nair in forum C Programming
    Replies: 6
    Last Post: 10-09-2010, 11:10 AM
  2. names in alphabetical order?
    By n3cr0_l0rd in forum C Programming
    Replies: 21
    Last Post: 02-06-2009, 08:59 PM
  3. Replies: 13
    Last Post: 11-14-2008, 03:52 PM
  4. Sorting numbers in descending order
    By Yunasnk in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2003, 05:55 PM
  5. Alphabetical order
    By BubbleMan in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2001, 03:38 PM

Tags for this Thread