techPowerUp! Forums

Go Back   techPowerUp! Forums > Software > Programming & Webmastering

Reply
 
Thread Tools
Old Nov 23, 2009, 06:23 PM   #1
Lampmaster
75 Posts
 
Lampmaster's Avatar
 
Join Date: Aug 2009
Location: Washington The State
Posts: 149 (0.11/day)
Thanks: 35
Thanked 17 Times in 14 Posts

System Specs

Noob programmer help

Hi, I was just wondering if it was possible to take this C++ code and make it to where it sums using the Fibonacci sequence.

Quote:

cin >> n;

sum = 0;
ii = 0; // Loop control variable
while (ii <= n)
{
sum = sum + ii;
ii++;
} // While loop

Last edited by Lampmaster; Nov 23, 2009 at 08:07 PM.
Lampmaster is online now  
Reply With Quote
Old Nov 25, 2009, 02:40 PM   #2
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

Need more information. Is the inputted variable "n" the number of Fibonacci series numbers that you want, or do you want to generate numbers in the series until you hit "n"?

For instance, if n=3 do you want "0,1,1" (first three numbers in the series) or do you want "0,1,1,2,3" (numbers up to 3 in the series)?
__________________

Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other.


Get more tech news on a wide variety of topics at NextPowerUp
Kreij is offline  
Reply With Quote
Old Nov 25, 2009, 03:07 PM   #3
qamulek
75 Posts
 
qamulek's Avatar
 
Join Date: Feb 2008
Posts: 185 (0.10/day)
Thanks: 51
Thanked 26 Times in 26 Posts

Just a quick guess(based on the limited information as pointed out by kreij):
Quote:
cin >> n;

sum1 = 0;
sum2= 1;
sumtemp;
ii = 0; // Loop control variable
while (ii < n)
{
sumtemp= sum1 + sum2;
sum2=sum1;
sum1=sumtemp
ii++;
} // While loop
//output is in sum1 and *should* contain the nth iteration of the fibonacci sequence
__________________

avatarsuperduper[youtube]: ..."stop lieing feel ur self owned!"
qamulek is offline  
Reply With Quote
The Following User Says Thank You to qamulek For This Useful Post:
Old Nov 25, 2009, 04:20 PM   #4
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

Depending upon your needs, you could also do it in an array as they lend themselves nicely to numeric series...
Code:
cin >> count;

// Create dynamically sized array based on input.
int* fibarray;
fibarray = new int[count];

// Set first two values
fibarray[0] = 0;
fibarray[1] = 1;

int i = 2; // Loop variable which starts with 3rd array member

while (i < count)
{
    fibarray[i] = fibarray[i - 1] + fibarray[i - 2];
    i++;
}
The above code does not do any error checking in the event that the count variable is set to a value less than 2.
__________________

Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other.


Get more tech news on a wide variety of topics at NextPowerUp
Kreij is offline  
Reply With Quote
Old Nov 25, 2009, 06:40 PM   #5
Lampmaster
75 Posts
 
Lampmaster's Avatar
 
Join Date: Aug 2009
Location: Washington The State
Posts: 149 (0.11/day)
Thanks: 35
Thanked 17 Times in 14 Posts

System Specs

Thanks for the replies. What Ijust noticed is I don't need to sum the Fibonacci sequence, I just need to output them. So if n = 3 it would output 1,1,2.
Lampmaster is online now  
Reply With Quote
Old Nov 25, 2009, 06:44 PM   #6
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

Then either of the above two suggestions will work for you as they both use the inputted number as the number of series results you need.
__________________

Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other.


Get more tech news on a wide variety of topics at NextPowerUp
Kreij is offline  
Reply With Quote
The Following User Says Thank You to Kreij For This Useful Post:
Old Nov 25, 2009, 07:04 PM   #7
Lampmaster
75 Posts
 
Lampmaster's Avatar
 
Join Date: Aug 2009
Location: Washington The State
Posts: 149 (0.11/day)
Thanks: 35
Thanked 17 Times in 14 Posts

System Specs

Alright cool, thanks.
Lampmaster is online now  
Reply With Quote
Reply


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for a good Java Programmer Wastedslayer Programming & Webmastering 3 Apr 9, 2009 11:31 PM
Help a noob, X800GTO2 SiCk Graphics Cards 19 Apr 10, 2006 12:47 PM
im a noob at overclockin... help me! karolpl2004 Overclocking & Cooling 12 Dec 3, 2005 11:03 PM


All times are GMT. The time now is 03:17 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
no new posts