C++ Inserting an element into a sorted array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whodgson
    Contributor
    • Jan 2007
    • 542

    C++ Inserting an element into a sorted array

    This is a learning exercise-it is not course homework- so not urgent
    When i try to compile it i get error messages about the insert() function as
    commented below. Could someone explain what is wrong? If so thanks.

    Here is test driver and offending function text code:

    [CODE=cpp]#include<iostre am>
    using namespace std;
    void print(float x[],int n);
    void insert(float x[],int n,float t);//doesn`t work
    int main()
    {
    cout<<"\nInsert s a value into a sorted array\n";
    float x[8]={22.3,33.4,44. 5,66.7,77.8};
    cout<<"Array x [] is: ";
    print(x,5);
    insert(x,4,55.6 );
    cout<<"\n\n";
    system("pause") ;
    return 0;
    }
    void print(float x[],int n)
    {
    for(int i=0;i<n;i++)
    cout<<x[i]<<" ";
    }
    void insert(float x[],int n,float t)//this is not my work now; it is the published solution.
    {
    for(int i = n;i>0 && x[i-1]> t;i--)//using obsolete binding at 'i'
    x[i]=x[i-1];
    x[i]= t;//name lookup of i changed for new ISO 'for' scoping
    }[/CODE]
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    Since this won`t compile and is code by a reputable academic i was hoping someone could easily find cause/s.

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Where does it give you an error? I see that you are trying to use i outside of the for loop, but i is declared in the header of the for loop and doesn't exist before or after it.

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        Thanks Gannon11,
        The two error messages are commented on lines 23 and 25. This function will not compile for these 2 reasons which i do not understand. I can only think that the compiler has been upgraded since the code was written and no longer
        accepts what is now obsolete code. I`m using Bloodshed Dev C++ version 4.9.9.2.

        Comment

        • davmt
          New Member
          • Feb 2008
          • 3

          #5
          Hi, I'm a newbie in C++ but it seems you're forgetting to enclose the { } brackets around the for loop if you intended to use all those instructions for that particular for loop,

          for(int i = n;i>0 && x[i-1]> t;i--)//using obsolete binding at 'i'
          {
          x[i]=x[i-1];
          x[i]= t;//name lookup of i changed for new ISO 'for' scoping
          }

          However I can't seem to understand the purpose of your program. Are you trying to modify the value of one of the elements in the array and then resort the array?

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            Thanks DAVMT
            Yes the purpose of the insert() function is to insert a new element into an already sorted array. On L11 it attemts to insert 55.6 into its correct position in array x[]. Will put some braces round the for loop in the insert function and see what happens.

            Comment

            • whodgson
              Contributor
              • Jan 2007
              • 542

              #7
              Yes braces round the for loop corrected the compile problem now i just need to clean up the code so that it works correctly Many thanks davmt.
              This thread is ok to terminate.

              Comment

              • whodgson
                Contributor
                • Jan 2007
                • 542

                #8
                As a matter of interest, even though the code compiled i couldn`t get the insert function to insert the element into the array in the correctly ordered position. It just inserted it at the (new) last element position.
                However with the following insert function() using a while loop the inserted element is placed in its correctly ordered position.

                Code:
                void insert (float a [],int n, float x)
                {
                     int j=n;
                     while (j>0 && a[j-1]>x)
                     a[j--]=a[j-1];
                     a[j]=x;
                     ++n;
                 }
                cheers.

                Comment

                • whodgson
                  Contributor
                  • Jan 2007
                  • 542

                  #9
                  This for loop now works as intended and same as while loop

                  Code:
                  void insert(float x[],int n,float t)
                  {
                     for(int i = n;i>0 && x[i-1]> t; )
                      { x[i--]=x[i-1];
                       x[i]= t;
                        ++n;
                      }
                    }

                  Comment

                  Working...