• We've upgraded our forums. Please post any issues/requests in this thread.

Noob programmer help

Joined
Aug 2, 2009
Messages
156 (0.05/day)
Likes
14
Location
Washington The State
System Name Tiny Computer
Processor i7 2600k
Motherboard ASRock H67M-ITX
Cooling Stock for now
Memory 8 GB G.skill
Video Card(s) Unlocked 6950 @ 910/1400
Storage WD Black 1 TB (FAEX)
Display(s) 24" Dell 1920x1080
Case SG07
Power Supply Silverstone 600W
Software Windows 7 x64 Pro
#1
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.

cin >> n;

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

Kreij

Senior Monkey Moderator
Staff member
Joined
Feb 6, 2007
Messages
13,817 (3.48/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#2
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)?
 

qamulek

New Member
Joined
Feb 7, 2008
Messages
184 (0.05/day)
Likes
26
#3
Just a quick guess(based on the limited information as pointed out by kreij):
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
 

Kreij

Senior Monkey Moderator
Staff member
Joined
Feb 6, 2007
Messages
13,817 (3.48/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#4
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.
 
Joined
Aug 2, 2009
Messages
156 (0.05/day)
Likes
14
Location
Washington The State
System Name Tiny Computer
Processor i7 2600k
Motherboard ASRock H67M-ITX
Cooling Stock for now
Memory 8 GB G.skill
Video Card(s) Unlocked 6950 @ 910/1400
Storage WD Black 1 TB (FAEX)
Display(s) 24" Dell 1920x1080
Case SG07
Power Supply Silverstone 600W
Software Windows 7 x64 Pro
#5
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.
 

Kreij

Senior Monkey Moderator
Staff member
Joined
Feb 6, 2007
Messages
13,817 (3.48/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#6
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.
 
Joined
Aug 2, 2009
Messages
156 (0.05/day)
Likes
14
Location
Washington The State
System Name Tiny Computer
Processor i7 2600k
Motherboard ASRock H67M-ITX
Cooling Stock for now
Memory 8 GB G.skill
Video Card(s) Unlocked 6950 @ 910/1400
Storage WD Black 1 TB (FAEX)
Display(s) 24" Dell 1920x1080
Case SG07
Power Supply Silverstone 600W
Software Windows 7 x64 Pro
#7
Alright cool, thanks.