I wish to ask how can permu and input has 4 size when dol is 0 ,1, 2 ,'\0'
because this would mean that it can fit in 4 charecters with null .
then null charecter should be at position 4 i.e permu[4]='\0';
Please correct if I am wrong?

I will take some more time understanding the explaination given by Lerner.

I understood Lerner ,it means you are interchanging the positions by all combinations . This is the thing which I did earlier but did not find much success by drawing the array with letters in different positions and getting combinations.
I need to develop code for accessing these positions with for loops in the given series.
I thought swapping would be enough, but here we are also trying to swap between 2 consecutive positions of the permuted word to get the position combinations. Is not it?

The working code that was in #9 is working code but I need to develop original code with my effort and your help, if I just mug up the solution , what is the use of it .

Right?:)

I am on the solution with a positve approach now . With your sincere help and my effort I will make it through. I will not lose paitence

Bear with me ,
hinduengg

To JRM,
line 26 prints the string with alterations automatically regarding that and that s why have a question too with thinking sign!

Yeah, I would think that it would only print the first character of the array.
Also , as presented, your code does not compile.
you are missing the scoping operator std:: prefix or you could use
the using namespace statement.

Also if it's C++ you are calling the wrong includes and I don't think any are needed beyond <iostream> (loose the "h").

I pulled the getch() with no ill effects.
What compiler are you using? I have gcc and it did not like that code AT ALL.

I wish to ask how can permu and input has 4 size when dol is 0 ,1, 2 ,'\0'
because this would mean that it can fit in 4 charecters with null .
then null charecter should be at position 4 i.e permu[4]='\0';
Please correct if I am wrong?

actually, '\0' would be @ permu[3]

btw... i'm using DEV... maybe that influenced some errors...

Well its not that my code is wrong , actually I use turbo C++ which is surely outdated in today's world. It still accepts the old rules and syntaxes applied in C++ , which have been changed since 1998. I openly admit that our college still loves old setups .

But no use of complaining to the authorities , they will never listen . Perhaps after my intermediate I would adopt the newer modified version of C++.
I will have to first learn the old setup for exams and perhaps the new one for software engineering.

Thank you for permu[3]='\0' answer.
It means that if an array has 100 then we can utilise space upto 99 charecters - o to 99 and then '\0'
Nichito puts and cout both do the same function in C++ atleast in my program . I would like to request you to tell whether this statement

cout<<per<<endl; OR puts(per) in your code print charecters or the modified word on new lines . I think it prints changed words but how does it happen is a mystery.

Please solve the trivial query
Any help would be appreciated

From.
hinduengg:)

Well its not that my code is wrong , actually I use turbo C++ which is surely outdated in today's world. It still accepts the old rules and syntaxes applied in C++ , which have been changed since 1998. I openly admit that our college still loves old setups .

Well, IMHO if you are PAYING good money for an education the university should give you a proper one! Learning outdated/obsolete methods is worthless. Can you tranfer to better school?

Also, if you have your own computer, geo to www.codeblocks.org for a very good and FREE IDE.

also get the mingw gcc compiler to work with it. The thing keeps me honest! Also FREE.

You code as written will not fly in any modern commercial setting.
In fact, that gets() is depreciated. It works but is "dangerous". How do I know? My compiler told me!

I would highly recommend that you work with modern tools like these open source professional quality programs i mentioned.

Why learn to write code that won't compile by today's standards?

BTW, i wrote a version of tis that uses char* instead of arrays (much easier). They don't need to be pre-allocated either.

I stuffed the the pointers with cin.getline(a, 100, '\n').

