sorting problem

This is a discussion on sorting problem within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to get my program to sort a list alphabetically and numerically. This is the list to ...

  1. #1
    Unregistered
    Guest

    sorting problem

    Hi,

    I am trying to get my program to sort a list alphabetically and numerically.

    This is the list to sort:
    trapezoid 45
    triangle 36
    circle 10
    square 67
    oval 56
    rectangle 25

    This is what I want to print out:
    (the list in alphab)
    circle 10
    oval 56
    rectangle 25
    square 67
    trapezoid 45
    triangle 36
    (the list numer.)
    circle 10
    rectangle 25
    triangle 36
    trapezoid 45
    oval 56
    square 67


    To do this, I stored the data in two arrays, one for the character strings and the other for the numbers. I need to convert the character strings to all caps first to be able to sort the list. I am able to compile my program, but then it does not print out the list out correctly. Is my logic wrong? Can anyone help me?

    Here are my functions that I wrote to call in my main program:
    Code:
    void uppercase(char *shape [25])
    {
    /*convert all to caps*/
    int toupper(char shape);
    }
          
    void sortalphabetically(char *shape [25], int numbers [], int size)
    {
        
    /*declaration of variables*/
    char temp[50];
    int pass, i;
    
    /*sort alphabetically*/
    for (pass=0; pass<size; pass++)
      for (i=pass+1; i<size; i++)
        {
        if (strcmp (shape[numbers[pass]], shape[numbers[i]]) >0 )
          /*swap*/
          {
          strcpy(temp, shape[numbers[pass]]);
          strcpy(shape[numbers[pass]], shape[numbers[i]]);
          strcpy(shape[numbers[i]], temp);
          }
       }
    }
    /*using selection sort method*/
    void sortnumerically(char *shape [25], int numbers [], int size)
    {
     
    /*declaration of variables*/
    int i, j, smallestnum, temporary;
     
    /*initialization*/
    for (i=0; i<size; i++)
      {
      smallestnum=i;
      for (j=i; j<size; j++)
        {
        if ( numbers[smallestnum] > numbers[j] ) 
          {
          smallestnum=j;  
        }
      /*swap*/
      temporary=numbers[i];
      numbers[i]=numbers[smallestnum];
      numbers[smallestnum]=temporary;
      }
    }
           
    /*print contents in the array*/
    void printarray(char *shape [25], int numbers [], int size)
    {      
    int i;
    
    for (i=0; i<size; i++)
      printf("The sorted list of shapes: %d\n", shape[numbers[i]]);
    }

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Can you post the main code?

    How did you define shapes and numbers?
    shape[numbers[pass]] ????

    You should declare a structure containing the number and the shape. This way you'll only have to swap the structures when sorting.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Try this
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define ASIZE   6
    
    typedef struct {
        char    *name;
        int      num;
    } db_st;
    
    int sort_by_name ( const void *a, const void *b ) {
        const db_st *pa = a;
        const db_st *pb = b;
        return strcmp( pa->name, pb->name );
    }
    int sort_by_number ( const void *a, const void *b ) {
        const db_st *pa = a;
        const db_st *pb = b;
        if ( pa->num < pb->num ) return -1;
        if ( pa->num > pb->num ) return +1;
        return 0;
    }
    
    void print_arr ( db_st  *arr ) {
        int i;
        for ( i = 0 ; i < ASIZE ; i++ ) {
            printf( "%s %d\n", arr[i].name, arr[i].num );
        }
        printf( "\n" );
    }
    
    int main ( ) {
        db_st data[] = {
            { "trapezoid", 45 },
            { "triangle", 36  },
            { "circle", 10  },
            { "square", 67  },
            { "oval", 56  },
            { "rectangle", 25  },
        };
        print_arr( data );
        qsort( data, ASIZE, sizeof(db_st), sort_by_name );
        print_arr( data );
        qsort( data, ASIZE, sizeof(db_st), sort_by_number );
        print_arr( data );
    
        return 0;
    }

  4. #4
    Unregistered
    Guest
    Thanks Salem, but we haven't learned about structures and I am just a beginner, so I don't quite understand all of your code.

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    what salem did is create a struct with both the name and the number in it, so they would be forever bonded. when one is sorted, the other stays with it. you can do the same thing without structs, but you need to keep the number and string together by moving both in the same swap.

  6. #6
    Unregistered
    Guest
    Thanks for the explanation ygfperson. But in my assignment we have to separate them into two arrays. In my main code I call a function that reads in the data and then separates them into a character array and an array to hold the numbers. This read file then returns the number of shapes/number pairs that were successfully read from the file. I know that the readfile works, it is just the sorting and making the character string in caps that don't work. That is why I have an array in an array, because I don't know how to keep the shape and number together. Is there a way to do that without using structures?

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    45
    When you set your arrays up, make sure the position of the number and the type of shape are the same. eg:

    Code:
      int shapenum[6];
                char *shapetype[6];
    
    shapetype[0] = "circle";
    shapenum[0] = 10; 
    //etc. etc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting problem?
    By audinue in forum C Programming
    Replies: 5
    Last Post: 01-06-2009, 01:16 PM
  2. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  3. What is problem about the sorting?
    By Mathsniper in forum C Programming
    Replies: 2
    Last Post: 04-17-2005, 07:00 AM
  4. Sorting text file problem...
    By John-m in forum C Programming
    Replies: 3
    Last Post: 10-01-2002, 04:51 PM
  5. Sorting array output problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 12:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21