1. ## help is needen

hey there,
i'm new at c programming and got some very difficult homework.
No one around me succeded at solving this problem(pro coders and such), so i'll try my luck here.
The problem is a permutation problem, you need to print all the permutations of an array in certein length, for example- for the number 3 you will need to get-
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
now the fun part comes- you cant use any loops. at all. you need to do it only with recurtion. no other functions in the recurtion is allowed (you cant do recurtion inside recurtion and by that by-pass the loops prohabition). the only thing you may use is a print function which will print your array.

If someone is up for the challenge i will be more then happy to share my thoghts about it.

2. This is not the way to do your homework.
Solving problems is how you learn.
Sometimes you muck around for days and days.
And even then you might still have failed.

3. zach please don't teach me how to do my homework. i've tried for days and days. and have a close deadline. so im looking for all the help i can get.

4. Originally Posted by Yoav
zach please don't teach me how to do my homework. i've tried for days and days. and have a close deadline. so im looking for all the help i can get.
Then you should post the code you have written, so we can guide you. It is the policy of the forum, along with most others, to not write the code for you from scratch, but to comment on the code YOU have already written.

5. Hi Yoav!

I have only a simple Program with comments in german, but may be it can help you:

first the file permutieren.h

Code:
```#ifndef PERMUTATION_H_INCLUDED
#define PERMUTATION_H_INCLUDED

#include <iostream>

void zeichendoppeltest(std::string zahlwort,int *zaehler, int zeiza);
int zaehleperm(int zeiza);

class permutieren
{
public:
permutieren();
~permutieren();
int atoint(std::string zawo);
std::string itoa(int number);
void initwort(std::string *wort, std::string *bwort, int zeiza);
int pruefestring(std::string wort, std::string soll);
void zeichendoppeltest(std::string zahlwort,int *zaehler, int zeiza);
int zaehleperm(int zeiza);
void showperms(std::string zahlwort, int zeiza);
char permwort[26];

};

#endif // PERMUTATION_H_INCLUDED```
now permutieren.cxx
Code:
```#include <permutieren.h>

using namespace std;

#include <string.h>
#include <sstream>
#include <complex.h>

permutieren::permutieren()
{

char pwort[26]={
'A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X',
'Y','Z'};
for (int i = 0; i < 26; i++)
permwort[i] = pwort[i];

}
permutieren::~permutieren(){};

int permutieren::atoint(std::string zawo)
{
int rewer = 0;
std::istringstream b(zawo);
b >> rewer;
return rewer;
}

std::string permutieren::itoa(int number)
{
std::ostringstream buff;
buff << number;
return buff.str();
}

void permutieren::initwort(std::string *wort, std::string *bwort, int zeiza)
{
int slei, sleib;
char numchar[10] = {'0','1','2','3','4','5','6','7','8','9'};
for (slei = 1; slei <= zeiza; slei++)
*wort +=numchar[slei];

for (slei = zeiza; slei >= 1; slei--)
*bwort +=numchar[slei];
}

// Prüft String wort auf vorkommen der Zeichen in String soll, bei nichtübereinstimmung
// Rückgabewert ungleich Null sonst Null;
int permutieren::pruefestring(std::string wort, std::string soll)
{
int slei, sleib, lgeist, lgesoll, rewer;
std::string str1 ="";
std::string str2 = "";

lgeist  = wort.length();
lgesoll = soll.length();

for (slei = 0; slei < lgeist; slei++)
{
//cout << endl << "vor slei=" << slei << "   ";
if (rewer == lgesoll) break;
for (sleib = 0; sleib < lgesoll; sleib++)
{
str1 = wort.substr(slei, 1);
str2 = soll.substr(sleib, 1);

/// if (wort[slei] != soll[sleib])
if (str1.compare(str2) != 0)
{
++rewer;
//cout << "  wort=" << wort[slei] << "   soll=" << soll[sleib] << "   rewer=" << rewer << endl;
}
else
{
rewer = 0;
//cout << "* wort=" << wort[slei] << "   soll=" << soll[sleib] << "   rewer=" << rewer << endl;
break;
}
}
//cout << "    nach slei=" << slei << "  \n";
}

//cout << "E-soll=" << soll<< "  wort:" << wort <<  "  rewer: " << rewer  << "   \n\n";
return rewer;
}

/// untersucht den String ob ein Zeichen mehr als einmal vorkommt
void permutieren::zeichendoppeltest(std::string zahlwort,int *zaehler, int zeiza)
{
int slei, sleib, dummy = 0;
std::string str1 ="";
std::string str2 = "";

//cout << "Zahlwort=" << zahlwort  << "\n";

for (slei = 0; slei < zeiza; slei++)
{
for (sleib = slei + 1; sleib < zeiza; sleib++)
{
str1 = zahlwort.substr(slei, 1);
str2 = zahlwort.substr(sleib, 1);
if (str1.compare(str2) == 0)
dummy++;
//cout << "str1=" << str1 << "  str2:" << str2 <<  "  dummy: " << dummy  << "\n";
}
}
*zaehler = dummy;
//cout << "dummy: " << dummy  << "\n";

}

int permutieren::zaehleperm(int zeiza)
{
std::string wort, bwort, zahlwort;
int start, ende, slei, zaehler, nuze, perze;

initwort(&wort, &bwort, zeiza);

start = atoint(wort); //, 10);
ende  = atoint(bwort); //, 10);

cout << "\nAnzahl der Elemente.: "<< zeiza << endl << "Start bei Zahl......: "<< start << endl << "Ende bei Zahl.......: " << ende << endl;

cout << "Startzahl als String: " << wort  << endl;
cout << "Endezahl als String.: " << bwort << endl << endl;

cout << "start: " << start  << endl;
cout << "ende:  " << ende << endl << endl;

perze = 0; // zählt die Anzahl der dargestellten Zahlen

// Schleife von 12345 bis über 54321 weil im diesem Beispiel 5 unterschiedliche Zeichen
for (slei = start; slei <= ende; slei++)//ende
{
zahlwort = itoa(slei);     // , zahlwort, 10); // wandelt ganze Zahl in String um
zeiza = zahlwort.length(); // strlen(zahlwort); // Stellt fest, wieviele Zeichen die als String dargestellte Zahl hat
zaehler = 0;               // zählt, wie oft ein Zeichen im String vorkommt

// untersucht den String ob ein Zeichen mehr als einmal vorkommt
zeichendoppeltest(zahlwort, &zaehler, zeiza);

nuze = 0;

// durchsucht den String nach nicht erlaubten Zeichen
nuze = pruefestring(zahlwort, wort);

//cout << "*Permutation Nr.: " << perze << " = " << zahlwort <<  "   nuze= " << nuze  << "  zaehler=" << zaehler << "   ";

if ((nuze == 0) && (zaehler == 0))
{
perze++;
cout << "Permutation Nr.: " << perze << " = " << zahlwort << "   ";
showperms(zahlwort, zeiza);
}
}

return perze;
}

void permutieren::showperms(std::string zahlwort, int zeiza)
{
int slei, sleia, sleib = 0;
std::string zsymbol = "123456789";
std::string str1 ="";
std::string str2 = "";
//char permwort[8] = {'A','B','C','D','E','F','G','H'};
//std::cout << "\n"; //permwort[sleib];
for (slei = 0; slei < zeiza; slei++)
{
for (sleia = 0; sleia < zeiza; sleia++)
{
str1 = zahlwort.substr(slei, 1);
str2 = zsymbol.substr(sleia, 1);

if (str1.compare(str2) == 0)
{
sleib = sleia;
//std::cout << "str1=" << str1 << " str2=" << str2 << "   sleib=" << sleib <<"\n"; //permwort[sleib];
break;
}
}

std::cout << permwort[sleib];
}

std::cout << std::endl;
}```
and here main.cc
Code:
```/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-  */
/*
* main.cc
* Copyright (C) 2017 Josef Wismeth, germany, upper palatinate,
*
* permutieren4 is free software: you can redistribute it and/or modify it
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* permutieren4 is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "permutieren.h"

//using namespace std;

/* In C ist dies die Hauptfunktion */
int main()
{

int wastun = 9, zeiza, perze;
permutieren euler;

do
switch(wastun)
{
default:
std::cout << "Erzeugen von Permutierten Zahlenreihen" << std::endl;
std::cout << "Programm beenden.....................0" << std::endl;
std::cout << "Permutierte Zahlenreihe aufzeigen....1" << std::endl;
std::cout << "Permutierte Letterreihe aufzeigen....2" << std::endl;
std::cout << "Ihre Wahl: ";
std::cin >> wastun;
break;

case 0:
std::cout << "Programm beendet" << std::endl;
return EXIT_SUCCESS;
break;

case 3:
case 1:
if (wastun == 1)
{
std::cout << "Bitte Anzahl der Zahlen(3 bis 8) eingeben: ";
std::cin >> zeiza;
}

if ((zeiza < 3) || (zeiza > 8))
{
std::cout << "Falsche Anzahl von Elementen: "<< zeiza << std::endl;
wastun = 9;
break;
}

perze = euler.zaehleperm(zeiza);

std::cout << "Anzahl der Permutationen: " << zeiza << "! = " << perze << std::endl;
wastun = 9;
break;

case 2:
std::cout << "Bitte Wort (mindestend 3 maximal 8 Buchstaben Laenge) eingeben: ";
std::cin >> euler.permwort;
for (zeiza = 0; zeiza < 26; zeiza++)
if (euler.permwort[zeiza] == '\0'){
std::cout <<"Laenge des Wortes: " << zeiza << std::endl;
break;
}
wastun = 3;
break;

}while(wastun != 0);

return EXIT_SUCCESS;
}```
Here a little manual in english:

