I'm taking a C++ programming class this semester. Our professor gave us an assignment to be able to enter a number and compute Fibonnacci numbers, you know 1,1,2,3,5,8,13,21,34,55,89,144..... We are supposed to make a class, called HugeNumber to do this and my question is am I supposed to take this class and implement it into a C++ program then compile sort of like a Java client or will this HugeNumber class do all the computing within itself and all I would have to do is compile the class, ? Any websites where I can get some examples of classes that I could download and compile or implement to get some practice?
My guess would be that since it is a C++ class, the HugeNumber class should do all the work. But honestly, this sounds like a question that only your professor can answer.
I already asked him and he tried to explain but he is from a different country and has a very firm accent which makes it difficult to understand him most of the time.
My recommendation is: 1) Understand what this exercise is trying to teach you. 2) Send your professor an e-mail if you have a hard time understanding him. Based on what you wrote, here is my interpretation. I think that you are being asked to calculate the Nth element of a Fibinocci sequence. For example, the second element is 1, the third element is 2, but what is the billionth element? In C++, the maximum value of an unsigned long on a 32 bit operating system is 4294967295. So I think that you are being asked to implement a C++ class that you can use to hold integers greater than that of an unsigned long. Again, I would ask your professor. Good luck!
He's probably trying to get you to implement the function and then call it recursively to do the calculations. I've got a link to a website that has a sample implementation of this. Let me know if you need it...of course it would be for comparison purposes...to see if your working code is similar to someone else's.
Yeah go ahead and give it to me since I already sent it in this morning. Hey super_mario that's what he said he wants but was I supposed to write two programs instead of the one I wrote?
You are some confused dude ... mixing C++ with Java, .... 2 programs .... I have to read between the line and here is your assignment: Write a C++ program with a class called Hugenumber that compute the Fibonnacci numbers. The class takes in 1 argument: the number that you want to compute the F number for. You can feed this number to your class thru command line argument to your main program, which instantiates an object of the Hugenumber class. Your prof. probably wants you print the input and the output too. You compile a C++ program with a C++ compiler. It produces an executable program that runs stand-alone. It has nothing to do with Java, the Web ...
You should have make an class in the same file that your main function is in, and your main function will call the class, and pass the argument to the constructor of the class. I haven't been use C++ in a while now, but that "HugeNumber" class's main fuction should be little something like this: int HugeNumber (int n) { if (n == 0 || n == 1) return n; else return HugeNumber (n - 1) + HugeNumber (n - 2); }
OK, Legend that is what I was asking, does something extra need to go in the program and what you have told me seems to make sense. I already have my Fibonnaci program written out I just don't think it's what the professor wants at the moment. He wants to print the huge number and he also wants two huge numbers added together and he also wants us to limit the digits to 300. Basically what I have got, so far, is how to calculate Fibonnaci of a integer given by the user and print that out one huge number but within my program I have no calculations yet but that shouldn't be a problem but how do I set a limit of 300? Oh ragingfire thanks for your info but I know it has nothing to do with Java but I can handle Java pretty well, this is my first stab at C++ and a TA told me that the two languages were some what similar so I thought a C++ class may be like a Java Client but I guess I was incorrect.
This thread is frying my brain. I graduated from CS in 1998 and I haven't touched a C++ book since. I don't understand anything anyone is saying.
Limit to 300 is easy, you just have to add another condition in the loop that checks if the number is smaller than 300, like do{blah blah blah}while(n < 300); .
Hey Legend do you think this is good code: I don't know but something is telling me I'm not using a class in this function, what do you think? #include iostream. h> long fibonacci( long ); int main() { long result, number; cout << "Enter an integer: "; cin >> number; result = fibonacci( number ); cout << "Fibonacci(" << number << ") = " << result << endl; return 0; } long fibonacci( long n ) { if ( n == 0 || n == 1 ) // base case return n; else // recursive case return fibonacci( n - 1 ) + fibonacci( n - 2 ); }
I don't have a c++ compiler, so I can't tell you if it's going to run or not. But first of all, you didn't use a class, you only creat a function, which was not what your professor asked for. And I wouldn't use recursion if you going to pass reference through parameter.
Well I have one and the program compiles and runs fine I was just asking you to confirm what you did, that I did not use a class. How do I eliminate recursion and what exactly do you mean pass reference through a parameter? Damn it I thought I was close! Is there some place like a chat room or messenger service (ie Yahoo!, MSN) I can talk to you on so I don't post any more questions here?
Well, if it runs fine and have no logical error, then I guess you just have to have a class, and to tell you the truth, I pretty much forgot how to use class in C++, so I went back to the book from CS1, and I think you have do this: class HugeNumber { private: long n; public: long fibonacci(); }; int main() { long result, number; HugeNumber fibo; cout << "Enter an integer: "; cin >> number; result = fibo.fibonacci( number ); cout << "Fibonacci(" << number << ") = " << result << endl; return 0; } long HugeNumber::fibonacci( long n ) { if ( n == 0 || n == 1 ) // base case return n; else // recursive case return fibonacci( n - 1 ) + fibonacci( n - 2 ); }