Template Sorting Problem

This is a discussion on Template Sorting Problem within the C++ Programming forums, part of the General Programming Boards category; I've looked over my code several times and I'm unable to find out what I did wrong. It looks ok ...

  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.
    Attached Files Attached Files

  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, 09:11 AM
  2. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 10:42 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05: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

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