To exit the Program in main-menu choose 0.

To make a permutation choose 1:

Then you have the choice between 3 up to 8 elements were the
permutations will print on screen.

It was written by myself with anjuta on SUSE LINUX tumbleweed.
I think you can take also codeblocks or codelite an hope it will run on your system an wokts properly.

Sorry, i am to lazy changing this program into English.

6. @rusyoldguy

2) This is a C forum, NOT C++. Your code is a mix of pure C and C++, namespace, cout, etc....

3) This is an English based forum. The German names only confuse the reader. Don't be lazy.

7. Originally Posted by rstanley
@rusyoldguy

2) This is a C forum, NOT C++. Your code is a mix of pure C and C++, namespace, cout, etc....

3) This is an English based forum. The German names only confuse the reader. Don't be lazy.
Having passed many years in academia, teaching, I object to what is happening here. By responding to the student's request, you are taking things out of the hands of his professor. The professor is the one the student should turn to, moreover because the professor needs to know where the difficulties lie. Or where the initial approach is wrong. That is an essential part of the professor's job. His job isn't to wait till the polished answers land on his desk. One step further and the student will be buying the answer. This really is no good at all.

8. Originally Posted by zach
Having passed many years in academia, teaching, I object to what is happening here. By responding to the student's request, you are taking things out of the hands of his professor. The professor is the one the student should turn to, moreover because the professor needs to know where the difficulties lie. Or where the initial approach is wrong. That is an essential part of the professor's job. His job isn't to wait till the polished answers land on his desk. One step further and the student will be buying the answer. This really is no good at all.
@zach:

