Thread: Pile Program

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    64

    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. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    64
    any suggestions???

  3. #3
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    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. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    64
    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. #5
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Read up on the bubble sort algorithm.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    64
    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. #7
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    that's what a bubble sort does!

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    64
    alright guys, i'll give it a try and see what happens. can anyone direct me to source code for a bubble sort?

  9. #9
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    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. #10
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    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;
    }
    Last edited by BMJ; 09-13-2002 at 02:47 PM.

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    64
    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. #12
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    PHP Code:
    #include <iostream>
    int pile[] = {251510105252515};
    const 
    int size 10;

    void ShowArray()
    {
        for (
    int i 0sizei++)
            
    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 (
    == size-1)
                break;   
            if (
    pile[i] > pile[i+1])
            {
                
    flip(i);
                
    = -1;
            }
            
    i++;
        }
        
    // end sort
        
    ShowArray();
        return 
    0;

    Does this work? (remember, I can't test it)
    Last edited by BMJ; 09-13-2002 at 03:04 PM.

  13. #13
    Registered User
    Join Date
    Sep 2002
    Posts
    64
    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. #14
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    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. #15
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    or in other words, it starts from the bottom of the pile, and works its way to the top

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM