[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

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.
0
Inbal
Asked:
Inbal
  • 4
  • 3
  • 3
  • +2
1 Solution
 
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
 
InbalAuthor Commented:
Edited text of question.
0
Managing Security & Risk at the Speed of Business

Gartner Research VP, Neil McDonald & AlgoSec CTO, Prof. Avishai Wool, discuss the business-driven approach to automated security policy management, its benefits and how to align security policy management with business processes to address today's security challenges.

 
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
 
deightonCommented:
Don't delete it, I'm working on it!
0
 
deightonCommented:
#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
 
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
 
deightonCommented:
#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
 
deightonCommented:
So good its not homework or a student exercise.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now