473,385 Members | 1,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 13599
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

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

Similar topics

by: ebc | last post by:
Hi, I have written a function that removes double entries from a sorted array. See the structures typedef struct tagRECR { char name;
by: Raed Sawalha | last post by:
I have following code that reads a binary file into array of bytes, after I read the file i need to insert a space in fisrt element of array ,how can I achieve this System.IO.FileStream fs = new...
by: Don | last post by:
My user control has a combobox with an arraylist attached to it along with custom add and remove methods. The "Add" method is working great. However I don't understand why the "Remove" method...
by: Murt | last post by:
if i perform a sort on an array, how do i get the sorted array to be displayed in a listbox? thanks murt
by: ericnyc | last post by:
This is my second request. Some how I wrote the first problem I posted. In this assignment I need to print out the sorted array with say 10 entries per line and then Finally it should print out...
by: Sonnich | last post by:
Hi if I have an array of and want to insert c, what is the way then? Sonnich
by: =?utf-8?B?5YiY5piK?= | last post by:
Hi, folks, Is it possible to delete an element from a sorted array with O(1) time? Best regards
by: getteddy | last post by:
hi i am working on array , i am facing a problem in comparing data base element with array element if not (objrs("namephase")=("despArray(ctr,0)+despArray(ctr,3)")) then or If...
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.