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

  • Context: C/C++ 
  • Thread starter Thread starter franz32
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around writing a recursive function in C++ that adheres to specific constraints: it must not use local variables or loops. Participants explore the cocktail shaker sort algorithm and its relationship to bubble sort, while also seeking assistance in implementing a recursive function that generates a specific output format based on given parameters.

Discussion Character

  • Technical explanation
  • Homework-related
  • Exploratory

Main Points Raised

  • One participant requests help with the cocktail shaker sort algorithm and seeks simple code examples.
  • Another participant clarifies that the cocktail shaker sort is commonly known as bubble sort and provides a resource link.
  • There is a discussion about the terminology used for the cocktail shaker sort, with some participants referring to it as bi-directional bubble sort.
  • A participant presents a recursive function example that meets the specified constraints and requests alternative methods for achieving the same functionality.
  • The provided code includes functions for generating output based on seed and size parameters, but the participant is looking for different approaches to the problem.

Areas of Agreement / Disagreement

Participants do not reach a consensus on alternative methods for creating the recursive function, and multiple viewpoints on the cocktail shaker sort's terminology are present.

Contextual Notes

The discussion includes various interpretations of the cocktail shaker sort and the constraints of the recursive function, but does not resolve the differences in terminology or coding approaches.

franz32
Messages
133
Reaction score
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
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...
 
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;
count << "input seed: ";
cin >> seed;
count << "input no. of columns: ";
cin >> size;
col (seed, size);
getch();
return 0;
}

int col (int seed, int size)
{
if (size != 0)
{
recur (seed, size);
count << "\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;
count << "0" << " ";
seed += 1;
recur (seed, size - 1);
}
else
{
count << 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...
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
9
Views
3K
  • · Replies 6 ·
Replies
6
Views
13K
  • · Replies 16 ·
Replies
16
Views
7K
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K