Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# recursion

Posted on 2000-02-26
Medium Priority
267 Views
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)

Thanks.
0
Question by:Inbal
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 3
• 3
• +2

Author Comment

ID: 2561336
0

LVL 2

Expert Comment

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

ID: 2561543
Edited text of question.
0

LVL 3

Expert Comment

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.

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

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

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

LVL 10

Expert Comment

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

ID: 2564739
Don't delete it, I'm working on it!
0

LVL 18

Accepted Solution

deighton earned 400 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

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

ID: 2566396
Hi Deighton,

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

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

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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn hoâ€¦
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address.Â This address might be address of another variable/address of devices/address of fuâ€¦
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 and use conditional statements in the C programming language.
###### Suggested Courses
Course of the Month8 days, 7 hours left to enroll