Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 02-28-08   #1 (permalink)
Overclocker in Training
 
.dp-BarackObama's Avatar
 
intel nvidia

Join Date: Feb 2008
Location: Indiana
Posts: 159

Rep: 5 .dp-BarackObama Unknown
Unique Rep: 5
Hardware Reviews: 2
Trader Rating: 0
Default A little recursion c++ app.

I wrote a recursion program in c++. It's a basic program to show how recursion works, all thought it could be commented a little better.

It prints a number and its factorial and then all the numbers that add up to its factorial as well.

Code:
//recursion

#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>

void instruct(void); //instructions function

unsigned long factorial ( unsigned long number);
unsigned long f_out ( unsigned long numb);

int
main(void)
{

	instruct();
for ( int lol = 0; lol <= 50; lol++){
	cout << setw ( 2 ) << lol << "! = " << factorial ( lol ) << setw( 5 ) <<  " = ";
	f_out(lol);
	cout << "n";
	}
	return 0;
}

unsigned long factorial( unsigned long number)
{
	if ( number <= 1) // base case
		return 1;
	else
		return number * factorial( number - 1 );
}

unsigned long f_out( unsigned long numb ) {
if ( numb <= 1 ) {
	cout << "1";
	return 1;}

else {
cout << numb << " * ";
	return ( numb * f_out( numb - 1));
}
}
void
instruct()
{
		cout << "Modify a factorial function to print its local variable and recursive call parameter.nn";
}
__________________
ß₤ứə Çřёώ
[b]3DMARK06: 9623/B]
XBOX360 GAMERTAG: Spankey4861

System: HELENKELLER
CPU
Q6600 @ 2.4
Motherboard
eVGA 780I
Memory
4 GB OCZ SLI RDY
Graphics Card
EVGA 8800GTS
Hard Drive
400GB WD/Seagate Internal / 500 WD External
Sound Card
SB X-Fi Audio [AC00]
Power Supply
Ultra X2 750watt
Case
Ultra Aluminus Mid-Tower
CPU cooling
AC Freezer 7
GPU cooling
80mm
OS
Vista X86

Last edited by .dp-BarackObama : 02-28-08 at 11:14 PM.
.dp-BarackObama is offline   Reply With Quote
Old 02-28-08   #2 (permalink)
Miscelaneous
 
Licht's Avatar
 
amd ati

Join Date: Mar 2007
Location: Bel Air
Posts: 9,933
Blog Entries: 3

Rep: 289 Licht is a proven memberLicht is a proven memberLicht is a proven member
Unique Rep: 188
Folding Team Rank: 904
Trader Rating: 0
Default

Very nice, any chance you would do a C# version?

System: Uzicht #4
CPU
Athlon X2 5200+ Windsor
Motherboard
Gigabyte 790X AM2+
Memory
6GB Kingston DDR2 667MHZ
Graphics Card
Saphire HD 3870
Hard Drive
4x WD1600AAJS RAID0
Sound Card
X-Fi Extreme Gamer Fatality Professional
Power Supply
OCZ Game-X-Stream 700w
Case
NZXT Black Steel
CPU cooling
Xigmatec Rifle
GPU cooling
Stock Saphire 3870 Cooling
OS
Vista Home Premium x64 SP1
Monitor
Samsung SyncMaster 19"Wide
Licht is online now I fold for Overclock.net Overclocked Account Licht's Gallery   Reply With Quote
Old 02-29-08   #3 (permalink)
Overclocker in Training
 
.dp-BarackObama's Avatar
 
intel nvidia

Join Date: Feb 2008
Location: Indiana
Posts: 159

Rep: 5 .dp-BarackObama Unknown
Unique Rep: 5
Hardware Reviews: 2
Trader Rating: 0
Default

yeah ill try to get it up tomorrow.
__________________
ß₤ứə Çřёώ
[b]3DMARK06: 9623/B]
XBOX360 GAMERTAG: Spankey4861

System: HELENKELLER
CPU
Q6600 @ 2.4
Motherboard
eVGA 780I
Memory
4 GB OCZ SLI RDY
Graphics Card
EVGA 8800GTS
Hard Drive
400GB WD/Seagate Internal / 500 WD External
Sound Card
SB X-Fi Audio [AC00]
Power Supply
Ultra X2 750watt
Case
Ultra Aluminus Mid-Tower
CPU cooling
AC Freezer 7
GPU cooling
80mm
OS
Vista X86
.dp-BarackObama is offline   Reply With Quote
Old 02-29-08   #4 (permalink)
Every base is base 10
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 5,009
Blog Entries: 1

Rep: 566 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 338
FAQs Submitted: 6
Trader Rating: 5
Default

Did you do that for a class or just for you? Recursion is powerful, but a factorial calculation is much better implemented in loop form

Quote:
Originally Posted by Licht View Post
Very nice, any chance you would do a C# version?
Just out of curiosity, why?
__________________
BIG BROTHER
Apple doesn't love you

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 02-29-08   #5 (permalink)
Miscelaneous
 
Licht's Avatar
 
amd ati

Join Date: Mar 2007
Location: Bel Air
Posts: 9,933
Blog Entries: 3

Rep: 289 Licht is a proven memberLicht is a proven memberLicht is a proven member
Unique Rep: 188
Folding Team Rank: 904
Trader Rating: 0
Default

Quote:
Originally Posted by rabidgnome229 View Post
Did you do that for a class or just for you? Recursion is powerful, but a factorial calculation is much better implemented in loop form



Just out of curiosity, why?
I can do C++ but its a chore. Saves time for me to have a code in native C#.

System: Uzicht #4
CPU
Athlon X2 5200+ Windsor
Motherboard
Gigabyte 790X AM2+
Memory
6GB Kingston DDR2 667MHZ
Graphics Card
Saphire HD 3870
Hard Drive
4x WD1600AAJS RAID0
Sound Card
X-Fi Extreme Gamer Fatality Professional
Power Supply
OCZ Game-X-Stream 700w
Case
NZXT Black Steel
CPU cooling
Xigmatec Rifle
GPU cooling
Stock Saphire 3870 Cooling
OS
Vista Home Premium x64 SP1
Monitor
Samsung SyncMaster 19"Wide
Licht is online now I fold for Overclock.net Overclocked Account Licht's Gallery   Reply With Quote
Old 02-29-08   #6 (permalink)
Overclocker in Training
 
.dp-BarackObama's Avatar
 
intel nvidia

Join Date: Feb 2008
Location: Indiana
Posts: 159

Rep: 5 .dp-BarackObama Unknown
Unique Rep: 5
Hardware Reviews: 2
Trader Rating: 0
Default

Quote:
Originally Posted by rabidgnome229 View Post
Did you do that for a class or just for you? Recursion is powerful, but a factorial calculation is much better implemented in loop form
did it for a class a while ago. thought i'd post it so people who don't know recursion. maybe it will help them understand better
__________________
ß₤ứə Çřёώ
[b]3DMARK06: 9623/B]
XBOX360 GAMERTAG: Spankey4861

System: HELENKELLER
CPU
Q6600 @ 2.4
Motherboard
eVGA 780I
Memory
4 GB OCZ SLI RDY
Graphics Card
EVGA 8800GTS
Hard Drive
400GB WD/Seagate Internal / 500 WD External
Sound Card
SB X-Fi Audio [AC00]
Power Supply
Ultra X2 750watt
Case
Ultra Aluminus Mid-Tower
CPU cooling
AC Freezer 7
GPU cooling
80mm
OS
Vista X86
.dp-BarackObama is offline   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 03:07 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.38790 seconds with 8 queries