commented: I have stock shares with gets(), when did it depreciate? :( +12

>>It means that if an array has 100 then we can utilise space upto 99 charecters

That's correct so farorrect so far

>> o to 99 and then '\0'

but that's not quite right. If input is declared to have memory for 100 char, as indicated below:

char input[100];

If none of the char in input are the null char, then input is just a char array, not a string. If the last char stored in input is a null char (actually it's a little more technical than that, but that's close enough for starting out) then input is a string. Input could hold an empty string, which is the same as having a null char at input[0], a string of length 3---meaning visible char (at input[0], input[1], input[2]) and a null char at input[3], or a string up to size 99, with the last element, input[99] being the null char.

To repeat, with input declared as above, the indexes of input would range from 0 - 99----that's 100 elements. In a string with size 99 (that is, there are 99 visible char) the first visible char would be at input[0], the last visible char would be at input[98], and the null char would be at input[99].

That means the maximal size, or length, of the string is always one less than the available memory that's been declared, since you always have to have room for the null char at the end.

>

If none of the char in input are the null char, then input is just a char array, not a string. If the last char stored in input is a null char (actually it's a little more technical than that, but that's close enough for starting out) then input is a string.

AHA!
So that probably explains why this works, even though "per" was declared as a character array:

cout<<per<<endl;

Am I on the right track?
Do have good URL for further reading?

AHA!
So that probably explains why this works, even though "per" was declared as a character array:

cout<<per<<endl;

Am I on the right track?
Do have good URL for further reading?

The reason it works is because operator<<() is overloaded for all the built-in types to work with std::ostream - including char*. Although as Lerner explained, a character array with no '\0' terminating character doesn't constitute a string, and shouldn't be expected to work the same way.

With operator overloading, its also possible to extend the functionality further with your own types - Have a look here for a little more on operator overloading
http://www.parashift.com/c++-faq-lite/operator-overloading.html

WOW! Thank you Lerner, JRM , Bench .

You are great for you removed such an important doubt. I had been confused about this but now its pretty clear to me and perhaps in my code thats why its prints elements of char array each time the loop of x runs so that at the end of 'for' loop we get the modified char array printed till the null charecter is reached at the end of 'for' loop.

Super duper!

Thankyou JRM for the advice but the transfer thing is not quite possible . Never mind in a year or two I would be doing software engineering , my dream . I would now not stretch the thing too far . The program is right now my priority .

I am in struggle with the working code, I would post the new thing when its ready , I am trying Lerner's method instead of plain swapping of the letters.:)

Thank you,
hinduengg

Here a hint;
use more or less the same logic as in my original description, with a simplification of using only ONE chr*

You only need two loops. One for the number of
"places" and another to move letters through those places.

I suppose there are other ways to do it but might require more code.

I get a n! output with no duplicates to clean up.

you mean that you got the output with a code
do i need 1 nested loop and nothing more complication

you mean that you got the output with a code
do i need 1 nested loop and nothing more complication

Yes, only one nested loop gets it done.
Think of it as a methodical/mechanical non-random shell game.
Each letter must move through the other positions. Display each time it moves.

Maybe that reference is too local? ...?

Did you try swapping in nested loop ?:)

What is meant by mechanical random shell game?. I did not understand this hint , please enlighten me.:)

Thank you,
hinduengg

What is meant by mechanical random shell game?. I did not understand this hint , please enlighten me.:)

Thank you,
hinduengg[/QUOTE

A shell game is an American urban "hustle" (AKA con-game, AKA rip-off). The con man running the game has three cups and a marble (for example). He show you the marble , places it under one of the cups, then shuffles the the cups very quickly. The "mark" (AKA connned customer) bets that he knows where the marble is after the shuffle. In reality, the con-man game runner "palms" the marble, so the "mark" loses his money.

Soooo, after that digression, i only meant that the letters move in a "bucket brigade" fashion though the string!

BTW "bucket brigade" is a common use term from the early days of fire fighting where buckets were filled at the well or tanker and passed man to man in a human chain until the last man threw the water on the fire.

Sorry, sometimes i forget that this is the WWW.

Also I discovered that my program fails with more than 3 letters, primarily because of the fast progression of n!
You see for 3 letters, n! = 6 .A 3 X 3 loop will yield 9 iterations which is adequate. 4! = 24. A 4X4 loop only provides 16 iterations.

I am now experimenting with adjusting the loops by using a calculated n! value. It's close, but not quite there yet.

Thank you JRM!

This is the code that gives me only three permutations of string " joy"
I am not able to swap the values because I cannot hold th position of y in the 'if' loop.

temp=a[x];
a[x]=a[x++];
a[x++]=temp;

HELP ME!

#include<iostream.h>
#include<conio.h>  // these headers need to be modified as per new rules in c++
#include<string.h>
int main()
{
   char a[100];
   char per[100];
   cout<<" word";
   cin.get(a,100);
    stcpy(per,a);
  
int l=strlen(per);
for(int x=0;x<l;x++)
 for(int y=0;y<l;y++)
 {
   if(x!=y)
  cout<<per[y];
  }
  cout<<per[x]<<endl;
}
getchar();
return 0;
}

Help needed in getting hold of different ( index )values of x so that I could swap them up to print 6 combinations instead of 3 .

