print string factorial

J

jkelt46

(e-mail address removed) said:
On Apr 5, 10:05 pm, Richard Heathfield <[email protected]>
wrote:
(e-mail address removed) said:
On Apr 3, 10:29 am, Richard wrote:
(Actually, Richard Heathfield wrote. The reason for
distinguishing between the two is that, in comp.lang.c, Richard
Heathfield is me, whereas Richard (with no surname) is someone
else with whom I have no desire to be confused (and I'm sure the
feeling is mutual, which is why it's rather odd that he doesn't
use his surname - but then he's a rather odd fellow).
I am sorry about that.  Sometime I remove too much.
Having got that out of the way, let's take a look at your
problem:
<pseudocode snipped>
I am sorry it took so long to post back.  I had unexpected
house guests.
Ouch. I sympathise.
I took a stab at solving this problem and this is what I
have come up with based on the above pseudo code:
void jpermute(char *data, int p, int n) {
int i;
char temp;
if (p == n)
printf("%s\n", data);
else {
for (i=p; i<n; i++) {
temp = data[p];      //1
data[p] = data;   //2
Here's your problem. You've just doubled up some of the data.
jpermute(data, p+1, n);
data[p] = temp;      //3
}
}
}
and this is my output:
abc
acc
Right.
<snip>
Here's one that works.
void Permute(char *Perm,
size_t n,
size_t unchanged)
{
size_t outer = 0;
size_t inner = 0;
int temp = 0;
if(unchanged < n)
{
for(outer = unchanged; outer < n; outer++)
{
temp = Perm[outer];
/* NOTE: once you understand the following
loop, you can replace it with memmove */
for(inner = outer; inner > unchanged; inner--)
{
Perm[inner] = Perm[inner - 1];
}
Perm[unchanged] = temp;
Permute(Perm,
n,
unchanged + 1);
/* NOTE: once you understand the following loop,
you can replace this one with memmove too */
for(inner = unchanged; inner < outer; inner++)
{
Perm[inner] = Perm[inner + 1];
}
Perm[outer] = temp;
}
}
else
{
printf("%s\n", Perm);
}
}
After a quick read shouldn't temp a char?
Yes, you're probably right. It doesn't actually matter except in
some very odd circumstances that almost certainly don't apply on
your architecture (basically, sizeof(int) == 1 (making char at
least 16 bits wide) AND char is unsigned AND at least one of the
characters uses the top bit - but yes, better safe than sorry.

We are working PCs for the first half of the semester.  Then we are
moving to a AXP running VMS for the second half.  So code portability
is good as we will be using some of the code we wrote on the PC on the
AXP too.
Going back to the memmove for a minute.  I have seen memmove examples
where in the third argument they add *sizeof(int) or *sizeof(char)
like the following:
memmove(u + 1, u, 5*sizeof(int));
Do you have to add the *sizeof?  Is this one of the reasons my memmove
didn't work?

You have to tell memmove() the correct number of bytes to copy. If
sizeof(int) is greater than 1, and you want to copy 5 ints, but you
tell memmove() to only copy 5 bytes, I predict that you will be
unhappy with the result.

The expression that identifies how many bytes to copy can be written
many different ways, but will often involve sizeof(), though it's
usually best to use "sizeof expression" for some appropriate
expression rather than "sizeof(type)".  For instance, if *u has the
type 'int', then it's probably best to use "sizeof *u". Excecption:
sizeof(char) is 1, so the sizeof(char) unsually both can and should be
dropped.


Thank you for the clear explanation. It explains why some code I
found has the *sizeof(char) and others don't.

Thanks,
 
C

CBFalconer

.... snip ...


I am trying to write the code myself. Once I knew what I was
looking for I found about a dozen different examples on the
Internet that I could have turned in as my assignment. Some
used recursion and others iteration. However that is cheating
and possibly copyright infringement. Yes the code I turned in
is similar to code that Richard Heathfield posted, but it is
not exactly the same and I understand the code. This means I
can explain what it is doing if challenged. OK I admittedly
don't understand how to replace the for loops with memmoves
but as I said we haven't gotten to that in class yet. Why pay
$$$$$ for a degree and not learn anything???

No sweat. I just asked a question.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top