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...
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; }
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.
****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.
**Add comments to existing ARM code to explain steps** Write an ARM assembly program to convert...
**Add comments to existing ARM code to explain steps** Write an ARM assembly program to convert temperatures from Celsius to Fahrenheit or from Fahrenheit to Celsius. Here are the two formulas for your reference. Use variable to read and store values. C= 5* (F - 32) / 9 F = (9 * C / 5 ) + 32 My code below: TempConvert.s LDR R8,=temperature LDR R1,[R8] LDR R8,=unit LDRB R2,[R8] LDR R8,=celsius LDRB R3,[R8] LDR R8,=fahrenheit LDRB R4,[R8] MOV R6,#9...
C++ Download the attached program and complete the functions. (Refer to comments) main.cpp ~ #include #include...
C++ Download the attached program and complete the functions. (Refer to comments) main.cpp ~ #include #include #define END_OF_LIST -999 using namespace std; /* * */ int exercise_1() { int x = 100; int *ptr; // Assign the pointer variable, ptr, to the address of x. Then print out // the 'value' of x and the 'address' of x. (See Program 10-2) return 0; } int exercise_2() { int x = 100; int *ptr; // Assign ptr to the address of...
Q1:Write a Java program to find Fibonacci Series using 1) using for loop 2) using while...
Q1:Write a Java program to find Fibonacci Series using 1) using for loop 2) using while loop To Understand what the Fibonacci series is: The Fibonacci sequence is a series of numbers where a number is the sum of the previous two numbers. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. Q2: Write a Java program to find the Factorial of a number using a while loop. To...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT