473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Recursion (slightly off-topic)

I'm trying to convert this:

for(int i=0; i<5; ++i)
for(int j=0; j<5; ++j)
for(int k=0; k<5; ++k)
cout << i+1 << j+1 << k+1 << endl;

Into a recusive function. But I ended with:

#include <iostream>
using namespace std;

void permute(int);
int A[3];

int main(){

permute(0);

return 0;
}

void permute(int n){

for(int i=0; i<5; ++i){

A[n]=i+1;

if(n<3){
permute(n+1);
} else {
for(int j=0; j<3; ++j)
cout << A[j];
cout << endl;
}
}
}

Which doesn't work! I'm lost here, could anyone give me a clue?

Nov 22 '05 #1
2 962
> if(n<3){

Probably:

if (n+1 < 3) {
Nov 22 '05 #2

You are very close! (but it's late and being the Saturday of a very long
week, there is drinking involved)

Shouldn't that be if(n<2)? If n==2 you will call permute(3) which will
cause A[3] = I+1; and there ain't no A[3]

On 11/19/05 8:37 PM, in article
11*********************@g49g2000cwa.googlegroups.c om, "Gaijinco"
<ga******@gmail.com> wrote:

void permute(int n) {
for(int i=0; i<5; ++i) {
A[n]=i+1;

if(n<3) ////////////////////// if (n<2) here { permute(n+1);
} else
{ for(int j=0; j<3; ++j) { cout << A[j]; } cout << endl;
}
}

}

Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
8
by: pembed2003 | last post by:
Hi all, As an exercise, I am trying to come up with a function to count the length of a char* string using recursion. I came up with this: int count_length(char* s){ if(s == 0) return 0;...
8
by: No bother | last post by:
I have a table with two columns, one named master, the other slave. Each column has a set of numbers. A number in one column can appear in the other. I am trying to see if there is any infinite...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
19
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
3
by: David Bear | last post by:
Is there an easy way to get the current level of recursion? I don't mean sys.getrecursionlimit. I want to know during a live run of a script how many times the functions has recursed -- curses, I...
5
by: Jstone | last post by:
Hi all. I know this group is devoted to Javascript but I thought someone may be able to answer a couple questions for me. First, is there a way to customize the border of a Flash MX projector with...
20
by: athar.mirchi | last post by:
..plz define it.
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.