Insertion Sort Difficulty Compiling
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Insertion Sort Difficulty Compiling

  1. #1
    Join Date
    Dec 2010
    Posts
    2

    Insertion Sort Difficulty Compiling

    I've been trying to get this to compile but I've been unsuccesful:

    Code:
    #include<iostream>
    using namespace std;
    
    #define MAX_LIST_LEN  100
    
    int main(){
    
       int   n,                    // list length
              j,                    // loop control
              i,                    // index of maximum element in unsorted section
              R;                    // index of rightmost element in unsorted section
             double max,                  // maximum value in unsorted section
             temp,                 // temporary storage
              list[MAX_LIST_LEN];   // array to be sorted
    
       // Get values for n and list.
       cout << "Enter list length (must be less than or equal to "
            << MAX_LIST_LEN << "): ";
       cin  >> n;
    cout << "Enter " << n << " numbers:" << endl;
       for(j=0; j<n; j++){
          cin >> list[j];
       }
       (int array[], int n){
          for (int L = 1; L < n; L++){
              int j = L;
              int B = array[L];
          while ((j > 0) && (array[j-1] > B)){
              array[j] = array[j-1];
    j--;
          }
          array[j] = B;
        }
      }
    
       // Print out sorted list.
       for(j=0; j<n; j++){
    
          cout << list[j] << " ";
     }
       cout << endl;
    
       return(0);
    }

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,588

    Re: Insertion Sort Difficulty Compiling

    Code:
       (int array[], int n){
          // ...
        }
    What is this supposed to do? If it's meant to limit the scope of the variables array and n to the following block, this would be a bit closer:

    Code:
       {
          int array[];
          int n;
          // ...
        }
    It would still be illegal syntax, though, because the array needs a size. And even if it had one, both variables would be used in an uninitialized state inside the block, causing either runtime errors or undefined behaviour, depending on the compiler settings.

    Another tip: It's no good idea to use array as a variable name because it could conflict with std::array on newer compilers.

    HTH

    Generally, including the error messages and where in the code you got them would make life of those trying to help you much easier. At least you used code tags from your first post on. That's not really common either among new users here.

    Ah, and... Welcome to CodeGuru!
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Dec 2010
    Posts
    2

    Re: Insertion Sort Difficulty Compiling

    I got the code to work but now the compiled program won't do anything after asking for the list of numbers, I can keep entering numbers but the program doesn't sort them.

    Code:
    #include<iostream>
    using namespace std;
    
    #define MAX_LIST_LEN  100
    
    int main(){
    
       int    n,                    // list length
              j,                    // loop control
              L,                   // index of maximum element in unsorted section
              R;                   // index of rightmost element in unsorted section
    double max,                  // maximum value in unsorted section
              temp,                 // temporary storage
              a[MAX_LIST_LEN];   // array to be sorted
    
       cout << "Enter list length (must be less than or equal to "
            << MAX_LIST_LEN << "): ";
       cin  >> n;
       cout << "Enter " << n << " numbers:" << endl;
       for(j=0; j<n; j++){
     cin >> a[j];
       }
       for (int L = 2; L<n; L++){
       while (L <= n)
         j = L;
         while (j>=2
         )a[j]<a[j-1];
            a [j] == a[j-1];
            j = j-1;
         int L = L+1;
       for(L=0; L<n; L++){
          cout << a[L] << " ";
       }
       cout << endl;
    
       return(0);
    }
    }

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,588

    Re: Insertion Sort Difficulty Compiling

    Code:
       while (L <= n)
         j = L;
    Unless already L > n when this is reached, it is an endless loop. That's because the loop only comprises these two lines and neither L nor n gets changed within the loop. You probably forgot a pair of braces.

    Code:
         while (j>=2
         )a[j]<a[j-1];
    If this is ever reached (because of the endless loop above), it is an endless loop itself unless j < 2 in the first place for practically the same reasons. Apparently here are some braces missing as well.

    This is what I've found so far, but there may very well be more. Your indents are inconsistent and some of the line breaks are simply weird (e.g. considering the ')' in the second code snippet above). Therefore your code is really hard to follow.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center