Factorial

M

Michael

Hi,

1. Could you please show me how to get the following print out in a
better way? If the number go to 100!

#include <iostream>

int main()
{

int a,b,c;

for (a=1; a<=3; a++)
for (b=1; b<=3; b++)
for (c=1; c<3; c++)
{
if ((a!=b) && (b!=c))
cout<<a<<' '<<b<<' '<<c<<endl;
}

return 0;
}

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

2. And, how to do a loop like a=5, 3, 7, 1, which is not continuous? I
need to use do while or while?

Thanks,
Michael
 
K

Kai-Uwe Bux

Michael said:
Hi,

1. Could you please show me how to get the following print out in a
better way? If the number go to 100!

#include <iostream>

int main()
{

int a,b,c;

for (a=1; a<=3; a++)
for (b=1; b<=3; b++)
for (c=1; c<3; c++)
{
if ((a!=b) && (b!=c))
cout<<a<<' '<<b<<' '<<c<<endl;
}

return 0;
}

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

2. And, how to do a loop like a=5, 3, 7, 1, which is not continuous? I
need to use do while or while?

Use a std::vector to hold the items you want to order in all possible ways.
Then use sort to put them in initial order. You can now iterate through all
possible arrangements using std::next_permutation (from <algorithm>).
Actual coding is left as an exercise.


Best

Kai-Uwe Bux
 
M

Markus Schoder

std::cout ... std::endl
Use a std::vector to hold the items you want to order in all possible
ways. Then use sort to put them in initial order. You can now iterate
through all possible arrangements using std::next_permutation (from
<algorithm>). Actual coding is left as an exercise.

Sounds like homework material to me so he probably cannot use a predefined
function.

To OP: Try to solve the problem recursively. Basically pick each element as
the first one in turn and prepend it to all permutations of the remaining
elements.
 
M

Michael

Kai-Uwe Bux said:
Use a std::vector to hold the items you want to order in all possible ways.
Then use sort to put them in initial order. You can now iterate through all
possible arrangements using std::next_permutation (from <algorithm>).
Actual coding is left as an exercise.


Best

Kai-Uwe Bux


I tried but it did not work. Could you please show me the code? Thanks!
 
K

Kai-Uwe Bux

Michael said:
I tried but it did not work. Could you please show me the code? Thanks!

The rules around here are: if something smells of homework, you go first.
Let us see what you have and then, we will help you getting it right. Post
a minimal complete program that shows the problem.


Best

Kai-Uwe Bux
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

Michael said:
I tried but it did not work. Could you please show me the code? Thanks!

Work it out on paper first. Try to organise your thoughts properly and
it will come to you. Even those of us who have been programming for
more than 20 years still have to sit down with paper and pencil to work
things out before jumping in and trying code.

If you don't have it clear in your head you're just programming at
random and you'll probably never find a solution. Sometimes you can be
lucky, but that's no way to learn :)


K
 
H

Howard

Michael said:
Hi,

1. Could you please show me how to get the following print out in a
better way? If the number go to 100!

#include <iostream>

int main()
{

int a,b,c;

for (a=1; a<=3; a++)
for (b=1; b<=3; b++)
for (c=1; c<3; c++)
{
if ((a!=b) && (b!=c))
cout<<a<<' '<<b<<' '<<c<<endl;
}

return 0;
}

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

FYI... That code does not match that output. The output doesn't show cases
where a==c. But the code doesn't test that a and c are unequal, so it would
print thing like "1 2 1" as well.

-Howard
 
M

Michael

Michael said:
I tried but it did not work. Could you please show me the code? Thanks!

I was a bit busy days ago. Now I get the answer. Yay! Thanks for your
solution
next_permutation :)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{

vector<int> x;

for (int t=1; t<4; t++)
x.push_back(t);

vector<int>::iterator y;

do
{
for (y=x.begin(); y!=x.end(); y++)
cout<<*y<<' ';

cout<<endl;
}
while(next_permutation(x.begin(), x.end()));


return 0;

}
 
J

Jerry Coffin

[ ... ]
vector<int>::iterator y;

do
{
for (y=x.begin(); y!=x.end(); y++)
cout<<*y<<' ';

FWIW, this loop is equivalent to:

copy(x.begin(), x.end(), ostream_iterator<int>(cout, " "));
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top