Thread: Template Sorting Problem

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    20

    Template Sorting Problem

    I've looked over my code several times and I'm unable to find out what I did wrong. It looks ok to me. Its suppose to set all of the elements of the array to random numbers under 100 and then sort those numbers. Instead of outputting numbers under 100, it outputs weird numbers. See for yourself.

  2. #2
    Registered User quagsire's Avatar
    Join Date
    Jun 2002
    Posts
    60
    When using
    Code:
    MAX= sizeof(array) / sizeof(array[0]);
    MAX is assigned a value of 1. This is because sizeof(array) returns the size of the pointer to the array (4 bytes) and sizeof(array[0]) returns the size of an integer (4 bytes). You should pass the number of elements as an extra parameter to the SetArray and SortArray functions.

    When outputting, you output all 20 elements of the array, but only the first one was initialized for the reason mentioned above.

    For your sorting you should use
    Code:
     for(int i=0; i<MAX-1; i++)
     {
      for(int j=i + 1; j<MAX; j++)
    and not
    Code:
     for(int i=0; i<=MAX; i++)
     {
      for(int j=i + 1; j<=MAX; j++)
    In main() you should also reset i to 0 before displaying the sorted array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  2. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Template specialization
    By CornedBee in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2003, 02:02 AM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM