Writing a recursive function with no local variables and no loops in C++

  • C/C++
  • Thread starter franz32
  • Start date
  • Tags
    C++
In summary, this person needs help with a recursive function that accepts two parameters. One is the seed and the other is the size of the output.
  • #1
franz32
133
0
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:
Technology news on Phys.org
  • #2
Last edited by a moderator:
  • #3
Originally posted by dduardo
For those that don't know, the cocktail shaker sort is more commonly known as bubble sort.


oh, I see. now it has some connection with what I learned from college...
 
  • #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...
 

1. What is "On C++ cocktail shaker"?

"On C++ cocktail shaker" is a popular book written by David Vandevoorde and Bjarne Stroustrup, two experts in the field of C++ programming. It offers a comprehensive guide to programming in C++, covering topics from introductory concepts to advanced techniques.

2. Is "On C++ cocktail shaker" suitable for beginners in C++?

Yes, "On C++ cocktail shaker" is suitable for beginners in C++. The book covers basic concepts in a clear and easy-to-understand manner, making it a great resource for those new to the language.

3. What sets "On C++ cocktail shaker" apart from other C++ books?

"On C++ cocktail shaker" is known for its in-depth coverage of C++ language features, including the latest updates and advancements. The book also includes practical examples and exercises, allowing readers to apply what they have learned.

4. Is "On C++ cocktail shaker" a good reference book for experienced C++ programmers?

Yes, "On C++ cocktail shaker" is a valuable reference book for experienced C++ programmers. It covers advanced topics and offers insights from two experts in the field, making it a valuable resource for those looking to deepen their understanding of the language.

5. Can "On C++ cocktail shaker" be used as a textbook for a C++ course?

Yes, "On C++ cocktail shaker" can be used as a textbook for a C++ course. The book covers all the necessary topics and includes exercises and examples that can be used for classroom instruction. It is also widely used in universities and colleges as a recommended textbook for C++ courses.

Similar threads

  • Programming and Computer Science
Replies
5
Views
1K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
16
Views
4K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
12
Views
3K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
1
Views
942
Back
Top