Question

** C++ LANGUAGE ** Write a function that will implement each Fibonacci number with the help...

** C++ LANGUAGE **

Write a function that will implement each Fibonacci number with the help of an integer array of size 100 (elements of this array will be digits of the Fibonacci number).

When the function is called to find , it will calculate all Fibonacci numbers from to using the basic formula . To add two Fibonacci numbers, the function will add elements of two arrays corresponding to and and store their sums in the array corresponding to . (You are NOT supposed to store ALL arrays.)

Write a program that will calculate Fibonacci numbers containing up to 100 digits. Display the biggest Fibonacci number that has less than 100 digits. Make sure that your program will display the error message when a Fibonacci number has 100 digits or more.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ program

#include <iostream>
using namespace std;

class long_number {
static const int max_digits = 100;
int number[max_digits];
int size;

public:
   long_number(){
       size = 0;
       for (int i = 0; i < max_digits; i++) {
               number[i] = 0;
        }
   }
   long_number(string input){
       size = input.length() ;
       for (int i = 1; i <= size; i++)
           number[max_digits- i] = (int)input[size - i] - (int)'0';
  
   }
long_number operator+(long_number rhs) {
           int carry =0;
           int i = max_digits -1;
           long_number ob;
           for (; i >= max_digits - max(size, rhs.size); i-- ) {
                   int sum = number[i] + rhs.number[i] + carry;
                   ob.number[i] = sum % 10;
                   carry = sum / 10;
               }
           ob.number[i] = carry;
           ob.size = max(size, rhs.size);
           if (carry > 0)ob.size++;
           return ob;
   }
   void print(){
       for (int i = max_digits - size ; i < max_digits; i++) {
       cout << number[i];
       }
       cout << endl;
   }
  
   int length(){
       return size;
   }

};

int main(){
   long_number a,c;
   long_number b("1");
  
   while(b.length()<=100){
       c = a+b;
       a=b;
       b=c;
   }
   a.print();
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
** C++ LANGUAGE ** Write a function that will implement each Fibonacci number with the help...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Program in C language for: 1. Create a C program to read 7 integers...

    Write a Program in C language for: 1. Create a C program to read 7 integers and store them in an array. Next, the program is to check if the array is symmetric, that is if the first element is equal to the last one, the value of the second one is equal to the value of the last but one, and so on. Since the middle element is not compared with another element, this would not affect the symmetry...

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • programing C,already write part of code (a) Write a function print.array that receives an array of...

    programing C,already write part of code (a) Write a function print.array that receives an array of real numbers and the number of el stored in the array. This function must display the elements of the array in order with each one on a line by itself in a field width of 7 with two decimal places. (See sample output. Recall the format specifier would be "%7.21f"). (b) Sometimes we want to treat an array as a mathematical vector and compute...

  • Write a c program to implement Quicksort for arrays of length 16, as described in the...

    Write a c program to implement Quicksort for arrays of length 16, as described in the textbook, taking always as the pivot the last element of the corresponding subarray. Define a variable COUNTER in your program that keeps track of the total number of comparisons among the elements in the array needed to sort the array Run your program 100 times using as inputs 100 arrays of real numbers in the interval [0, 11 generated randomly. Find the sample average...

  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

  • • Write a JavaFX code that generates Fibonacci number sequence of a certain length (-100 numbers)...

    • Write a JavaFX code that generates Fibonacci number sequence of a certain length (-100 numbers) • The program then uses bucket sort technique to sort the Fibonacci numbers based on the order of their division by the digits 2,3,4,5,6,7,8 and 9 (→ 08 digits) • The sorting order is defined based on the divisibility of numbers starting from 2,3,4 and onwards. As a result of sorting, some of the Fibonacci numbers will be in more than one bucket because...

  • Write an ARM assembly language subroutine (named nfibo) to calculate and return the n-th Fibonacci number....

    Write an ARM assembly language subroutine (named nfibo) to calculate and return the n-th Fibonacci number. Fibonacci numbers (or a Fibonacci sequence) are a series of numbers with a property that the next number in the series is a sum of previous two numbers. Starting the series from 0, 1 as the first two numbers we have 0, 1, (0 + 1) = 1, (1 + 1) = 2, (1 + 2) = 3, (2 + 3) = 5, (3...

  • Assembly Language Program Help Write a procedure named CountNearMatches that receives pointers to two arrays of...

    Assembly Language Program Help Write a procedure named CountNearMatches that receives pointers to two arrays of signed doublewords, a parameter that indicates the length of the two arrays, and a parameter that indicates the maximum allowed difference (called diff) between any two matching elements. For each element x(i) in the first array, if the difference between it and the corresponding y(i) in the second array is less than or equal to diff, increment a count. At the end, return a...

  • (C++) Write a function, remove, that takes three parameters: an array of integers, the number of...

    (C++) Write a function, remove, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, removeItem). The function should find and delete the first occurrence of removeItem in the array. (Note that after deleting the element, the number of elements in the array is reduced by 1.) Assume that the array is unsorted. Also, write a program to test the function. Your program should prompt the user to enter 10 digits...

  • Write a set of C++ programs to implement the following operations listed in the menu function...

    Write a set of C++ programs to implement the following operations listed in the menu function below. a. File: main.cpp - contains the main function b. File: arrayhw.cpp – contains the 1D array function implementations c. File: arrayhw.h – contains the 1D array function prototyped declarations d. May create other files to implement any other needed function The main menu function should display the following: Main Menu, 1D Array Functions Enter a number to choose one of the following actions:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT