Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

On C++ cocktail shaker

  1. Feb 26, 2004 #1
    help On C++, recursive function

    Hello.

    I need help... does anyone know /where can I get the code for the cocktail shaker algorithm for C++ in which it contains the simplest code?

    How would I make a graphics sort out of the elementary sorting algorithms? Are there sources w/c show the codes for making graphsort?
     
    Last edited: Feb 27, 2004
  2. jcsd
  3. Feb 26, 2004 #2

    dduardo

    User Avatar
    Staff Emeritus

  4. Feb 27, 2004 #3


    oh, I see. now it has some connection with what I learned from college.....
     
  5. Feb 27, 2004 #4
    Thank you... now this time on recursive function

    Oh, thanks for the shaker sort... some sources say it is called the bi-directional bubble sort.

    The new problem: Write a recursive function with no local variables and no loops that will accept 2 parameters seed and size where...
    seed denotes the first number to appear in the output screen
    size - no of columns. (allows to display single digit numbers...)

    Ex.1 seed: 3
    size: 3

    output:
    3 4 5
    6 7
    8

    ex2. seed: 7
    size: 5

    output:
    7 8 9 0 1
    2 3 4 5
    6 7 8
    9 0
    1

    I made a recursive function out of these: Try it. (Turbo C++)
    # include <iostream.h>
    # include <conio.h>
    # include <stdlib.h>

    int recur(int seed, int size);
    int col(int seed, int size);
    int main()
    {
    clrscr();
    int seed, size;
    cout << "input seed: ";
    cin >> seed;
    cout << "input no. of columns: ";
    cin >> size;
    col (seed, size);
    getch();
    return 0;
    }

    int col (int seed, int size)
    {
    if (size != 0)
    {
    recur (seed, size);
    cout << "\n";
    size -= 1;
    seed = seed + size + 1;
    if (seed > 10)
    seed -= 10;
    col (seed, size);
    }
    return 0;
    }

    int recur (int seed, int size)
    {
    if (size != 0)
    {
    if (seed == 10)
    {
    seed = 0;
    cout << "0" << " ";
    seed += 1;
    recur (seed, size - 1);
    }
    else
    {
    cout << seed << " ";
    seed += 1;
    recur (seed, size - 1);
    }
    }
    return 0;
    }


    That's one example I've made on my own... is there another way of showing a different way of creating recursive function following the restrictions above? I hope someone can show it to me...
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: On C++ cocktail shaker
  1. Learning C/C++ (Replies: 5)

  2. C question (Replies: 6)

  3. C++ vector (Replies: 3)

  4. Learning C++ (Replies: 9)

  5. A C problem (Replies: 9)

Loading...