Connecting Tech Pros Worldwide Help | Site Map

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

Jimakos Bilakis
Guest
 
Posts: n/a
#1: Mar 23 '06
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!

Howard
Guest
 
Posts: n/a
#2: Mar 23 '06

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



"Jimakos Bilakis" <divasila238@yahoo.gr> wrote in message
news:1143133472.952475.134110@g10g2000cwb.googlegr oups.com...[color=blue]
> 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++)[/color]

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,
[color=blue]
> 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;[/color]

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!)
[color=blue]
> 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}![/color]

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.
[color=blue]
> 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)!
>[/color]

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




Nitin Motgi
Guest
 
Posts: n/a
#3: Mar 23 '06

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


>> 10. for (int a=0; a<=TableRows-1; a++)[color=blue][color=green]
>> 11. {
>> 12. for (int b=1; b<=TableRows-1; b++)
>> 13. {[/color][/color]
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

Howard
Guest
 
Posts: n/a
#4: Mar 23 '06

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



"Nitin Motgi" <nitin.motgi@gmail.com> wrote in message
news:1143136864.968571.323160@e56g2000cwe.googlegr oups.com...[color=blue][color=green][color=darkred]
>>> 10. for (int a=0; a<=TableRows-1; a++)
>>> 11. {
>>> 12. for (int b=1; b<=TableRows-1; b++)
>>> 13. {[/color][/color]
> 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.[/color]

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



Jimakos Bilakis
Guest
 
Posts: n/a
#5: Mar 23 '06

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


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.[color=blue][color=green]
>> for(int b = a+1; b < TableRows;++b)[/color][/color]

That's right, here is the error in my thought. Guys... what can i
say... Thank you a lot again! Take care .. :-)

Noah Roberts
Guest
 
Posts: n/a
#6: Mar 23 '06

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



Howard wrote:[color=blue]
> "Jimakos Bilakis" <divasila238@yahoo.gr> wrote in message
> news:1143133472.952475.134110@g10g2000cwb.googlegr oups.com...[color=green]
> > 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++)[/color]
>
> 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,
>[color=green]
> > 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;[/color]
>
> 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!)[/color]

Yep. There's a std::sort for that matter.

std::sort(Table, Table + TableRows);

Marco Spatz
Guest
 
Posts: n/a
#7: Mar 24 '06

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


Noah Roberts schrieb:[color=blue]
> Howard wrote:[color=green]
>> "Jimakos Bilakis" <divasila238@yahoo.gr> wrote in message
>> news:1143133472.952475.134110@g10g2000cwb.googlegr oups.com...[color=darkred]
>>> 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++)[/color]
>> 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,
>>[color=darkred]
>>> 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;[/color]
>> 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!)[/color]
>
> Yep. There's a std::sort for that matter.
>
> std::sort(Table, Table + TableRows);
>[/color]

Is this portable code?
I think this only works because std::vector's iterators are defined as
ptrs in some stl implementations.
Ben Pope
Guest
 
Posts: n/a
#8: Mar 24 '06

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


Marco Spatz wrote:[color=blue]
> Noah Roberts schrieb:[color=green]
>> Howard wrote:[color=darkred]
>>> "Jimakos Bilakis" <divasila238@yahoo.gr> wrote in message
>>> news:1143133472.952475.134110@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!)[/color]
>> Yep. There's a std::sort for that matter.
>>
>> std::sort(Table, Table + TableRows);
>>[/color]
>
> Is this portable code?
> I think this only works because std::vector's iterators are defined as
> ptrs in some stl implementations.[/color]

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...
Marco Spatz
Guest
 
Posts: n/a
#9: Mar 24 '06

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


>>[color=blue][color=green]
>> Is this portable code?
>> I think this only works because std::vector's iterators are defined as
>> ptrs in some stl implementations.[/color]
>
> 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[/color]

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
Ben Pope
Guest
 
Posts: n/a
#10: Mar 27 '06

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


Marco Spatz wrote:[color=blue][color=green][color=darkred]
>>> Is this portable code?
>>> I think this only works because std::vector's iterators are defined as
>>> ptrs in some stl implementations.[/color]
>> 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.
>>[/color]
>
> That's what I wanted to know. So this code is legal:
>
> numElements = 12;
> float array[numElements];[/color]

Nope... array dimension must be constant and have an integral type ;)
[color=blue]
> std::sort(array, array+numElements);[/color]

Yes, that's fine.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Closed Thread