I'm a rookie at C++ and trying to learn on my own with a lot of tutorials. My question is, what is the best method of caculating and displaying the values of 1! to 10! using nested "for" statements? Thanks for any advice in advance.

03-15-2005

prog-bman

You don't really need nested for statments you only need one loop think about it.
a = 1;
for i = 0; i < 10; i++
a = a * i
print a
[edit] thanks hunter ya jerk :) [/edit]

You can do it with a recursive function as well, although personally I can't write a recursive function to save my life. There is a tutorial on this site covering recursive functions, but I would never have figured out the output of this without running it:

Code:

void printnum ( int begin )
{
cout<< begin;

if ( begin < 9 )
printnum ( begin + 1 );

cout<< begin;
}

03-16-2005

treenef

Hi,

I just thought I'd add a little bit although you probably get the picture now. I have written a complete code which computes n! from n=1 to n=10, like you have mentioned using a 'nested for loop'.

There are easier ways to do it, but I am just doing what you have specified. Underneath is the entire code which can be copied and pasted into your programme straight away.

Code:

/************************************
Calculates factorial of a number (n!)

from n=1 to n=10
************************************/
#include<iostream.h>

Thanks everybody for responding. To help me better understand, why did treenef initialize the sample code within the inner loop? I notice if I take this out, you get weird negative numbers. The code in question is:

accum=1; //initialise accumulate

Thanks

03-16-2005

Hunter2

Actually, it's the outer loop. The outer loop is for displaying the factorial from 1 to 10, and the inner loop is for calculating the factorial of i. Since the accumulator must be reset for each factorial you are calculating, it makes sense to reset it each time at the beginning of the outer loop.

03-17-2005

treenef

Yes hunter is correct.

I should have initialised accum before the nested for statements.
And I should have reset accum at the end of the outer loop. However, I had written the code in a rush and since it worked fine I didn't notice that the logic was incorrect. Here is the ammended code.

Oh and just a helpful hint S_ny 33 -
it's always useful to convert code into flow charts to help you visualise what is going on, especially when it comes to nested statements.

Code:

/************************************
Calculates factorial of a number (n!)

from n=1 to n=10
************************************/
#include<iostream.h>

Treenef -- your code is fine and it was very helpful...thanks. The flowchart is a good idea because I think that is my problem when determining the proper outlay of the code. Thanks.

03-17-2005

Hunter2

>>I should have initialised accum before the nested for statements.
No, it's fine as it is - accum will always be initialized to 1 before being used, and it will be reset when the outer loop wraps around to the beginning again. Either way will work fine though.

03-17-2005

Krak

Okay boys and girls, let's learn something really quick:

You do not need 2 for loops to do something simple like factorials. Usually, 2 for loops would be used to anything involving a 2D array or something of that nature. Also for creating a map or tiles or representing anything in 2 dimensions:

But simple crap like factorials don't need nest for loops. It'll just be more confusing to read, and stupid.

......There's your daily dose of common sense. :cool:

03-17-2005

Hunter2

Quote:

Originally Posted by Krak

Okay boys and girls, let's learn something really quick:

You do not need 2 for loops to do something simple like factorials. Usually, 2 for loops would be used to anything involving a 2D array or something of that nature. Also for creating a map or tiles or representing anything in 2 dimensions:

But simple crap like factorials don't need nest for loops. It'll just be more confusing to read, and stupid.

......There's your daily dose of common sense. :cool:

And, Krak, you should do some reading before posting. The factorial is being calculated in one loop, and the outer loop is to repeat the factorial operation from 1! to 10!. Not the most efficient method to be sure (a single loop from 1 to 10, printing the running product after each iteration as prog-bman suggested, would be ideal but more of a specific-case hack), but the logic is correct and intuitive.

03-18-2005

VirtualAce

Quote:

Also for creating a map or tiles or representing anything in 2 dimensions:

Wrong.

2 variables for x and y, one for offset. 1 loop and faster than a 2D array.