Author |
Topic: fibonacci numbers... (Read 1436 times) |
|
Arcadi_04
Newbie
Gender:
Posts: 1
|
|
fibonacci numbers...
« on: Apr 27th, 2004, 10:59am » |
Quote Modify
|
I'm a c ++ student but also a complete noob in computer programming lol the project was to display the fibonacci numbers less than 100 and their sum, you can apply any method you know to solve this problem. I was able to create this basic thing, but no output is comming out T__T i run it and that screen thats suppose to show up doesn't: #include <iostream> int main () { int x,y,z; x=0; y=1; while(z<100) { cout << z; y=z + x; x=z; z=y; } } i'm just looking for a few leads here and there.
|
|
IP Logged |
|
|
|
Barukh
Uberpuzzler
Gender:
Posts: 2276
|
|
Re: fibonacci numbers...
« Reply #1 on: Apr 27th, 2004, 11:09am » |
Quote Modify
|
on Apr 27th, 2004, 10:59am, Arcadi_04 wrote:i'm just looking for a few leads here and there. |
| Arcadi, are you sure your program executes the while loop at least once?
|
|
IP Logged |
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: fibonacci numbers...
« Reply #2 on: Apr 27th, 2004, 1:01pm » |
Quote Modify
|
It ought to.. Of course most C++ implementations don't actually do everything they ought to.. (like initialize variables to their default value when it goes unspecified)
|
|
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
John_Gaughan
Uberpuzzler
Behold, the power of cheese!
Gender:
Posts: 767
|
|
Re: fibonacci numbers...
« Reply #3 on: Apr 27th, 2004, 7:39pm » |
Quote Modify
|
The first thing that comes to mind is you need to initialize z. Non-static local variables are not guaranteed to be intialized to any specific value. Some compilers may initialize to 0, 0.0, false, or '\0', depending on the type, but there is no requirement to do so. When main() executes, the program loader (for lack of the proper term ) allocates stack storage for local variables. All it does is move the stack pointer down, it does not erase the values already there (except that it sets the return address and any parameters passed in). If you do not set them explicitly yourself, their value is whatever bits are set from whatever used them last time. Please note that static variables are handled differently. These have special storage specification and are initialized before main() starts.
|
|
IP Logged |
x = (0x2B | ~0x2B) x == the_question
|
|
|
Mugwump101
Junior Member
Gender:
Posts: 61
|
|
Re: fibonacci numbers...
« Reply #4 on: Apr 27th, 2004, 8:12pm » |
Quote Modify
|
on Apr 27th, 2004, 11:09am, Barukh wrote: Arcadi, are you sure your program executes the while loop at least once? |
| well it should right? how can you tell T__T do i need a counter of some-sort? And john what you mean is that i create a function prototype before the main program right? to define z....i think.....
|
|
IP Logged |
"When I examine myself and my methods of thought, I come to the conclusion that the gift of fantasy has meant more to me than my talent for absorbing positive knowledge. "~ Albert Einstein
|
|
|
John_Gaughan
Uberpuzzler
Behold, the power of cheese!
Gender:
Posts: 767
|
|
Re: fibonacci numbers...
« Reply #5 on: Apr 27th, 2004, 8:49pm » |
Quote Modify
|
on Apr 27th, 2004, 8:12pm, Mugwump101 wrote:And john what you mean is that i create a function prototype before the main program right? to define z....i think..... |
| Protypes are evaluated at compile time, resolved by the linker, and never actually "run." I think I got into too much detail. Here is a simple version of what happens. When your program runs, there is a function that runs before main(). The linker links this in invisibly. It sets up argc/argv, initializes stuff, and calls main(). Well, when you have static variables, they must be initialized somewhere. You do not have to initialize them because it does not always make sense to do so. This "invisible" function initializes static variables for you before your program even exists. If you have static const class members this is different. Those must be initialized explicitly. The linker should tell you if you do not. I think part of the confusion comes from the fact that I can look at C++ code and even classes and tell you generally how it will look in assembly code. I think like a processor does. When I see a static variable, I imagine it as residing in the data segment. Not everyone thinks like I do, especially people who do not understand assembly code. I have actually programmed computers in machine code, pure binary (well, hexadecimal, but close enough). That is just one of the benefits of a college education in computer science
|
|
IP Logged |
x = (0x2B | ~0x2B) x == the_question
|
|
|
John_Gaughan
Uberpuzzler
Behold, the power of cheese!
Gender:
Posts: 767
|
|
Re: fibonacci numbers...
« Reply #6 on: Apr 27th, 2004, 8:55pm » |
Quote Modify
|
on Apr 27th, 2004, 8:12pm, Mugwump101 wrote:well it should right? how can you tell T__T do i need a counter of some-sort? |
| Oh, I forgot to mention in my last post... that loop already has a counter, z. Loops must have two characteristics: you test something between each iteration (in this case, z<100), and between tests, you change the state of whatever you test (z=y). This is the general case. You could do file I/O and even put both in the loop test: Code:string str; istream in; while ((str = getline (in)) {} |
| This tests whether there is any more data left in the stream, and it changes the state of the stream by extracting data from it.
|
|
IP Logged |
x = (0x2B | ~0x2B) x == the_question
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: fibonacci numbers...
« Reply #7 on: Apr 28th, 2004, 1:31am » |
Quote Modify
|
on Apr 27th, 2004, 7:39pm, John_Gaughan wrote:Some compilers may initialize to 0, 0.0, false, or '\0', depending on the type, but there is no requirement to do so. |
| I'm pretty sure it is in the ANSI C++ standard
|
« Last Edit: Apr 28th, 2004, 1:37am by towr » |
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
John_Gaughan
Uberpuzzler
Behold, the power of cheese!
Gender:
Posts: 767
|
|
Re: fibonacci numbers...
« Reply #8 on: Apr 28th, 2004, 5:27pm » |
Quote Modify
|
on Apr 28th, 2004, 1:31am, towr wrote:I'm pretty sure it is in the ANSI C++ standard |
| I cannot vouch for the standard, but this is what I was always told. I googled and came up with this: Google search one of the sites from the search You may be correct. These links are not the standard, they are just what some people wrote. But they do agree with my position
|
|
IP Logged |
x = (0x2B | ~0x2B) x == the_question
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: fibonacci numbers...
« Reply #9 on: Apr 28th, 2004, 11:21pm » |
Quote Modify
|
Well, considering no compiler seems compelled to adhere to the standard anyway it's generally better to be safe than sorry..
|
|
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
John_Gaughan
Uberpuzzler
Behold, the power of cheese!
Gender:
Posts: 767
|
|
Re: fibonacci numbers...
« Reply #10 on: Apr 29th, 2004, 6:08am » |
Quote Modify
|
Very true. I do not trust computers, despite being a computer scientist. Actually, that may explain my feelings. Anyway, I always initialize variables because it is good practice. I program in a variety of languages each with its own quirks and each one is different. If I always initialize I save myself from my own idiocy. Of course, I still want to know what the standard says. I think I might ask one of the GCC lists. They probably know.
|
|
IP Logged |
x = (0x2B | ~0x2B) x == the_question
|
|
|
John_Gaughan
Uberpuzzler
Behold, the power of cheese!
Gender:
Posts: 767
|
|
Re: fibonacci numbers...
« Reply #11 on: Apr 29th, 2004, 10:16am » |
Quote Modify
|
I asked on a GCC list and people who actually have the standard said that section 5.3.4/15 specifies that heap memory is not initialized, and 8.5/9 specifies that stack memory is not initialized. I do not have the standard so I cannot quote it, but I think Icarus does. Anyway, like I said before, I think it is always a good idea to initialize primitive variables explicitly no matter what language and what the situation. If nothing else, it leaves a paper trail showing where the variable got its value. In C++ specifically it helps get rid of compiler warnings when using strict language compliance.
|
|
IP Logged |
x = (0x2B | ~0x2B) x == the_question
|
|
|
|