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

J

Jimakos Bilakis

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)
15. {
16. Help1 = Table[a];
17. Help2 = Table;
18.
19. Table[a] = Help2;
20. Table = 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 = Table[1] = 1.37. It's ok so
it takes the next element.
Then it has
Table[a] = Table[0] = 1.17 and Table = 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!
 
H

Howard

Jimakos Bilakis said:
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)
15. {
16. Help1 = Table[a];
17. Help2 = Table;
18.
19. Table[a] = Help2;
20. Table = Help1;


simpler:
{
float tmp = Table[a];
Table[a] = Table;
Table = 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 = Table[1] = 1.37. It's ok so
it takes the next element.
Then it has
Table[a] = Table[0] = 1.17 and Table = 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
 
N

Nitin Motgi

10. for (int a=0; a<=TableRows-1; a++)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
 
H

Howard

Nitin Motgi said:
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
 
J

Jimakos Bilakis

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.
That's right, here is the error in my thought. Guys... what can i
say... Thank you a lot again! Take care .. :)
 
N

Noah Roberts

Howard said:
Jimakos Bilakis said:
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)
15. {
16. Help1 = Table[a];
17. Help2 = Table;
18.
19. Table[a] = Help2;
20. Table = Help1;


simpler:
{
float tmp = Table[a];
Table[a] = Table;
Table = 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);
 
M

Marco Spatz

Noah said:
Howard said:
Jimakos Bilakis said:
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)
15. {
16. Help1 = Table[a];
17. Help2 = Table;
18.
19. Table[a] = Help2;
20. Table = Help1;

simpler:
{
float tmp = Table[a];
Table[a] = Table;
Table = 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.
 
B

Ben Pope

Marco said:
Noah said:
Howard said:
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)
15. {
16. Help1 = Table[a];
17. Help2 = Table;
18.
19. Table[a] = Help2;
20. Table = Help1;
simpler:
{
float tmp = Table[a];
Table[a] = Table;
Table = 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
 
M

Marco Spatz

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
 
B

Ben Pope

Marco said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top