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?
This is a discussion on Pile Program within the C++ Programming forums, part of the General Programming Boards category; how would one use arrays to construct a program in which you are given a pile of coins, and you ...
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?
any suggestions???
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
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.
Read up on the bubble sort algorithm.
Try not.
Do or do not.
There is no try.
- Master Yoda
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.
that's what a bubble sort does!
alright guys, i'll give it a try and see what happens. can anyone direct me to source code for a bubble sort?
Try something like that for the sort. You'll have to do the figuring of which size is > the others and such.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; } }
HTH.
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 happensCode:#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; }
Last edited by BMJ; 09-13-2002 at 02:47 PM.
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?
Does this work? (remember, I can't test it)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;
}
Last edited by BMJ; 09-13-2002 at 03:04 PM.
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!
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!
or in other words, it starts from the bottom of the pile, and works its way to the top