473,322 Members | 1,526 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,322 software developers and data experts.

How To Create a Function that Rearranges the elements of a table by ASC

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 FunctionReArrange (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};
FunctionReArrange (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!

Mar 23 '06 #1
9 1820

"Jimakos Bilakis" <di*********@yahoo.gr> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.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 FunctionReArrange (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};
FunctionReArrange (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


Mar 23 '06 #2
>> 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

Mar 23 '06 #3

"Nitin Motgi" <ni*********@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.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

Mar 23 '06 #4
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 .. :-)

Mar 23 '06 #5

Howard wrote:
"Jimakos Bilakis" <di*********@yahoo.gr> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.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 FunctionReArrange (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);

Mar 23 '06 #6
Noah Roberts schrieb:
Howard wrote:
"Jimakos Bilakis" <di*********@yahoo.gr> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.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 FunctionReArrange (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.
Mar 24 '06 #7
Marco Spatz wrote:
Noah Roberts schrieb:
Howard wrote:
"Jimakos Bilakis" <di*********@yahoo.gr> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.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 FunctionReArrange (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...
Mar 24 '06 #8
>>
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+numElements);
Thanks

Marco Spatz
Mar 24 '06 #9
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+numElements);


Yes, that's fine.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 27 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
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"....
10
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?...
1
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...
12
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...
7
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...
10
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),...
5
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...
1
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 =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.