Are you criticizing my response to @rusyoldguy, or to the response by @rusyoldguy???

9. Originally Posted by rstanley
@zach:

Are you criticizing my response to @rusyoldguy, or to the response by @rusyoldguy???
I would have thought that my English was quite clear. This is not the right thing to do. Not right from the point of view of the student, not right from the point of view of his professor, not right form the point of view of his co-students.

10. Originally Posted by Yoav
zach please don't teach me how to do my homework. i've tried for days and days. and have a close deadline. so im looking for all the help i can get.

11. Code:
```void Permute(int arr4[],int N,int i,int j){
int temp;
if(i==N){
print1(arr4,N+1,N+1);
return;
}
if(i!=j){
temp=arr4[i];
arr4[i]=arr4[j];
arr4[j]=temp;
Permute(arr4,N,i+1,j);
temp=arr4[i];
arr4[i]=arr4[j];
arr4[j]=temp;
return;
}
Permute(arr4,N,i+1,j+1);
Permute(arr4,N,i,j+1);
if(!j){
Permute(arr4,N,i,j+2);
print1(arr4,N+1,N+1);
}
}```
A small explantion for the code-
what I was thinking about is to define an array and to play with the indexes, the array definition is happening in a different function (under the work limitation), the print function is for printing the array out side the recurtion problem. my problem with this is that i wont get the last permutations, for example for the user input 3 ill get-
1 2 3
1 3 2
2 1 3
2 3 1
3 2 1
and that's it.
also the last permutation is wrong and suppuse to be "3 1 2".
after debugging ive found my problem, yet dont know how to fix it.
also if the user enters 4, this become a lot more problematic

