Hi everyone!
I want to create a function that it will take as parameters a table
(float), an integer (the number of Table's rows) and it will re-arrange
the elements in the table from the smallest value to the biggest one. I
know it's very simple but something is missing from my code... Untill
now i manage to create something like this:
1. void FunctionReArran ge (float Table[], int TableRows)
2. {
3. float Help1 = 0;
4. float Help2 = 0;
5.
6. if (TableRows <= 1)
7. cout<<"Nothing' s gonna happen!";
8. else
9. {
10. for (int a=0; a<=TableRows-1; a++)
11. {
12. for (int b=1; b<=TableRows-1; b++)
13. {
14. if (Table[a] > Table[b])
15. {
16. Help1 = Table[a];
17. Help2 = Table[b];
18.
19. Table[a] = Help2;
20. Table[b] = Help1;
21. }
22. }
23. }
24. }
25.}
I tried this one with the table (in main):
float myTable[4] = {1.17, 1.37, 1.00, 1.31};
FunctionReArran ge (myTable, 4);
When i compile and run the code, i get results similar to these:
myTable = {1.00, 1.37, 1.17, 1.31} which of course aren't the right
results! (1.00, 1.17, 1.31, 1.37) It's very clear that i cannot see a
detail... The error is somewhere in lines 14 - 20.
When it comes to line 12 for the 1st time it has
Table[a] = Table[0] = 1.17 and Table[b] = Table[1] = 1.37. It's ok so
it takes the next element.
Then it has
Table[a] = Table[0] = 1.17 and Table[b] = Table[2] = 1.00.
So it goes for the change. After that i have myTable[4] = {1.00, 1.37,
1.17, 1.31}!
Here is the "spot" where i want to write something so i can return the
whole procedure from the beginning, couse my code continues like no
change happened.(It continues the check like i had declared the table
myTable with elements {1.00, 1.37, 1.17, 1.31} from the start - it
doesn't recognise that the elements have changed and that it has to
beggin the procudure from the beginning)!
Can anyone help me?
Thanks in advance! 9 1829
"Jimakos Bilakis" <di*********@ya hoo.gr> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.com.. . Hi everyone!
I want to create a function that it will take as parameters a table (float), an integer (the number of Table's rows) and it will re-arrange the elements in the table from the smallest value to the biggest one. I know it's very simple but something is missing from my code... Untill now i manage to create something like this:
1. void FunctionReArran ge (float Table[], int TableRows) 2. { 3. float Help1 = 0; 4. float Help2 = 0; 5. 6. if (TableRows <= 1) 7. cout<<"Nothing' s gonna happen!"; 8. else 9. { 10. for (int a=0; a<=TableRows-1; a++)
You should terminate with : "a < TableRows-1" (or "a <= TableRows-2"), so
that the final iteration in the loop below doesn't hit the same location,
11. { 12. for (int b=1; b<=TableRows-1; b++) 13. { 14. if (Table[a] > Table[b]) 15. { 16. Help1 = Table[a]; 17. Help2 = Table[b]; 18. 19. Table[a] = Help2; 20. Table[b] = Help1;
simpler:
{
float tmp = Table[a];
Table[a] = Table[b];
Table[b] = tmp;
}
(or, I think there's a std::swap function which also does the trick!)
21. } 22. } 23. } 24. } 25.}
I tried this one with the table (in main):
float myTable[4] = {1.17, 1.37, 1.00, 1.31}; FunctionReArran ge (myTable, 4);
When i compile and run the code, i get results similar to these: myTable = {1.00, 1.37, 1.17, 1.31} which of course aren't the right results! (1.00, 1.17, 1.31, 1.37) It's very clear that i cannot see a detail... The error is somewhere in lines 14 - 20.
When it comes to line 12 for the 1st time it has Table[a] = Table[0] = 1.17 and Table[b] = Table[1] = 1.37. It's ok so it takes the next element. Then it has Table[a] = Table[0] = 1.17 and Table[b] = Table[2] = 1.00. So it goes for the change. After that i have myTable[4] = {1.00, 1.37, 1.17, 1.31}!
That's correct. All you're interested in after the first iteration of the
outer loop is: what is the first value in the array. It should be 1.00.
Here is the "spot" where i want to write something so i can return the whole procedure from the beginning, couse my code continues like no change happened.(It continues the check like i had declared the table myTable with elements {1.00, 1.37, 1.17, 1.31} from the start - it doesn't recognise that the elements have changed and that it has to beggin the procudure from the beginning)!
You don't need to restart from the beginning.
This sort is referred to as a "bubble sort". One values "bubbles up" to its
correct location after each iteration of the outer loop.
After the first complete iteration of the outer loop (i.e., after the inner
loop has completed going through all elements the first time, when a is 0),
the first element of the array will be the smallest value (1.00). The
second complete iteration of the loop (where a is 1) will result in the
second smallest element being in the second position, so that the array will
start out {1.00,1.17,...} . What comes after those values in the array isn't
really relevant (yet). So...after each complete iteration of the outer
loop, one more value will have "bubbled up" to where it belongs in the list.
I don't see the problem here offhand, but try my fix I mention above, and
try outputting the values (using std::cout) at the bottom of the outer (a)
for loop. Then see if you don't get the resulyts I describe.
-Howard
>> 10. for (int a=0; a<=TableRows-1; a++) 11. { 12. for (int b=1; b<=TableRows-1; b++) 13. {
As Howard mentioned it's a bubble sort. So after one iteration
of outer loop you have bubbled an element. Hence, the second
loop can start of from the position + 1 that is already bubbled.
for(int a = 0; a < TableRows-1; ++a) {
for(int b = a+1; b < TableRows;++b) {
This is just a performance metric.
But, when you are comparing floating point number I would
usually do by subtracting the operands I want to compare and
check the episilon and if that is greater or less I can make a
decision to exchange based on my criteria for sorting in ascending
or decending order.
-- Nitin Motgi
"Nitin Motgi" <ni*********@gm ail.com> wrote in message
news:11******** **************@ e56g2000cwe.goo glegroups.com.. . 10. for (int a=0; a<=TableRows-1; a++) 11. { 12. for (int b=1; b<=TableRows-1; b++) 13. {
As Howard mentioned it's a bubble sort. So after one iteration of outer loop you have bubbled an element. Hence, the second loop can start of from the position + 1 that is already bubbled.
for(int a = 0; a < TableRows-1; ++a) { for(int b = a+1; b < TableRows;++b) {
This is just a performance metric.
But, when you are comparing floating point number I would usually do by subtracting the operands I want to compare and check the episilon and if that is greater or less I can make a decision to exchange based on my criteria for sorting in ascending or decending order.
That's not needed if all you're doing is comparing which is bigger/smaller.
The epsilon usage is for when you're checking for equality within a
specific, reasonable interval. Comparing floats simply using < or > is
always valid.
-Howard
Thank you so much... you saved me!
Yes... Bubble sort...i have heard about this algorithm.
Howard, you're right it's much simpler the way you showed me with the
variable tmp
Also, Nitin Motgi, thanks for your response too. for(int b = a+1; b < TableRows;++b)
That's right, here is the error in my thought. Guys... what can i
say... Thank you a lot again! Take care .. :-)
Howard wrote: "Jimakos Bilakis" <di*********@ya hoo.gr> wrote in message news:11******** **************@ g10g2000cwb.goo glegroups.com.. . Hi everyone!
I want to create a function that it will take as parameters a table (float), an integer (the number of Table's rows) and it will re-arrange the elements in the table from the smallest value to the biggest one. I know it's very simple but something is missing from my code... Untill now i manage to create something like this:
1. void FunctionReArran ge (float Table[], int TableRows) 2. { 3. float Help1 = 0; 4. float Help2 = 0; 5. 6. if (TableRows <= 1) 7. cout<<"Nothing' s gonna happen!"; 8. else 9. { 10. for (int a=0; a<=TableRows-1; a++)
You should terminate with : "a < TableRows-1" (or "a <= TableRows-2"), so that the final iteration in the loop below doesn't hit the same location,
11. { 12. for (int b=1; b<=TableRows-1; b++) 13. { 14. if (Table[a] > Table[b]) 15. { 16. Help1 = Table[a]; 17. Help2 = Table[b]; 18. 19. Table[a] = Help2; 20. Table[b] = Help1;
simpler: { float tmp = Table[a]; Table[a] = Table[b]; Table[b] = tmp; }
(or, I think there's a std::swap function which also does the trick!)
Yep. There's a std::sort for that matter.
std::sort(Table , Table + TableRows);
Noah Roberts schrieb: Howard wrote: "Jimakos Bilakis" <di*********@ya hoo.gr> wrote in message news:11******** **************@ g10g2000cwb.goo glegroups.com.. . Hi everyone!
I want to create a function that it will take as parameters a table (float), an integer (the number of Table's rows) and it will re-arrange the elements in the table from the smallest value to the biggest one. I know it's very simple but something is missing from my code... Untill now i manage to create something like this:
1. void FunctionReArran ge (float Table[], int TableRows) 2. { 3. float Help1 = 0; 4. float Help2 = 0; 5. 6. if (TableRows <= 1) 7. cout<<"Nothing' s gonna happen!"; 8. else 9. { 10. for (int a=0; a<=TableRows-1; a++) You should terminate with : "a < TableRows-1" (or "a <= TableRows-2"), so that the final iteration in the loop below doesn't hit the same location,
11. { 12. for (int b=1; b<=TableRows-1; b++) 13. { 14. if (Table[a] > Table[b]) 15. { 16. Help1 = Table[a]; 17. Help2 = Table[b]; 18. 19. Table[a] = Help2; 20. Table[b] = Help1; simpler: { float tmp = Table[a]; Table[a] = Table[b]; Table[b] = tmp; }
(or, I think there's a std::swap function which also does the trick!)
Yep. There's a std::sort for that matter.
std::sort(Table , Table + TableRows);
Is this portable code?
I think this only works because std::vector's iterators are defined as
ptrs in some stl implementations .
Marco Spatz wrote: Noah Roberts schrieb: Howard wrote: "Jimakos Bilakis" <di*********@ya hoo.gr> wrote in message news:11******** **************@ g10g2000cwb.goo glegroups.com.. . Hi everyone!
I want to create a function that it will take as parameters a table (float), an integer (the number of Table's rows) and it will re-arrange the elements in the table from the smallest value to the biggest one. I know it's very simple but something is missing from my code... Untill now i manage to create something like this:
1. void FunctionReArran ge (float Table[], int TableRows) 2. { 3. float Help1 = 0; 4. float Help2 = 0; 5. 6. if (TableRows <= 1) 7. cout<<"Nothing' s gonna happen!"; 8. else 9. { 10. for (int a=0; a<=TableRows-1; a++) You should terminate with : "a < TableRows-1" (or "a <= TableRows-2"), so that the final iteration in the loop below doesn't hit the same location,
11. { 12. for (int b=1; b<=TableRows-1; b++) 13. { 14. if (Table[a] > Table[b]) 15. { 16. Help1 = Table[a]; 17. Help2 = Table[b]; 18. 19. Table[a] = Help2; 20. Table[b] = Help1; simpler: { float tmp = Table[a]; Table[a] = Table[b]; Table[b] = tmp; }
(or, I think there's a std::swap function which also does the trick!) Yep. There's a std::sort for that matter.
std::sort(Table , Table + TableRows);
Is this portable code? I think this only works because std::vector's iterators are defined as ptrs in some stl implementations .
I'm not sure where std::vector comes into this discussion.
std::sort requires random access iterators, a pointer into an array, and
a std::vector<T>: :iterator (regardless of how it is implemented) are
both valid random access iterators.
Ben Pope
--
I'm not just a number. To many, I'm known as a string...
>> Is this portable code? I think this only works because std::vector's iterators are defined as ptrs in some stl implementations .
I'm not sure where std::vector comes into this discussion.
std::sort requires random access iterators, a pointer into an array, and a std::vector<T>: :iterator (regardless of how it is implemented) are both valid random access iterators.
Ben Pope
That's what I wanted to know. So this code is legal:
numElements = 12;
float array[numElements];
std::sort(array , array+numElemen ts);
Thanks
Marco Spatz
Marco Spatz wrote: Is this portable code? I think this only works because std::vector's iterators are defined as ptrs in some stl implementations . I'm not sure where std::vector comes into this discussion.
std::sort requires random access iterators, a pointer into an array, and a std::vector<T>: :iterator (regardless of how it is implemented) are both valid random access iterators.
That's what I wanted to know. So this code is legal:
numElements = 12; float array[numElements];
Nope... array dimension must be constant and have an integral type ;)
std::sort(array , array+numElemen ts);
Yes, that's fine.
Ben Pope
--
I'm not just a number. To many, I'm known as a string... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: js |
last post by:
Two questions:
1. I created Javascript objects called oNewObj using {} construct as
in the follwoing code segment. When I clicked on the <td> element, I
got runtime error "oNewObj is undefined". When I changed the <td>
onclick event to doThis(this), then I can exam (this) object.
However, I really want to exam the oNewObj. Does anyone know how to
do it? Thanks.
2. I need to add additional things to the oNewObj, how could I append
|
by: Peter Kirk |
last post by:
Hi there
can someone please help me with creating dynamic content in a table? For
example, see the below javascript and html - why is a new row not created in
the table when I click the button? (I am using Internet Explorer 6).
<html>
<head>
<title>TEST</title>
<script language="javascript">
|
by: SAN CAZIANO |
last post by:
is there a function that get the name of the first input field of the
current form ?
in my example below I want create an array of form field name and in the
onsubmit assign all element's name to create a simple iteration to test if
some elements in my array, that must be required, are null:
something like function verify(array of string) and in onsubmit something
like return onsubmit(field1,field2,field3....)
<HTML>
<HEAD>
|
by: Susan Cranford |
last post by:
Please forgive, I have looked at so much info I can't figure out how to
put it together even though I know it must be fairly simple.
I have an array of input text boxes (txtDOBn) where n is created at
load. On the onchange event I want to calc the age and show in adjacent
input text boxes that are readonly and also arrays (an age calced for
each DOB entered). I was going to use the datediff function in vbscript
to do the calc.
Can...
|
by: dog |
last post by:
I've seen plenty of articles on this topic but none of them have been
able to solve my problem.
I am working with an Access 97 database on an NT4.0 machine, which has
many Access reports.
I want my users to be able to select a report, click on a command
button on a form, which will then automatically create the report as a
pdf file and save it to the user's machine.
I am using Adobe Acrobat (5.0 I think) and have Adobe Distiller as a
| |
by: Piotr |
last post by:
In Effective STL item 9 "Choose carefully among erasing options", it
has this example:
bool badValue(int x); // returns whether x is 'bad'
c.erase ( remove_if(c.begin(), c.end(), badValue), c.end()); // this
is the best way to get rid of objects where badValue returns true when
c is a vector, string, or dequeue
c.remove_if(badValue); // this is the best way to get rid of objects
where badValue returns true when c is a list
|
by: atyndall |
last post by:
OK,
Well I've got this script and I'd like to implement this function into it where a function retrieves a specified amount of data from a specific mysql database, rearranges a part of it, and outputs the result to another specified varible. this is what i've got so far (please ignore the long-winded variables, they are just for explanations):
function RearrangeDate ($input-data/time, $the-way-it-should-be-arranged-e.g.-D/m/y-(not...
|
by: Beamor |
last post by:
function art_menu_xml_parcer($content, $showSubMenus)
{
$doc = new DOMDocument();
$doc->loadXML($content);//this is the line in question
$parent = $doc->documentElement;
$elements = $parent->childNodes;
need help. my site worked fine on my localhost but when i uploaded it to my live server i keep getting this error
need help to recode line to match my hosting server.
im using php 5
i have attached the common_method file
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |