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

Reply
 
LinkBack Thread Tools
Old 10-31-09   #1 (permalink)
Intel Overclocker
 
intel ati

Join Date: Oct 2004
Posts: 5,189

Rep: 238 Mikey122687 is acknowledged by manyMikey122687 is acknowledged by manyMikey122687 is acknowledged by many
Unique Rep: 176
FAQs Submitted: 2
Folding Team Rank: 1831
Hardware Reviews: 5
Trader Rating: 48
Default Fibonacci Numbers

PHP Code:
#include <iostream>
using namespace std;

// The function for finding the Fibonacci number
int fib(int);

int main()
{
  
// Prompt the user to enter an integer
  
cout <<  "Enter an index for the Fibonacci number: ";
  
int index;
  
cin >> index;

  
// Display factorial
  
cout << "Fibonacci number at index " << index << " is "
    
<< fib(index) << endl;

  return 
0;
}

// The function for finding the Fibonacci number
int fib(int index)
{
  if (
index == 0// Base case
    
return 0;
  else if (
index == 1// Base case
    
return 1;
  else 
// Reduction and recursive calls
    
return fib(index 1) + fib(index 2);

So i have that code, and i'm supposed to find the number of times the 'fib' function is called.

Any idea how to do that?
__________________


System: Main Rig
CPU
Intel C2Q 6600
Motherboard
DFI LP UT P35-T2R
Memory
GSkill F2-6400CL5D-4GBPQ
Graphics Card
Diamond 4870 1GB
Hard Drive
WD Raptor 74GB RAID0
Sound Card
Creative XFi Fatal1ty FPS
Power Supply
Silverstone Zeus 850W
Case
Lian Li V2000B
CPU cooling
TRUE 120
OS
Windows 7 X64 Pro
Monitor
SAMSUNG 2253BW Black 22" LCD
Mikey122687 is offline I fold for Overclock.net   Reply With Quote
Old 10-31-09   #2 (permalink)
Graphics Card Aficionado
 
Error 404's Avatar
 
amd ati

Join Date: Sep 2008
Posts: 1,000

Rep: 91 Error 404 is acknowledged by some
Unique Rep: 82
Trader Rating: 8
Default

Quote:
Originally Posted by Mikey122687 View Post
PHP Code:
#include <iostream>
using namespace std;

// The function for finding the Fibonacci number
int fib(int);
int num 0;

int main()
{
  
// Prompt the user to enter an integer
  
cout <<  "Enter an index for the Fibonacci number: ";
  
int index;
  
cin >> index;

  
// Display factorial
  
cout << "Fibonacci number at index " << index << " is "
    
<< fib(index) << endl;

 
//Display # of times fib() is called
cout <<"Fib() called " << num << " times.";
  return 
0;
}

// The function for finding the Fibonacci number
int fib(int index)
{
  
num++;
  if (
index == 0// Base case
    
return 0;
  else if (
index == 1// Base case
    
return 1;
  else 
// Reduction and recursive calls
    
return fib(index 1) + fib(index 2);

So i have that code, and i'm supposed to find the number of times the 'fib' function is called.

Any idea how to do that?
It creates another variable (num) and increments it once every time fib() is called.

That should do it. My C/C++ is a bit rusty...

edit: forgot ";'s"

edit 3, maybe 4 or 5: Added .exe
Attached Files
File Type: zip fibonacci.zip (71.9 KB, 4 views)
__________________
I'm not terribly active at the moment...I'm caught up in school and such, so you have my advance apology if I don't respond to a post/PM the way I usually do.


4850 at 720 core/1100 mem - 1.2vGPU
3.5GHz Unlocked @ 1.42VCore Stable so far

Project: Sruth de athrú - An un-decided and slow-moving techstation project.

System: Baby Dragon
CPU
Phenomnom II 720 BE Unlocked.
Motherboard
GA-MA790XT-UD4P
Memory
2x1 GB Crucial "Value" D9JNL
Graphics Card
MSI R4850 512 MB
Hard Drive
500GB 'Cuda plus assorted others.
Sound Card
Onboard
Power Supply
OCZ StealthXStream 600W
Case
CM 690
CPU cooling
Swiftech GTZ [Pending arrival]
GPU cooling
DangerDen Maze 4 [Pending installation]
OS
Windows 7 RC x64/Arch x86_64
Monitor
Samsung 2343BWX High Density

Last edited by Error 404 : 10-31-09 at 11:50 PM Reason: bah. Doesn't take BBcode in [php] tags
Error 404 is online now   Reply With Quote
Old 10-31-09   #3 (permalink)
New to Overclock.net
 
intel nvidia

Join Date: Nov 2007
Location: Surrey, BC, Canada
Posts: 405

Rep: 17 serge2k Unknown
Unique Rep: 17
Trader Rating: 2
Default

that will work.

then just print num after the call.

Absolutely horrible way to calculate fibonacci numbers though. very wasteful. As you shall see.
__________________
System: The Black Box
CPU
E6750
Motherboard
Asus P5N-E SLI
Memory
4GB DDR-2 800 MHz
Graphics Card
EVGA 8800 GTS 640
Hard Drive
2x250GB Seagate in Raid 0, 2x1TB WD Caviar Blacks
Sound Card
integrated
Power Supply
Antec Earthwatts 500W
Case
Antec Sonata 3
CPU cooling
Xigmatek HDT S1283
GPU cooling
stock
OS
Windows 7 Ultimate x64
Monitor
22" LG
serge2k is offline   Reply With Quote
Old 10-31-09   #4 (permalink)
Programmer
 
chatch15117's Avatar
 
intel nvidia

Join Date: Sep 2009
Location: Orlando, FL
Posts: 821

Rep: 47 chatch15117 is acknowledged by some
Unique Rep: 43
Folding Team Rank: 435
Trader Rating: 0
Default

have fib increment a global variable at the beginning of each function call.

System: Mr. Doctor Professor General Sir Lancelot III
CPU
i7 920 D0 @ 3.84GHz (24/7)
Motherboard
GIGABYTE GA-EX58-UD3R
Memory
mushkin 3GB DDR3 1600 7-8-7-20
Graphics Card
MSI OCv3 GeForce GTX 260
Hard Drive
3x WD Caviar Black 640GB-RAID0, HM160HI, 7200.7
Sound Card
On board
Power Supply
OCZ GameXStream 850W
Case
Antec Nine Hundred
CPU cooling
MUGEN-2
OS
x86 Windows 7 Ultimate RTM
Monitor
HP w2207h 22" LCD
chatch15117 is online now I fold for Overclock.net   Reply With Quote
Old 11-01-09   #5 (permalink)
Intel Overclocker
 
intel ati

Join Date: Oct 2004
Posts: 5,189

Rep: 238 Mikey122687 is acknowledged by manyMikey122687 is acknowledged by manyMikey122687 is acknowledged by many
Unique Rep: 176
FAQs Submitted: 2
Folding Team Rank: 1831
Hardware Reviews: 5
Trader Rating: 48
Default

Quote:
Originally Posted by Error 404 View Post
It creates another variable (num) and increments it once every time fib() is called.

That should do it. My C/C++ is a bit rusty...

edit: forgot ";'s"

edit 3, maybe 4 or 5: Added .exe
Can you post the code? I don't need the program, i just need to know how to do it

Quote:
Originally Posted by serge2k View Post
that will work.

then just print num after the call.

Absolutely horrible way to calculate fibonacci numbers though. very wasteful. As you shall see.
Why is this a bad way to do it?

Quote:
Originally Posted by chatch15117 View Post
have fib increment a global variable at the beginning of each function call.
what do you mean? how would i do that?
__________________


System: Main Rig
CPU
Intel C2Q 6600
Motherboard
DFI LP UT P35-T2R
Memory
GSkill F2-6400CL5D-4GBPQ
Graphics Card
Diamond 4870 1GB
Hard Drive
WD Raptor 74GB RAID0
Sound Card
Creative XFi Fatal1ty FPS
Power Supply
Silverstone Zeus 850W
Case
Lian Li V2000B
CPU cooling
TRUE 120
OS
Windows 7 X64 Pro
Monitor
SAMSUNG 2253BW Black 22" LCD
Mikey122687 is offline I fold for Overclock.net   Reply With Quote
Old 11-01-09   #6 (permalink)
Graphics Card Aficionado
 
Error 404's Avatar
 
amd ati

Join Date: Sep 2008
Posts: 1,000

Rep: 91 Error 404 is acknowledged by some
Unique Rep: 82
Trader Rating: 8
Default

I edited the code in your quotes

It works, too. Bonus!
__________________
I'm not terribly active at the moment...I'm caught up in school and such, so you have my advance apology if I don't respond to a post/PM the way I usually do.


4850 at 720 core/1100 mem - 1.2vGPU
3.5GHz Unlocked @ 1.42VCore Stable so far

Project: Sruth de athrú - An un-decided and slow-moving techstation project.

System: Baby Dragon
CPU
Phenomnom II 720 BE Unlocked.
Motherboard
GA-MA790XT-UD4P
Memory
2x1 GB Crucial "Value" D9JNL
Graphics Card
MSI R4850 512 MB
Hard Drive
500GB 'Cuda plus assorted others.
Sound Card
Onboard
Power Supply
OCZ StealthXStream 600W
Case
CM 690
CPU cooling
Swiftech GTZ [Pending arrival]
GPU cooling
DangerDen Maze 4 [Pending installation]
OS
Windows 7 RC x64/Arch x86_64
Monitor
Samsung 2343BWX High Density
Error 404 is online now   Reply With Quote
Old 11-01-09   #7 (permalink)
Nom nom nom..
 
Blizzie's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: California, USA
Posts: 3,122

Rep: 290 Blizzie is a proven memberBlizzie is a proven memberBlizzie is a proven member
Unique Rep: 237
Trader Rating: 2
Default

Code:
	if (n == 0 || n == 1){
		return 1;
	}
	else {
		sum = (Fib(n-1) + Fib(n-2));
		return sum;
	}

You want this as a recursive function that calls itself.
__________________

Blizzie
Κατεψυγμένα Στερεά

Hold on to the calm before the storm comes



System: Halcyon
CPU
Intel i7 860 | 4.0 GHz @ 1.33v | HT on
Motherboard
EVGA P55 FTW
Memory
2x2 GB Corsair Dominator | 1.6 GHz | 8-8-8-24-1T
Graphics Card
SLI EVGA GTX 260 | 702/1404/1242
Hard Drive
OCZ 60GB Vertex | WD5000AAKS | WD1001FALS
Sound Card
Creative X-Fi XtremeGamer
Power Supply
Corsair HX850W
Case
Antec Nine Hundred
CPU cooling
Prolimatech Megahalems + 2x Scythe S-FLEX SFF21G
OS
Windows 7 Professional x64
Monitor
Envision 22 in. LCD Widescreen | 1680x1050
Blizzie is offline Overclocked Account   Reply With Quote
Old 11-01-09   #8 (permalink)
Programmer
 
chatch15117's Avatar
 
intel nvidia

Join Date: Sep 2009
Location: Orlando, FL
Posts: 821

Rep: 47 chatch15117 is acknowledged by some
Unique Rep: 43
Folding Team Rank: 435
Trader Rating: 0
Default

Quote:
Originally Posted by Mikey122687 View Post
Can you post the code? I don't need the program, i just need to know how to do it



Why is this a bad way to do it?



what do you mean? how would i do that?
Quote:
#include <iostream>
using namespace std;

// The function for finding the Fibonacci number
int fib(int);

int main()
{
// Prompt the user to enter an integer
cout << "Enter an index for the Fibonacci number: ";
int index;
cin >> index;
int RAWR = 0;
// Display factorial
cout << "Fibonacci number at index " << index << " is "
<< fib(index) << endl;

return 0;
}

// The function for finding the Fibonacci number
int fib(int index)
{
if (index == 0) // Base case
return 0;
else if (index == 1) // Base case
return 1;
else // Reduction and recursive calls
{
RAWR++;
return fib(index - 1) + fib(index - 2);
}
}
like that?

System: Mr. Doctor Professor General Sir Lancelot III
CPU
i7 920 D0 @ 3.84GHz (24/7)
Motherboard
GIGABYTE GA-EX58-UD3R
Memory
mushkin 3GB DDR3 1600 7-8-7-20
Graphics Card
MSI OCv3 GeForce GTX 260
Hard Drive
3x WD Caviar Black 640GB-RAID0, HM160HI, 7200.7
Sound Card
On board
Power Supply
OCZ GameXStream 850W
Case
Antec Nine Hundred
CPU cooling
MUGEN-2
OS
x86 Windows 7 Ultimate RTM
Monitor
HP w2207h 22" LCD
chatch15117 is online now I fold for Overclock.net   Reply With Quote
Old 11-01-09   #9 (permalink)
Intel Overclocker
 
intel ati

Join Date: Oct 2004
Posts: 5,189

Rep: 238 Mikey122687 is acknowledged by manyMikey122687 is acknowledged by manyMikey122687 is acknowledged by many
Unique Rep: 176
FAQs Submitted: 2
Folding Team Rank: 1831
Hardware Reviews: 5
Trader Rating: 48
Default

PHP Code:
#include <iostream>
using namespace std;

int count 0;
// The function for finding the Fibonacci number
int fib(int);


int main()
{
  
// Prompt the user to enter an integer
  
cout <<  "Enter an index for the Fibonacci number: ";
  
int index;
  
cin >> index;

  
// Display factorial
  
cout << "Fibonacci number at index " << index << " is "
    
<< fib(index) << endl;

  
// Display how many times the function 'fib' is called
  
cout << "The Function 'fib' is called " << count << " times" << endl;
  return 
0;
}

// The function for finding the Fibonacci number
int fib(int index)
{
  if (
index == 0// Base case
    
return 0;
  else if (
index == 1// Base case
    
return 1;
  else 
// Reduction and recursive calls
  
{
    
count++;
    return 
fib(index 1) + fib(index 2);
  }

here is what i have so far
__________________


System: Main Rig
CPU
Intel C2Q 6600
Motherboard
DFI LP UT P35-T2R
Memory
GSkill F2-6400CL5D-4GBPQ
Graphics Card
Diamond 4870 1GB
Hard Drive
WD Raptor 74GB RAID0
Sound Card
Creative XFi Fatal1ty FPS
Power Supply
Silverstone Zeus 850W
Case
Lian Li V2000B
CPU cooling
TRUE 120
OS
Windows 7 X64 Pro
Monitor
SAMSUNG 2253BW Black 22" LCD
Mikey122687 is offline I fold for Overclock.net   Reply With Quote
Old 11-01-09   #10 (permalink)
Intel Overclocker
 
intel ati

Join Date: Oct 2004
Posts: 5,189

Rep: 238 Mikey122687 is acknowledged by manyMikey122687 is acknowledged by manyMikey122687 is acknowledged by many
Unique Rep: 176
FAQs Submitted: 2
Folding Team Rank: 1831
Hardware Reviews: 5
Trader Rating: 48
Default

Quote:
Originally Posted by chatch15117 View Post
like that?
Thanks!

Edit: Thanks everyone for their help, it was helpful, REP++
__________________


System: Main Rig
CPU
Intel C2Q 6600
Motherboard
DFI LP UT P35-T2R
Memory
GSkill F2-6400CL5D-4GBPQ
Graphics Card
Diamond 4870 1GB
Hard Drive
WD Raptor 74GB RAID0
Sound Card
Creative XFi Fatal1ty FPS
Power Supply
Silverstone Zeus 850W
Case
Lian Li V2000B
CPU cooling
TRUE 120
OS
Windows 7 X64 Pro
Monitor
SAMSUNG 2253BW Black 22" LCD

Last edited by Mikey122687 : 11-01-09 at 12:25 AM
Mikey122687 is offline I fold for Overclock.net   Reply With Quote
Reply


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



All times are GMT -5. The time now is 08:08 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.18779 seconds with 9 queries