• C

recursion

Hello,

Can you write a program in c that:

uses numbers until n in variations of m.
For example:

if:
m=2
n=3

desired output should be:
1,2
2,1
3,1
3,2
1,3
.
.
etc.

You have to use numbers until n (3 in the example - no zero included)in strings of m (2 in the example).

ANOTHER EXAMPLE FOR CLARIFICATION:

IF:
M=3
N=4

DESIRED OUTPUT:
1,2,3
1,3,2
3,2,1
3,1,2
2,3,1
2,1,3
2,1,4
4,1,3
2,3,4
.
.
ETC.

(-ORDER IS NOT IMPORTANT
 - CAN'T USE A NUMBER TWICE IN A STRING)


Waiting for your answer
Thanks.
InbalAsked:
Who is Participating?
 
deightonprogCommented:
#include<stdio.h>
#include<stdlib.h>

int perms(int m,int n)
{
      int *numbers;
      int *pointers;

      int bend,icarry,c,x;

      if ((pointers = (int *) malloc(m))==NULL)
            exit(1);

      if ((numbers = (int *) malloc(n))==NULL)
            exit(1);

      for(c=0;c<n;c++)
            pointers[c]=0;

      do {
      c=0;
      icarry = 1;
      do
      {
            pointers[c] += icarry;
            if (pointers[c]==n)
            {
                  pointers[c]=0;
                  icarry=1;
            }
            else
            {
                  icarry^=icarry;
            }
            ++c;

      } while (c<m && icarry != 0);
/*       printf("\n %i %i",pointers[0],pointers[1]);*/

      if (icarry == 0)
      {
            for(c=0;c<n;c++)
                  numbers[c]=0;
            c^=c;
            do
            {
                  x = numbers[pointers[c]]+1;
                  numbers[pointers[c]]=x;
                  c++;
            } while (c!=m && x!=2);

            if (x!=2)
            {
                  printf("\n");
                  for(c=0;c<m;c++)
                  {
                        printf("%i",pointers[c]+1);
                        if (c<m-1)
                              printf(",");
                  }
            }
      }
      else
      {
            free(pointers);
            free(numbers);
            return 1;
      }
      } while(69);

}


void main()
{
      char x;
      printf("\n\n-------------------\n");
      x = perms(3,4);
      printf("\n_____________________");

      x= getch();
}
0
 
InbalAuthor Commented:
Adjusted points to 100
0
 
dhymesCommented:
Your question is not clear. Give example if m=3. Oh, and this is not a homework assignment Right!!!

Regards,
Dave
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
InbalAuthor Commented:
Edited text of question.
0
 
arnondCommented:
Inbal hi, (and also dhymes....)
since your new here, I think you must know that asking (and answering) homework questions here is considered unethical and may cause you and the experts who help you to be removed and banned from Experts-Exchange.
Please don't ask homework quastions.

The most we can do, is help you in problems you may have with code that you've already written. say, for example, that you wrote a program that should do what you want but it doesn't, then you can post the code here and we'll try to see what's wrong and make suggestion as to how you can solve the problem.

That's all for now,
Arnon David.
0
 
dhymesCommented:
arnond,

Yes and that is why I asked Inbal if this was a homework assignment and posted no answer so no need to through the warning at me.

Inbal, the answer to your question is yes you can write a c program that can do what you have explained, but since this looks like a homework assignment I will not post any code for you and will leave it up to you to take a shot at it. I can only assist you and make recomendations along the way the same way your teacher would.

Regards,
Dave

0
 
arnondCommented:
dhymes, I saw that you joined EE in Febuary, what I didn't see is that it was in 1999 and not 2000, so I appologize. sorry.

Arnon David.
0
 
RONSLOWCommented:
Likewise .. show us what you have so far and we can help with specific problems.

But we won't do your homework for you.

I suggest you delete the question and post other(s) with specific problems you ar ehaving.
0
 
deightonprogCommented:
Don't delete it, I'm working on it!
0
 
dhymesCommented:
dieghton,

Did you follow the text that we posted eariler with regards to (We are not supposed to provide ANSWERS to home work assignments????????), Hello, anyone home?
0
 
InbalAuthor Commented:
Hi Deighton,

Thank you very much for your answer.
Can you please add some short comments to the main loops (their purpose) for clarification?

For all the others: Just for your knowledge I'm not a student for a long time now,and this is definitely not homework, so stop your unnecessary comments.
0
 
deightonprogCommented:
#include<stdio.h>
                   #include<stdlib.h>

                   int perms(int m,int n)
                   {
                   int *numbers;
                   int *pointers;

                   int bend,icarry,c,x;

/* allocate array of pointers to the numbers */

                   if ((pointers = (int *) malloc(m))==NULL)
                   exit(1);


                   if ((numbers = (int *) malloc(n))==NULL)
                   exit(1);


                   for(c=0;c<n;c++)
                        pointers[c]=0;

                   do {
/*Step through the numbers as if in base n */
                   c=0;
                   icarry = 1;
                   do
                   {
                   pointers[c] += icarry;
/*Carry to be added to the next digit */
                   if (pointers[c]==n)
                   {
                   pointers[c]=0;
                   icarry=1;
                   }
                   else
                   {
/* carry = 0 */
                   icarry^=icarry;
                   }
                   ++c;

                   } while (c<m && icarry != 0);   /*until no carry or all digits procesed */
                   /*       printf("\n %i %i",pointers[0],pointers[1]);*/

/*if icarry == 1 then we have processed all base n numbers */
                   if (icarry == 0)
                   {
/* clear test array */
                   for(c=0;c<n;c++)
                   numbers[c]=0;

                   c^=c;
                   do
/*look for a repeated digit e.g. 1,1.2 - not to be output */
                   {
                   x = numbers[pointers[c]]+1;
                   numbers[pointers[c]]=x;
                   c++;
                   } while (c!=m && x!=2);

/* no repeats - so output */
                   if (x!=2)
                   {
                   printf("\n");
                   for(c=0;c<m;c++)
                   {
                   printf("%i",pointers[c]+1);
                   if (c<m-1)
                   printf(",");
                   }
                   }
                   }
                   else
                   {
/* clear up at enhd of routine */
                   free(pointers);
                   free(numbers);
                   return 1;
                   }
                   } while(1);    */ Infinite loop */

                   }


                   void main()
                   {
                   char x;
                   printf("\n\n-------------------\n");
                   x = perms(3,4);
                   printf("\n_____________________");

                   x= getch();
                   }
0
 
deightonprogCommented:
So good its not homework or a student exercise.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.