Hello,
i was trying to generate a series as follows:
111
112
113
121
122
123
131
132
133

and this is what i tried:

#include <stdio.h>

int A[3];

void func(int k)
{
	static int level = -1;

	A[++level] = k;

	for(int j = 1; j <= 3; j++) {		
		if(level < 3) {
			func(j);
		} else {
			for(int i = 0; i < 3; i++) {
				printf("%d", A[i]);			
			}
		}
		printf("\n");
		//getchar();
	}
	--level;
}

int main(void)
{
  func(1);
  getchar();

  return 0;

}

This would be easier if you used strtol() to handle all the conversions.

im sorry..i did not get you..im not doing any conversions.

Try solving this problem with a linked list. It turns out to be pretty simple...Here's a very rough and buggy example to show you what I mean.

#include <stdio.h>
#include <stdlib.h>

struct node
{
	unsigned char value;
	struct node *nptr;
};

void showit(struct node *nptr)
{
	if (nptr->nptr) showit(nptr->nptr);
	fprintf(stdout, "%u", nptr->value);
}

void addone(struct node *ptr)
{
	if (ptr->value < 3)
	{
		++(ptr->value);
	}
	else if (!ptr->nptr)
	{
		ptr->value = 0;
		ptr->nptr = (struct node*)malloc(sizeof(struct node));
		ptr->nptr->value = 1;
		ptr->nptr->nptr = NULL;
	}
	else
	{
		ptr->value = 0;		
		addone(ptr->nptr);
	}
}

int main(int argc, char**argv)
{
	unsigned int i = 0;
	struct node start = {0, NULL};

	for (i = 0; i < 100; ++i)
	{
		addone(&start);
		showit(&start);
		fputs("\n", stdout);
	}
	exit(EXIT_SUCCESS);
}

Think of an old fashioned car odometer, with three wheels displaying their digit.

Now use three nested for loops, and don't over-complicate it. Let the loops do their magic. No if's, no else's, no bs. Easy as pie. ;)

int w1, w2, w3;  //wheel 1, wheel 2, wheel 3

for(w1=1;w1<10;w1++) {
  for(w2=1;w2<10;w2++) {
    for(w3=1;w3<10;w3++) {
      printf(\n %d %d %d", w1, w2, w3);
    }
  }
}

Try that, see if you understand it.

@gerard4143, Adak..Thanks..

@Adak
i needed to learn recursion. So i was using this method.

if (level < 3)
       {
               for (int i = 1; i <= base; i++)
               {
                       A[level] = i;
                       func(level+1);
               }
       }

This did the trick. Removed the static variable.

Thanks for the support :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.