Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   permutations II (http://www.velocityreviews.com/forums/t681527-permutations-ii.html)

Bill Cunningham 04-26-2009 10:48 PM

permutations II
 
Well this is as far as I've got with my permutations program if you call
it that. This is what I want it to look like though. With a prompt. I have
to be able to read the number of characters and sift them around. Any hints?

#include <stdio.h>
#include <limits.h>

int main(void)
{
char input[CHAR_MAX];
printf("enter word-> ");
fflush(stdout);
fgets(input, CHAR_MAX, stdin);
}

Bill



osmium 04-27-2009 12:53 AM

Re: permutations II
 
"Bill Cunningham" wrrote:

> Well this is as far as I've got with my permutations program if you
> call it that. This is what I want it to look like though. With a prompt. I
> have to be able to read the number of characters and sift them around. Any
> hints?
>
> #include <stdio.h>
> #include <limits.h>


Don't include things, such as limits.h, that you don't use. It's confusing.

> int main(void)
> {
> char input[CHAR_MAX];
> printf("enter word-> ");
> fflush(stdout);
> fgets(input, CHAR_MAX, stdin);
> }


Decide whether you want to do it recursively or not. Then go back to last
weekend's hints and follow the ones that pertain to that approach. I think
most seasoned programmers would favor the recursive approach but YMMV. I
think there might be easier ways to get your arms around recursion. Have
you written ANY working program that uses recursion? If not, I would look
for something a bit simpler to do first.



Eric Sosman 04-27-2009 01:03 AM

Re: permutations II
 
osmium wrote:
> "Bill Cunningham" wrrote:
>
>> Well this is as far as I've got with my permutations program if you
>> call it that. This is what I want it to look like though. With a prompt. I
>> have to be able to read the number of characters and sift them around. Any
>> hints?
>>
>> #include <stdio.h>
>> #include <limits.h>

>
> Don't include things, such as limits.h, that you don't use. It's confusing.


He's using CHAR_MAX. There's no good reason why he should
be using it, but using it he is.

>> int main(void)
>> {
>> char input[CHAR_MAX];
>> printf("enter word-> ");
>> fflush(stdout);
>> fgets(input, CHAR_MAX, stdin);
>> }

>
> Decide whether you want to do it recursively or not. Then go back to last
> weekend's hints and follow the ones that pertain to that approach. I think
> most seasoned programmers would favor the recursive approach but YMMV. I
> think there might be easier ways to get your arms around recursion. Have
> you written ANY working program that uses recursion? If not, I would look
> for something a bit simpler to do first.


--
Eric Sosman
esosman@ieee-dot-org.invalid

nick_keighley_nospam@hotmail.com 04-27-2009 08:56 AM

Re: permutations II
 
On 26 Apr, 23:48, "Bill Cunningham" <nos...@nspam.invalid> wrote:

> * * Well this is as far as I've got with my permutations program if you call
> it that. This is what I want it to look like though. With a prompt. I have
> to be able to read the number of characters and sift them around. Any hints?


define "read the number of characters"
define "sift them around"


<snip>

Bill Cunningham 04-27-2009 09:45 PM

Re: permutations II
 

<nick_keighley_nospam@hotmail.com> wrote in message
news:d1750541-3f9b-49ce-ac4a-a2c8d550b660@s1g2000prd.googlegroups.com...

define "read the number of characters"
define "sift them around"


<snip>

I think I need a char** definition here. Maybe I could replace CHAR_MAX
with 40 or something like that. I'll post something soon that I believe
might get me closer.

Bill



CBFalconer 04-27-2009 11:18 PM

Re: permutations II
 
osmium wrote:
> "Bill Cunningham" wrrote:
>

....snip ...
>
>> #include <stdio.h>
>> #include <limits.h>

>
> Don't include things, such as limits.h, that you don't use. It's
> confusing.
>
>> int main(void) {
>> char input[CHAR_MAX];
>> printf("enter word-> ");
>> fflush(stdout);
>> fgets(input, CHAR_MAX, stdin);
>> }


Without <limits.h>, how do you suggest discovering the value of
CHAR_MAX? :-)

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


Bill Cunningham 04-27-2009 11:55 PM

Re: permutations II
 