if you just print per[x]in if loop you will get
oy// 1,2
jy //0,2
jo //0,1
The current output is
oyj
jyo
joy

and my output greatly requires
012
021
102
120
210
201

With lots of hope,
hinduengg

I am not able to swap the values because I cannot hold th position of y in the 'if' loop.
temp=a[y];
a[y]=a[y++];
a[y++]=temp;

int main()
{   
  char a[100];   
  char per[100];   
  cout<<" word";   
  cin.get(a,100);   //use cin >> a; instead, or 
                    //if you want to include 
                    //whitespace in a then use
                    //cin.getline(a, 99); 

  stcpy(per,a);     //delete this, not needed

  int l=strlen(per); //never use el('l') as a 
                     //variable name, it looks
                     //like the numeral one.
                     //use len, it's only 2 
                     //extra key strokes and it's
                     //a lot safer

  for(int x=0;x<l;x++) 
                        //should have { here.
   for(int y=0;y<l;y++) 
   {   
     if(x!=y)         //exclusionary statement
       cout<<per[y];  //you are only printing a 
                      //a single char here.
   }  
   cout<<per[x]<<endl;
  }
   getchar();
   return 0;
}

This program uses two level nested loop. This
could handle a 2 letter input.

Now look at this and try to understand the difference.
When you understand the differences then extend it
to a three level nested loop.

int main()
{  
  const int MAX = 2;  //maximum number of visible char

  char a[MAX + 1];   
  char per[MAX + 1];   

  cout<<"enter a word with up to " << MAX << " letters";   
  cin >> a;

  int len = strlen(a); 
  cout << len << endl;

  cout << "the permutations are: " << endl;

  for(int x = 0; x < len; x++) //controls per[0]
  { 
   per[0] = a[x];
   for(int y = 0; y < len; y++) //controls per[1]
   {   
     if(x!=y)              //exclusionary check
       per[1] = a[y];
   } 
   per[2] = '/0';    //make per a string;
  
   cout << per << endl;  //display per
  }
   getchar();
   return 0;
}

If you don't want to print out per as a string then
you could do something like this, which prints each
letter in each permutation as it is identified by
each loop:

int main()
{  
  const int MAX = 2;  //maximum number of visible char

  char a[MAX + 1];      

  cout<<"enter a word with up to " << MAX << " letters";   
  cin >> a;

  int len = strlen(a); 
  cout << len << endl;

  cout << "the permutations are: " << endl;

  for(int x = 0; x < len; x++) //controls per[0]
  { 
   cout << a[x];
   for(int y = 0; y < len; y++) //controls per[1]
   {   
     if(x != y)              //exclusionary check
       cout << a[y];
   } 
   cout << endl;
  }
   getchar();
   return 0;
}

Thank you, thank you Lerner . I will try the program on my compiler to see what is its output. Or please could you tell me what is the difference between the last code of yours and mine .

What is its output? please tell . I tried mine one for atleast 1 hour but did not anything useful. May be your code would rectify me. Please sir . tell me the difference coz I am confused!

What is meant by exclusionary check - that is not needed but why so?
Please help.:)

Compile one of the programs and run it with the exclusionary statement active, then run it with exclusionary statement removed.

Enter input such that a is the word "my" in both runs of the program.

With the exclusionary statement active you should get output like:

my
ym

With the exclusionary statement commented out you should get:

mm
my
ym
yy

because the exclusionary statement prevents you from using the same element of the input more than once in the output. The same priniciple holds for longer versions of input but you need to extend the nested loop structure and extend the exclusionary statement to account for the longest string you want to allow as input.

I got that point but the code at present does not give 6 permutations as needed this means we neend to use 3 nested loops inside x = 0 to len-1 . Okay I will try it tomorrow for right now it is 12:29 am at my place . I would post the thing that I have got here to clear my doubts.

Thank you so much,
hinduengg

Thank you JRM!

This is the code that gives me only three permutations of string " joy"
I am not able to swap the values because I cannot hold th position of y in the 'if' loop.

temp=a[x];
a[x]=a[x++];
a[x++]=temp;

HELP ME!

#include<iostream.h>
#include<conio.h>  // these headers need to be modified as per new rules in c++
#include<string.h>
int main()
{
   char a[100];
   char per[100];
   cout<<" word";
   cin.get(a,100);
    stcpy(per,a);
  
int l=strlen(per);
for(int x=0;x<l;x++)
 for(int y=0;y<l;y++)
 {
   if(x!=y)
  cout<<per[y];
  }
  cout<<per[x]<<endl;
}
getchar();
return 0;
}

