473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Factorial

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

Aug 6 '06 #1
9 1226
Michael wrote:
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

Aug 6 '06 #2
Kai-Uwe Bux wrote:
Michael wrote:
>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;
std::cout ... std::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.
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.

Aug 6 '06 #3

Kai-Uwe Bux wrote:
Michael wrote:
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

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

Aug 6 '06 #4

Michael wrote:
Kai-Uwe Bux wrote:
Michael wrote:
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


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

Aug 6 '06 #5
* Michael:
Kai-Uwe Bux wrote:
>Michael wrote:
>>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.

I tried but it did not work. Could you please show me the code? Thanks!
How about, /you/ show the code that "did not work", and explain exactly
how it "did not work".

Before posting that code & explanation, though, please read the FAQ item
"How do I post a question about code that doesn't work correctly?",
currently at <url:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8>.

Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 6 '06 #6
Michael wrote:
>
Kai-Uwe Bux wrote:
>Michael wrote:
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


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
Aug 6 '06 #7

"Michael" <mi*******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
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
Aug 7 '06 #8

Michael wrote:
Kai-Uwe Bux wrote:
Michael wrote:
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


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<intx;

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;

}

Aug 10 '06 #9
In article <11*********************@i3g2000cwc.googlegroups.c om>,
mi*******@gmail.com says...

[ ... ]
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, " "));

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 11 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: km | last post by:
Hi all, was going thru the new features introduced into python2.4 version. i was stuck with 'decorators' - can someone explain me the need of such a thing called decorators ? tia KM
33
by: patrick_woflian | last post by:
hey guys, im just writing a basic calculation at the moment, before building on it for an A-Level piece of work. i can add/divide etc... two numbers together yet i am having a major problem with...
35
by: aNt17017 | last post by:
This is my code: long fact(int n) { if (n == 0) return(1); if(n > 100) { printf("\t\tERROR: %d is too large for factorial.\n", n); return 1;
8
by: salman | last post by:
this program is giving compile time error. so plse ge me the logic of factorial # include <iostream.h> # include <math.h> void main() { int f,sum=0,i,j,n; cout<<"\nEnter Number: ";
3
by: naeemulhaq | last post by:
Can anyone suggest a better solution to finding the digit frequencies in factorial of a number, like 3! = 6 (0) 0 (1) 0 (2) 0 (3) 0 (4) 0 (5) 0 (6) 1 ...
59
by: Umesh | last post by:
i wrote the following program to calculate factorial: #include<stdio.h> #include<iostream.h> void main() { int i,n; long int p=1; // or long double p=1; for exponential result which I don't...
3
by: Sugandh Jain | last post by:
Hi. How to write a function that will return me the factorial (say in a string) for the any positive integer it takes? When we find a factorial of even say 2000 or a higher number, it will be...
0
by: Killer42 | last post by:
Here's a very simple function for VB (written in VB6) to calculate the factorial (!) of a number. Note that it is limited by the magnitude of the value which can be stored in the Long data type. (In...
0
kadghar
by: kadghar | last post by:
Hi, I saw that Killer posted a simple Factorial Function that allows you to calculate up to 13!, well, you can use this for bigger numbers by changing the variable type. Why is this? You can...
2
by: becky808 | last post by:
Hi, I'm trying to write a program to calculate n factorial but it won't compile. Can anyone tell me what I'm doing wrong? #include <iostream> #include <cmath> using namespace std; int...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.