<nick_keighley_nospam@hotmail.com> wrote in message
news:d1750541-3f9b-49ce-ac4a-a2c8d550b660@s1g2000prd.googlegroups.com...

define "read the number of characters"
define "sift them around"


<snip>

Now I need to understand this factorial formula right first. If a word
is 7 letters long is this how it is to be figured?

7*6*5*4*3*2*1=possible permutations?

Bill



luserXtrog 04-28-2009 12:47 AM

Re: permutations II
 
On Apr 27, 6:55*pm, "Bill Cunningham" <nos...@nspam.invalid> wrote:
> <nick_keighley_nos...@hotmail.com> wrote in message
>
> news:d1750541-3f9b-49ce-ac4a-a2c8d550b660@s1g2000prd.googlegroups.com...
>
> define "read the number of characters"
> define "sift them around"
>
> <snip>
>
> * * Now I need to understand this factorial formula right first. If a word
> is 7 letters long is this how it is to be figured?
>
> 7*6*5*4*3*2*1=possible permutations?
>


Alright. This would be easier in person with paper, but here goes.
The usual way to tackle this sort of thing is build up a sense
of the pattern involved. It's rather like constructing an inductive
proof; you start with the simplest example; then the next one;
then notice how the two differ and make a prediction about how
each case will differ from the previous; then you examine the third
example and check how it jives with the prediction. If your pattern
applies no matter how many cases you can produce (in a proof, this
is the inductive step), you're home free.

With permutations, you're likely to confuse yourself by not
sufficiently distinguishing between the set of source elements
(which here is a string) and the ordered selection that needs
to be generated (which also happens to be a string). On paper,
this can be made more clear by writing the source vertically
and constructing your permutations horizontally.

Example. Permute the single character 'a'.

source
a
permutations: a

Example. Permute the two characters 'a' and 'b'.

source
a
b
permutations: ab ba

With me so far?
Now you may not be able to fully enunciate the pattern at this
point. Keep going and something will have to click eventually.

Example. Permute the characters in "abc".

source
a
b
c
permutations:
abc acb
bac bca
cab cba

Now a pattern has emerged. The pattern was already present in
the way I (consciously or otherwise) chose to write these down.
I started by putting something in the leftmost slot and then
performing the 2-character permutation on the remaining slots
with the remaining elements.

I'd recommend you skip in-place manipulations for now.
Make a copy to use as the source set and make an empty
array to be filled in with characters selected from the
source. When you fill a slot erase the character from
the source. When the source is empty, your string should
be filled and you can blank out the string and refill
the source from the *real* source (which you've wisely
left unmodified for just this purpose). It'll mean
3 character arrays instead of just one, but if they're
named sensibly this should help you keep them straight.

hth
--
lxt

Barry Schwarz 04-28-2009 01:12 AM

Re: permutations II
 
On Mon, 27 Apr 2009 17:45:34 -0400, "Bill Cunningham"
<nospam@nspam.invalid> wrote:

>
><nick_keighley_nospam@hotmail.com> wrote in message
>news:d1750541-3f9b-49ce-ac4a-a2c8d550b660@s1g2000prd.googlegroups.com...
>
>define "read the number of characters"
>define "sift them around"
>
>
><snip>
>
> I think I need a char** definition here. Maybe I could replace CHAR_MAX
>with 40 or something like that. I'll post something soon that I believe
>might get me closer.


Your just guessing again. If you don't know how you intend to use it,
you don't need it.

--
Remove del for email

Barry Schwarz 04-28-2009 01:12 AM

Re: permutations II
 
On Mon, 27 Apr 2009 19:55:47 -0400, "Bill Cunningham"
<nospam@nspam.invalid> wrote:

>
><nick_keighley_nospam@hotmail.com> wrote in message
>news:d1750541-3f9b-49ce-ac4a-a2c8d550b660@s1g2000prd.googlegroups.com...
>
>define "read the number of characters"
>define "sift them around"
>
>
><snip>
>
> Now I need to understand this factorial formula right first. If a word
>is 7 letters long is this how it is to be figured?
>
>7*6*5*4*3*2*1=possible permutations?


Only if the letters are distinct. There are two permutations of "ab"
but only one unique permutation of "aa".

--
Remove del for email


All times are GMT. The time now is 07:12 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.