12. Originally Posted by zach
I would have thought that my English was quite clear. This is not the right thing to do. Not right from the point of view of the student, not right from the point of view of his professor, not right form the point of view of his co-students.
Zach, my professor is quite aware of this situation, and also the people who practice this c class. they dont show any signs of care and throw us to the deep water.
ive never asked for help in any of my other courses (finished second year in my degree) and with this one- cant find any solution.
if you were a proffesor- you should know that your students have other things to do exept sitting and programming all day and night, and if you give them homework- it should match there skills, no one in my class (60+ people) had any succes with this work. so this is my last resort.
i was sitting with my father on this work (he owns a masters at computer sicence and math) and he also didnt had any succes with it. belive me when i say ive tried my best with this work.

13. For this kind of "the instructor has a very specific solution in mind and wants students to stumble upon it" kind of questions, I'm inclined to say just do your best, and if you cannot find a solution, it's okay to give up: in the real world you typically won't have artificial restrictions, so just learn from the "model answer" and move on.

It's a bit like those "think out of the box" poor interview questions that test whether the candidate knows the answer rather than whether the candidate has the general skills they are looking for.

(That said, permutation stuff tend to have a more recursive than iterative feel.)

14. Maybe you are trying to implement the solution without trying to understand it. It is common...

1st: How can you implement a loop recursively?

Example:
Code:
```void f(int N) { if ( N == 0 ) return; printf( "%d\n", N ); f(--N); }
//
// This is the same as 'for ( N = 10; N > 0; N-- ) printf( "%d\n", N );':
f(10);```
2nd: Did you tried to draw what you should be doiing?

Exemple... Conside the sequence A,B,C:

Notice, in the first pass, You have the each item of the sequence as the first item. Then, this item is fixed and you do the same for the remaining itens in the other passes...

PS: The argument of "students have other things to do exept sitting and programming all day" is FALSE, because students should try to sit and programming THE SMART way... If you are not able to solve a simple (or a mildly complex one) problem don't blame anyone else except yourself.

15. Just to extend on what flp said - You need to do a little research on what algorithm that you wish to use. This will be the case when you are doing future assigments, or coding for a company.

Step 1 should always be a review

You can see that there are different ways to solve your problem, but there is a really obvious one as well...

Once you have done a quick review, create a small program and try to recreate the results based on the algorithms you've seen. If you can't find anything, then you need to get pen and paper and work out an algorithm before you do any coding - 3 coins is also a good idea.