Help needed in getting hold of different ( index )values of x so that I could swap them up to print 6 combinations instead of 3 .

if you just print per[x]in if loop you will get
oy// 1,2
jy //0,2
jo //0,1
The current output is
oyj
jyo
joy

and my output greatly requires
012
021
102
120
210
201

With lots of hope,
hinduengg

your loops are set up in such a way that you will NEVER get more than a 2 character output. You only have per[y] and per[x] which are one character EACH.
Are you doing this on paper?
Your code still makes my complier angry!
Use the loops to move the selected letter though the string positions.

Hold the "selected" letter with something like: char hold = per[0], before entering the second loop .
here is what happens to j: then o is next up for shifting, etc, etc, etc...
joy
ojy
oyj

I simplified what you posted, but you will need to supply the underlying logic.

All this does is spread a whole lot of "joy"

 char  per[100];
   cout<<" word" << endl;
   cin.getline(per,100);
   int l = strlen(per);

    for(int x=0;x<l;x++)
    {
        for(int y=0;y<l;y++)
        {
            if(per[x]!=per[y])

            cout << per << endl;

        }
    }

To Lerner,

I tried your programs , they give the same output as my original one posted before yours. Could you help in atleast in hinting how to form the third loop to control per[3] if not the code for it .

I am trying this problem for the last 1 week but no concrete solution has come up my way to solve the problem . I am not saying that nobody helped but the solution yet is far away from me .

To JRM

could you post the code that can print atleast 6 combinations so that i can understand the problem better . Never mind if its wrong. There will be something to look forward to .

Thanking you both,
hinduengg

Try this:

Input is a string. The string is a char array. Each letter in the string can be accessed by using it's index (which is an int) within the [] operator. Each index can be used once and only once in each permutation so any index used before the current loop will have to excluded. Each loop can access each index in Input and just mechanically loops for zero to length of input minus one (or zero to less than len). Each letter in Permu is controlled by a loop in the nested loop construction and will be stored in appropriate index of permu as it is developed. Indexes will be named indexX where X is the index value.

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
  char input[] = "joy";
  char permu[4];
  int len = strlen(input);

  permu[3] = '\0';

  for(int index0 = 0; index0 < len; ++index0)
  {   
    //there is no previous loop so no exclusions are necessary
    permu[0] = input[index0];  
    for(int index1 = 0; index1 < len; ++index1)
    { 
      //index1 can't be same value as index0
      if(index1 != index0)
      {
        permu[1] = input[index1];  //this is permu[1];
        for(int index2 = 0; index2 < len; ++index2)
        {
          //index2 can't have value of either index0 or index1
          if (index2 != index0 && index2 != index1)
          {
            permu[2] = input[index2]; //this is permu[2]       
                        
            cout << permu << endl;
          }
        }
      }
    }
  }
  return 0;
}

Wow Lerner :icon_cheesygrin:your solution is really superb, awesome , mind blowing . Now I would make the whole code and then post it. I hope that although it is little lengthy(for 7 loops) but still the code is very very easily undestood by me !:)

Thank you so much,
hinduengg

Dump the loops, what you need is a recursive algorithm which would work for arbitrary lengths of arrays. One of the way is using 'Heap Permute'. Here is a sample program which you can easily adapt to use with C style strings:

// Untested

const int SIZE = 4;
static int counter = 1;

void swap(int arr[], int one, int two)
{
    int tmp = arr[one];
    arr[one] = arr[two];
    arr[two] = tmp;
}

void print(int arr[])
{
    for(int i = 0; i < SIZE; ++i)
    {
        cout << arr[i] << "  ";
    }
    cout << " ------->  " << counter++;
    putchar('\n');
}

void permute(int arr[], int size)
{
    if(size == 1)
    {
        print(arr);
    }
    else
    {
        for(int i = 0; i < size; ++i)
        {
            permute(arr, size - 1);
            if(size % 2 == 1)
                swap(arr, 0, size - 1);
            else
                swap(arr, i, size - 1);
        }
    }
}

int main()
{
    int myArr[SIZE] = {1, 2, 3, 4};
    permute(myArr, SIZE);
    cin.get();
}
commented: a very smart man indeed +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.