vendredi 3 avril 2015

C - Recursive Section Heading Code


#include <stdio.h>
#include <string.h>
void sections (int width, int depth) {
char buf[100];
int i;
//sprintf(buf,"%s %s", word1, word2);
for (i=1; i<=depth; ++i){
printf("Section %d\n", i);
if ( width >= 1 ){
sections (width-1,depth-1);
printf(" Section %d.X\n", i);
}
}
}

int main() {
sections(3,3);
}


I am trying to create a program (Recursively) that will take two inputs width and depth


So depth is how many sections 'deep' you go. So a depth of one is the printing of Section 1 and similar levels (2, 3, 4, etc). Depth of 2 is Section 1.A and the similar levels (1.B, 1.C, etc). Width is how many of each you have. So width of 3 you go down 1, 2, 3. If you had a depth of 2 it would go down to the 1.A level.


for example



Section 1

Section 1.A

Section 1.A.1

Section 1.A.2

Section 1.B

Section 1.B.1

Section 1.B.2

Section 2

Section 2.A

Section 2.A.1

Section 2.A.2

Section 2.B

Section 2.B.1

Section 2.B.2


But Im trying to create a program that prints things such that :



Section 1
Section XX
Section XX
Section 2
Section XX
Section XX


I am terrible with recursion, and I have tried for hours to create this program. Thanks for any help!


Aucun commentaire:

Enregistrer un commentaire