# Thread: Pile Program

1. ## Pile Program

how would one use arrays to construct a program in which you are given a pile of coins, and you must sort the coins from largest to smallest. the only action the program is able to do is a FLIP, where you flip coins from the top? any suggestions?

2. any suggestions???

3. Why did you wait 10 minutes and post? Wait a little longer, maybe someone wouldv'e answered. Anyways I don't really get what you're saying. It's not that clear

4. sorry for the quick reply.

okay, basically the program takes commands from the user.

for example, the user inputs the following: qdnppq
(quarter, dime, nickle, penny, penny, quarter)

imagine the first q is the bottom of a pile.
the program then must take this pile of coins and use a FLIP function to sort the list as follows. qqnppd (largest to smallest).

the flip function flips from the top. basically it would compare sizes and flip if needed.

if it's still not clear, i can be more precise.

5. Read up on the bubble sort algorithm.

6. thanks for the suggestion, but i really don't think bubble sorting is what this program is supposed to use. it is supposed to look at the highest and lowest values (based on coin size) and use that comparison in the flip.

7. that's what a bubble sort does!

8. alright guys, i'll give it a try and see what happens. can anyone direct me to source code for a bubble sort?

9. Code:
```#define ARR_SIZE 5

char coins[ARR_SIZE];

for (int i = 0; i < ARR_SIZE; ++i)
for (int j = 0; j < (ARR_SIZE - 1); ++j) {
if (coins[j] > coins[j + 1]) {
char tmp = coins[j + 1];
coins[j + 1] = coins[j];
coins[j] = tmp;
}
}```
Try something like that for the sort. You'll have to do the figuring of which size is > the others and such.

HTH.

10. Doing a bubble sort on built-in array types is easy:

Keep in mind that currently I am without a compiler (need to buy VC++.NET, so this code is untested; please let me know what happens
Code:
```#include <iostream>
const int size = 10;

void ShowArray(int array[])
{
for (int i = 0; i < size; i++)
std::cout << array[i] << ' ';
std::cout << "\n\n";
}

void flip(int array[], int index1, int index2))
{
int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}

int main()
{
int iarray[] = {10, 1, 6, 2, 0, 43, 322, 4, 15, 3};
ShowArray(iarray);
for (int i = 0;;)
{

if (i == size-1)
break;
if (iarray[i] > iarray[i+1])
{
flip(iarray, i, i+1);
i = -1;
}
i++;
}
ShowArray(iarray);
return 0;
}```

11. code works great! thanks guys....HOWEVER, the flip statement that i am supposed to use is only allowed to take an argument of type int. do you have any suggestions there?

12. PHP Code:
``` #include <iostream> int pile[] = {25, 1, 5, 10, 10, 5, 25, 25, 1, 5}; const int size = 10; void ShowArray() {     for (int i = 0; i < size; i++)         std::cout << pile[i] << ' ';     std::cout << "\n\n"; } void flip(int index) {     int temp = pile[index];     pile[index] = pile[index+1];     pile[index+1] = temp; } int main() {     ShowArray();     // begin sort     for (int i = 0;;)     {         if (i == size-1)             break;            if (pile[i] > pile[i+1])         {             flip(i);             i = -1;         }         i++;     }     // end sort     ShowArray();     return 0; }  ```
Does this work? (remember, I can't test it)

13. great! wow, you are a big help. there were 2 minor cosmetic errors ( just the ; in the for statement) but it works great besides that. thanks so much!

14. Think about it though... bubble sorts are so easy to understand... it starts at the beginning, sees if the next element is smaller, if it is flip em and start over, otherwise keep going... once you reach the last element (size-1); you're done!

15. or in other words, it starts from the bottom of the pile, and works its way to the top

Popular pages Recent additions