470,852 Members | 1,236 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,852 developers. It's quick & easy.

C++ Inserting an element into a sorted array

542 512MB
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:

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3. void print(float x[],int n);
  4. void insert(float x[],int n,float t);//doesn`t work
  5. int main()
  6. {
  7.   cout<<"\nInserts a value into a sorted array\n";
  8.   float x[8]={22.3,33.4,44.5,66.7,77.8};
  9.   cout<<"Array x [] is: ";
  10.   print(x,5);
  11.   insert(x,4,55.6);    
  12.   cout<<"\n\n";    
  13.   system("pause");
  14.   return 0;   
  15. }
  16. void print(float x[],int n)
  17. {
  18.      for(int i=0;i<n;i++)
  19.      cout<<x[i]<<" ";
  20. }
  21. void insert(float x[],int n,float t)//this is not my work now; it is the published solution.
  22. {
  23.    for(int i = n;i>0 && x[i-1]> t;i--)//using obsolete binding at 'i'     
  24.      x[i]=x[i-1];
  25.      x[i]= t;//name lookup of i changed for new ISO 'for' scoping
  26. }
Feb 14 '08 #1
8 13227
542 512MB
Since this won`t compile and is code by a reputable academic i was hoping someone could easily find cause/s.
Feb 19 '08 #2
3,652 Expert 2GB
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.
Feb 19 '08 #3
542 512MB
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
Feb 20 '08 #4
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]= 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?
Feb 20 '08 #5
542 512MB
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.
Feb 23 '08 #6
542 512MB
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.
Feb 23 '08 #7
542 512MB
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.

Expand|Select|Wrap|Line Numbers
  1. void insert (float a [],int n, float x)
  2. {
  3.      int j=n;
  4.      while (j>0 && a[j-1]>x)
  5.      a[j--]=a[j-1];
  6.      a[j]=x;
  7.      ++n;
  8.  }
Feb 24 '08 #8
542 512MB
This for loop now works as intended and same as while loop

Expand|Select|Wrap|Line Numbers
  1. void insert(float x[],int n,float t)
  2. {
  3.    for(int i = n;i>0 && x[i-1]> t; )
  4.     { x[i--]=x[i-1];
  5.      x[i]= t;
  6.       ++n;
  7.     }
  8.   } 
Feb 24 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by ebc | last post: by
reply views Thread by Raed Sawalha | last post: by
2 posts views Thread by Murt | last post: by
10 posts views Thread by Sonnich | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.