New Posts  All Forums:Forum Nav:

# C++ Assistance

Code:
``````I am having the hardest time with this project, all the other ones I have accomplished just fine but this one... something else!

So how its suppose to work, prompts user to key in a number and it will do the factorial of the number. Not only that though, it should show you that the factorial of that number "N" is the same as N! = N*N-1! Then do the math. With indents too

It should display like this:

5!=5*4!
4!=4*3!
3!=3*2!
2!=2*1!
1!=1
2!=2
3!=6
4!=24
5!=120```
```

Code:
``````// Fig. 5.28: fig05_28.cpp
// Demonstrating the recursive function factorial.
#include <iostream>
#include <iomanip>
using namespace std;

unsigned long factorial( unsigned long ); // function prototype

int main()
{
//Asking user what number to factorial
int max=0; // 'Max' is the integer to tell the counter where to stop
cout << "Please enter a number to factorial: ";
cin >> max;
cout << "\n\n";

// calculate the factorials of 0 through 10

for ( int counter = 0; counter <= max; counter++ )

cout << setw( 2 ) << counter << "! = " << factorial( counter ) << endl;

} // end main

// recursive definition of function factorial
unsigned long factorial( unsigned long number)
{
int result = 0;
int depthcounter = 1;
int decrease = 0;

if ( number <= 1 ) // test for base case
{
result = 1; // base cases: 0! = 1 and 1! = 1
}
else // recursion step
{

cout << "\t";
result = number * factorial( number -1), depthcounter + 1;

}
return result;
} // end function factorial```
```

Edited by kplonsky - 10/21/13 at 8:24pm
 RIP i7 920 Hello X5660 (18 items)
CPUMotherboardGraphicsRAM
X5660 P6T NVIDIA GeForce GTX 770 OCZ3x1333LV2G
RAMRAMHard DriveHard Drive
OCZ3x1333LV2G OCZ3x1333LV2G Western Digital WD Green WD15EADS Crucial M4 SSD 120 GB
Hard DriveOSMonitorKeyboard
Western Digital WD Blue WD10EZEX Windows 10 ASUS VG248QE Ducky Keyboard
EVGA SuperNOVA 750G2 Fractal Design Define R5 Logitech G5 Steelseries
Audio
Sennheiser HD 558
 RIP i7 920 Hello X5660 (18 items)
CPUMotherboardGraphicsRAM
X5660 P6T NVIDIA GeForce GTX 770 OCZ3x1333LV2G
RAMRAMHard DriveHard Drive
OCZ3x1333LV2G OCZ3x1333LV2G Western Digital WD Green WD15EADS Crucial M4 SSD 120 GB
Hard DriveOSMonitorKeyboard
Western Digital WD Blue WD10EZEX Windows 10 ASUS VG248QE Ducky Keyboard
EVGA SuperNOVA 750G2 Fractal Design Define R5 Logitech G5 Steelseries
Audio
Sennheiser HD 558
Any hints or anything would be helpful. I just need to rest my mind now, I cant turn it in since its late but I need to know!!! Some of the code I posted needs to be cleaned up since I have been playing around with it so sorry its a mess.
Edited by kplonsky - 10/21/13 at 8:14pm
 RIP i7 920 Hello X5660 (18 items)
CPUMotherboardGraphicsRAM
X5660 P6T NVIDIA GeForce GTX 770 OCZ3x1333LV2G
RAMRAMHard DriveHard Drive
OCZ3x1333LV2G OCZ3x1333LV2G Western Digital WD Green WD15EADS Crucial M4 SSD 120 GB
Hard DriveOSMonitorKeyboard
Western Digital WD Blue WD10EZEX Windows 10 ASUS VG248QE Ducky Keyboard
EVGA SuperNOVA 750G2 Fractal Design Define R5 Logitech G5 Steelseries
Audio
Sennheiser HD 558
 RIP i7 920 Hello X5660 (18 items)
CPUMotherboardGraphicsRAM
X5660 P6T NVIDIA GeForce GTX 770 OCZ3x1333LV2G
RAMRAMHard DriveHard Drive
OCZ3x1333LV2G OCZ3x1333LV2G Western Digital WD Green WD15EADS Crucial M4 SSD 120 GB
Hard DriveOSMonitorKeyboard
Western Digital WD Blue WD10EZEX Windows 10 ASUS VG248QE Ducky Keyboard
EVGA SuperNOVA 750G2 Fractal Design Define R5 Logitech G5 Steelseries
Audio
Sennheiser HD 558
got rid of some unnecessary code in your recursive function. Hopefully this is what you were looking for.
Code:
``````
// Fig. 5.28: fig05_28.cpp
// Demonstrating the recursive function factorial.
#include <iostream>
#include <iomanip>
using namespace std;

unsigned long factorial( unsigned long ); // function prototype

int main()
{
//Asking user what number to factorial
int max=0; // 'Max' is the integer to tell the counter where to stop
cout << "Please enter a number to factorial: ";
cin >> max;
cout << "\n\n";

// calculate the factorials of 0 through 10
int tab = 0;
for ( int counter = max; counter >= 1; --counter ){

if(counter == 1){
cout << setw(tab) << "1! = 1" << endl;
tab = tab - 15;
cout << endl;
}
else{
cout << setw(tab) << counter << "! = " << counter << " * " << counter - 1 << "!" << endl;
tab = tab + 15;
cout << endl;
}
}

for ( int counter = 0; counter <= max; ++counter ){

if(counter == 0 || counter == 1){
factorial(counter);
}
else{
cout << setw( tab ) << counter << "! = " << factorial( counter ) << endl;
tab = tab - 15;
cout << endl;
}
}
} // end main

// recursive definition of function factorial
unsigned long factorial( unsigned long number)
{
int result = 0;

if ( number <= 1 ) // test for base case
{
result = 1; // base cases: 0! = 1 and 1! = 1
}
else // recursion step
{
result = number * factorial( number - 1);
}
return result;
} // end function factorial
```
```
 Black Silence (15 items)
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder
SteelSeries QcK Mass Altec Lansing FX4021
 Black Silence (15 items)
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder
SteelSeries QcK Mass Altec Lansing FX4021
Oh wow, thank you! Time to study this
 RIP i7 920 Hello X5660 (18 items)
CPUMotherboardGraphicsRAM
X5660 P6T NVIDIA GeForce GTX 770 OCZ3x1333LV2G
RAMRAMHard DriveHard Drive
OCZ3x1333LV2G OCZ3x1333LV2G Western Digital WD Green WD15EADS Crucial M4 SSD 120 GB
Hard DriveOSMonitorKeyboard
Western Digital WD Blue WD10EZEX Windows 10 ASUS VG248QE Ducky Keyboard
EVGA SuperNOVA 750G2 Fractal Design Define R5 Logitech G5 Steelseries
Audio
Sennheiser HD 558
 RIP i7 920 Hello X5660 (18 items)
CPUMotherboardGraphicsRAM
X5660 P6T NVIDIA GeForce GTX 770 OCZ3x1333LV2G
RAMRAMHard DriveHard Drive
OCZ3x1333LV2G OCZ3x1333LV2G Western Digital WD Green WD15EADS Crucial M4 SSD 120 GB
Hard DriveOSMonitorKeyboard
Western Digital WD Blue WD10EZEX Windows 10 ASUS VG248QE Ducky Keyboard
EVGA SuperNOVA 750G2 Fractal Design Define R5 Logitech G5 Steelseries
Audio
Sennheiser HD 558
made some small fixes, inputting 0 now works...also condensed the 2nd for loop by removing the if else chain.
Code:
``````
// Fig. 5.28: fig05_28.cpp
// Demonstrating the recursive function factorial.
#include <iostream>
#include <iomanip>
using namespace std;

unsigned long factorial( unsigned long ); // function prototype

int main()
{
//Asking user what number to factorial
int max=0; // 'Max' is the integer to tell the counter where to stop
cout << "Please enter a number to factorial: ";
cin >> max;
cout << "\n\n";

// calculate the factorials of 0 through 10
int tab = 0;
if(max == 0)
cout << "0! = 1" << endl;
else{
for ( int counter = max; counter >= 1; --counter ){

if(counter == 1){
cout << setw(tab) << "1! = 1" << endl;
tab = tab - 15;
cout << endl;
}
else{
cout << setw(tab) << counter << "! = " << counter << " * " << counter - 1 << "!" << endl;
tab = tab + 15;
cout << endl;
}
}
}

for ( int counter = 2; counter <= max; ++counter ){

cout << setw( tab ) << counter << "! = " << factorial( counter ) << endl;
tab = tab - 15;
cout << endl;
}
} // end main

// recursive definition of function factorial
unsigned long factorial( unsigned long number)
{
int result = 0;

if ( number <= 1 ) // test for base case
{
result = 1; // base cases: 0! = 1 and 1! = 1
}
else // recursion step
{
result = number * factorial( number - 1);
}
return result;
} // end function factorial
```
```
 Black Silence (15 items)
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder
SteelSeries QcK Mass Altec Lansing FX4021
 Black Silence (15 items)
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder
SteelSeries QcK Mass Altec Lansing FX4021
Can you change the signature for the factorial() function? My take is that this exercise is intended to illustrate the recursive factorial process. With that assumption, the easiest implementation is to have the recursive function itself do the printing.
Code:
``````// Fig. 5.28: fig05_28.cpp
// Demonstrating the recursive function factorial.
#include <iostream>
#include <iomanip>
using namespace std;

unsigned long factorial( unsigned long, int tabs=0 ); // function prototype

int main()
{
//Asking user what number to factorial
int max=0; // 'Max' is the integer to tell the counter where to stop
cout << "Please enter a number to factorial: ";
cin >> max;
cout << "\n\n";

factorial(max);

} // end main

// recursive definition of function factorial
unsigned long factorial( unsigned long number, int tab)
{
int result = 0;

if ( number <= 1 ) // test for base case
{
cout << setw(tab) << "1! = 1" << endl;
result = 1; // base cases: 0! = 1 and 1! = 1
}
else // recursion step
{
cout << setw(tab) << number << "! = " << number << " * " << number - 1 << "!" << endl;
result = number * factorial( number - 1, tab + 15);
cout << setw( tab ) << number << "! = " << result << endl;
}
return result;
} // end function factorial```
```

As you can see, the main method is now much smaller. The added tabs parameter to the factorial function defaults to 0, so you can still call factorial(5).
 Cube (9 items)
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540
Mouse
Corsair M65
 Cube (9 items)
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540
Mouse
Corsair M65
Quote:
Originally Posted by jvolkman

As you can see, the main method is now much smaller. The added tabs parameter to the factorial function defaults to 0, so you can still call factorial(5).

Couldn't you just take the amount of tabbing needed to be the "max - [current input]"? I don't quite remember how C++ would treat the variable "max" with regards to where you could grab it from, but if you could grab it from within the function you could avoid having to pass in a tab parameter at all by simply tabbing over depending on how far you've counted down.

ie: setw( (max-number)*15 )

If you could declare the variable max in such a way that it would be visible to the factorial function it does not seem like you would need another input.

... Also I like you folks are just giving him the answer... Well whatever.
Quote:
Originally Posted by stolemyowncar

Couldn't you just take the amount of tabbing needed to be the "max - [current input]"? I don't quite remember how C++ would treat the variable "max" with regards to where you could grab it from, but if you could grab it from within the function you could avoid having to pass in a tab parameter at all by simply tabbing over depending on how far you've counted down.

ie: setw( (max-number)*15 )

If you could declare the variable max in such a way that it would be visible to the factorial function it does not seem like you would need another input.

You could do that, but global variables are generally frowned upon. You could also create a global "tabs" variable and remove the need for passing it to the recursive function, but this is again usually not desirable.

Quote:
Originally Posted by stolemyowncar

... Also I like you folks are just giving him the answer... Well whatever.

True, but he won't get very far if he simply uses the answer without understanding it.
 Cube (9 items)
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540
Mouse
Corsair M65
 Cube (9 items)
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540
Mouse
Corsair M65
I think they have their uses. In this case it would allow you to get around changing the function prototype, and you also would not have to allocate space for a local "tab" variable for every function call... though that's probably a bit trivial I guess, considering the math operation is also more complex... it's a tradeoff, just a minor side suggestion.

On a side note:
I'm not sure why globals are just generally frowned upon. I don't think anything should be frowned upon in programming unless it's used badly. Languages just have a set of tools, it's up to you to use them responsibly and optimally. Just imo.
Quote:
Originally Posted by jvolkman

Can you change the signature for the factorial() function? My take is that this exercise is intended to illustrate the recursive factorial process. With that assumption, the easiest implementation is to have the recursive function itself do the printing.
Code:
``````// Fig. 5.28: fig05_28.cpp
// Demonstrating the recursive function factorial.
#include <iostream>
#include <iomanip>
using namespace std;

unsigned long factorial( unsigned long, int tabs=0 ); // function prototype

int main()
{
//Asking user what number to factorial
int max=0; // 'Max' is the integer to tell the counter where to stop
cout << "Please enter a number to factorial: ";
cin >> max;
cout << "\n\n";

factorial(max);

} // end main

// recursive definition of function factorial
unsigned long factorial( unsigned long number, int tab)
{
int result = 0;

if ( number <= 1 ) // test for base case
{
cout << setw(tab) << "1! = 1" << endl;
result = 1; // base cases: 0! = 1 and 1! = 1
}
else // recursion step
{
cout << setw(tab) << number << "! = " << number << " * " << number - 1 << "!" << endl;
result = number * factorial( number - 1, tab + 15);
cout << setw( tab ) << number << "! = " << result << endl;
}
return result;
} // end function factorial```
```

As you can see, the main method is now much smaller. The added tabs parameter to the factorial function defaults to 0, so you can still call factorial(5).

This method I understand completely!
 RIP i7 920 Hello X5660 (18 items)
CPUMotherboardGraphicsRAM
X5660 P6T NVIDIA GeForce GTX 770 OCZ3x1333LV2G
RAMRAMHard DriveHard Drive
OCZ3x1333LV2G OCZ3x1333LV2G Western Digital WD Green WD15EADS Crucial M4 SSD 120 GB
Hard DriveOSMonitorKeyboard
Western Digital WD Blue WD10EZEX Windows 10 ASUS VG248QE Ducky Keyboard
EVGA SuperNOVA 750G2 Fractal Design Define R5 Logitech G5 Steelseries
Audio
Sennheiser HD 558
 RIP i7 920 Hello X5660 (18 items)
CPUMotherboardGraphicsRAM
X5660 P6T NVIDIA GeForce GTX 770 OCZ3x1333LV2G
RAMRAMHard DriveHard Drive
OCZ3x1333LV2G OCZ3x1333LV2G Western Digital WD Green WD15EADS Crucial M4 SSD 120 GB
Hard DriveOSMonitorKeyboard
Western Digital WD Blue WD10EZEX Windows 10 ASUS VG248QE Ducky Keyboard