Solved

recursion

Posted on 2000-02-26
13
258 Views
Last Modified: 2010-04-15
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
Comment
Question by:Inbal
  • 4
  • 3
  • 3
  • +2
13 Comments
 

Author Comment

by:Inbal
ID: 2561336
Adjusted points to 100
0
 
LVL 2

Expert Comment

by:dhymes
ID: 2561509
Your question is not clear. Give example if m=3. Oh, and this is not a homework assignment Right!!!

Regards,
Dave
0
 

Author Comment

by:Inbal
ID: 2561543
Edited text of question.
0
 
LVL 3

Expert Comment

by:arnond
ID: 2561596
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
 
LVL 2

Expert Comment

by:dhymes
ID: 2562824
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
 
LVL 3

Expert Comment

by:arnond
ID: 2562841
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 10

Expert Comment

by:RONSLOW
ID: 2563327
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
 
LVL 18

Expert Comment

by:deighton
ID: 2564739
Don't delete it, I'm working on it!
0
 
LVL 18

Accepted Solution

by:
deighton earned 100 total points
ID: 2565262
#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
 
LVL 2

Expert Comment

by:dhymes
ID: 2566127
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
 

Author Comment

by:Inbal
ID: 2566396
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
 
LVL 18

Expert Comment

by:deighton
ID: 2568054
#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
 
LVL 18

Expert Comment

by:deighton
ID: 2568099
So good its not homework or a student exercise.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now