Question

In: Computer Science

Here is a program that computes Fibonacci series. Can you add comments to variables and functions...

Here is a program that computes Fibonacci series.
Can you add comments to variables and functions of every functions? For example, what are these variables meaning? What "for" function do on this program?
Describe important section of code to text file. For example, which section of this code important? Why?
Thank you all of help.

#include<iostream>
using namespace std;

int main()
{
   int arr[100];
   
   int n1, n2;
   cin>>n1;
   
   arr[0] = 0;
   arr[1] = 1;
   for(int i = 2;i<n1;i++){
      arr[i] = arr[i-1]+arr[i-2];
   }
   cout<<arr[n1-1]<<endl;
   cin>>n2;
   
   bool found = false;
   for(int i = 0;i<n1;i++){
      if(arr[i] == n2){
         found = true;
         break;
      }
   }
   
   if(found){
      cout<<n2<<" is a Fibonacci number"<<endl;
   }
   else{
      cout<<n2<<" is not a Fibonacci number"<<endl;
   }
   
   for(int i = 0;i<10 && i<n1;i++){
      cout<<arr[i]<<" ";
   }
   cout<<endl;
   return 0;
}

Solutions

Expert Solution

#include<iostream>

using namespace std;

int main() {
    int arr[100];

    int n1, n2;
    cin >> n1;  // how many fibonacci numbers to be generated

    arr[0] = 0; // initialize first fibonacci number to 0
    arr[1] = 1; // initialize first fibonacci number to 1
    for (int i = 2; i < n1; i++) {  // run this loop n1-2 times
        /**
         * we know that f(n) = f(n-1) + f(n-2)
         * arr[i-1] represents f(n-1)
         * arr[i-2] represents f(n-2)
         * so, generate new fibonacci number f(n) by adding last two elements from array which represents f(n-1) and f(n-2)
         */
        arr[i] = arr[i - 1] + arr[i - 2];
    }
    cout << arr[n1 - 1] << endl;    // print n1th fibonacci number, which was generated using last for loop
    cin >> n2;  // read a number from user to check if it's a fibonacci number

    bool found = false;     // initially set found to false, indicating that the number is not found in the array
    for (int i = 0; i < n1; i++) {  // go through all elements in the array
        if (arr[i] == n2) {     // if any of the elements of the array equals the entered number n2 
            found = true;   // then set found is true
            break;
        }
    }

    if (found) {    // if number was found in the given for loop
        cout << n2 << " is a Fibonacci number" << endl; // then print that the number is a fibonacci number
    } else {    // if number was not found in the given for loop
        cout << n2 << " is not a Fibonacci number" << endl; // then print that the number is not a fibonacci number
    }

    for (int i = 0; i < 10 && i < n1; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
    return 0;
}

Related Solutions

( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence...
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence - 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … what is the initialization of a, b, and d? - a b d 1 ? ? 1 2 ? ? 1 3 1 1 2 4 1 2 3 5 2 3 5 6 3 5 8 7 5 8 13 wrong initialization - a b d 1 0 1 1 2...
java programing project Implement a program that computes the Fibonacci of a specified number, the factorial...
java programing project Implement a program that computes the Fibonacci of a specified number, the factorial of a specified number, or estimates the value of 'e' using the specified number of iterations (of a Taylor series). Please feel free to use the Internet to find resources that explain how to estimate the value of 'e' using a Taylor series. In the case of no or invalid number of parameters, the program should show help instructions that looks like: --- Assign...
hi,I have this C++ program,can someone add few comments with explanation what is the logic and...
hi,I have this C++ program,can someone add few comments with explanation what is the logic and what is what?thank you I m total beginner #include <iostream> using namespace std; int ArraySum(int MyArray[], int size){ int* p = MyArray; int sum = 0; while(p<MyArray+size){ sum += *p; p++; } return sum; } int main() { int MyArray[10] = {4, 0, 453, 1029, 44, 67, 111, 887, 4003, 1002}; cout<<ArraySum(MyArray,10); return 0; }
For this program you will add and test 2 new member functions to the class in...
For this program you will add and test 2 new member functions to the class in //************************ intSLList.h ************************** // singly-linked list class to store integers #ifndef INT_LINKED_LIST #define INT_LINKED_LIST class IntSLLNode { public: IntSLLNode() { next = 0; } IntSLLNode(int el, IntSLLNode *ptr = 0) { info = el; next = ptr; } int info; IntSLLNode *next; }; class IntSLList { public: IntSLList() { head = tail = 0; } ~IntSLList(); int isEmpty() { return head == 0; }...
The first program is to count the number of zeros in $3855. Please     add comments...
The first program is to count the number of zeros in $3855. Please     add comments to each line                 org   $1000 array     db $38, $55 ; data to be tested                      org $1100 zero_cnt ds.b   1 lp_cnt     ds.b   1              org   $1500              clr   zero_cnt       ;initialize the 0 count to 0              movb #16,lp_cnt                   ldd   array         again     lsrd             bcs   chk_end         ;             inc   zero_cnt chk_end   dec   lp_cnt                           bne   again                               swi                          end
Develop a python program to create a quiz with limited time and attempts!!! Add comments and...
Develop a python program to create a quiz with limited time and attempts!!! Add comments and screenshot of running the program quiz could be of anything like , what's the sum of 2&2. There should be number of attempts(limited) suppose if the answer is wrong.
Python3  Please add comments Write a program that implements the word guessing game - There is a...
Python3  Please add comments Write a program that implements the word guessing game - There is a secret word - The user enters letters one at a time, if the letter appears in the secret word, it is revealed. Otherwise, it counts as a wrong guess. If the user reveals all the letters in the word before getting too many wrong guesses then they win! Otherwise, they lose. 1 - define secret word 2 - create a revealed letter list that...
****user comments: PLEASE READ INSTRUCTIONS THOROUGHLY AND KEEP THE STRUCTURE OF THE PROGRAM. JUST ADD THE...
****user comments: PLEASE READ INSTRUCTIONS THOROUGHLY AND KEEP THE STRUCTURE OF THE PROGRAM. JUST ADD THE ADDITIVES NEEDED IN ORDER TO SUFFICE THE PROGRAM. PLEASE MAKE SURE IT IS IN C++ AND WORKS! THANK YOU!**** Write a program that uses a structure to store the following information for a particular month at the local airport: Total number of planes that landed Total number of planes that departed Greatest number of planes that landed in a given day that month Least...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
Create a simple dice game in Java. Add screenshots and the program here.
Create a simple dice game in Java. Add screenshots and the program here.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT