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: - #include<iostream>
-
using namespace std;
-
void print(float x[],int n);
-
void insert(float x[],int n,float t);//doesn`t work
-
int main()
-
{
-
cout<<"\nInserts 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
-
}
8 13601
Since this won`t compile and is code by a reputable academic i was hoping someone could easily find cause/s.
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.
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.
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?
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.
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.
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. - 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.
This for loop now works as intended and same as while loop - 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;
-
}
-
}
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: 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: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |