Question

In: Computer Science

Lab 11 C++ Download the attached program and run it - as is. It will prompt...

Lab 11 C++

Download the attached program and run it - as is. It will prompt for a Fibonacci number then calculate the result using a recursive algorithm. Enter a number less then 40 unless you want to wait for a very long time.

Find a number that takes between 10 and 20 seconds. (10,000 - 20,000 milliseconds).

  1. Modify the program to use the static array of values to "remember" previous results. Then when the function is called again with the same value, simply return the value. You might think of this as the number of base cases expanding dynamically as the program runs.
/*
 * CS 1337 Lab Assignment 11
 * 
 * Dynamic Programming Example
 * 
 * Use dynamic programming do make the Fib() function more usable for large 
 * values of n. This program has a timer that will calculate the time it 
 * takes to run the function.  Use that to compare the before and after times. 
 * 
 */
#include 
#include 
#include 

// ...

using namespace std::chrono;

milliseconds get_time() {
    milliseconds ms = duration_cast< milliseconds >(
        system_clock::now().time_since_epoch()
    );
    return ms;
}

using namespace std;


int Fib(int n) {
    // Keep track of calculated values in a static array.  
    static int values[100] = {0}; 
    int retval;
    
    // base cases.  
    if (n<=0) return 0;
    if (n==1) return 1; 

    retval = Fib(n-1) + Fib(n-2); 

    return retval;
}
/*
 * 
 */
int main(int argc, char** argv) {
    
    int val, n;
    duration time_span;
    high_resolution_clock::time_point start, end; 
    
    cout << "Enter a number less the 100: "; 
    cin >> n;

    start = high_resolution_clock::now();
    val = Fib(n);
    end = high_resolution_clock::now();

    time_span = end - start;

    cout << "Fib(" << n << ") is : " << val << endl;
    cout << "Elapsed time in milliseconds: " << time_span.count() << endl;
    return 0;
}

Solutions

Expert Solution

// do comment if any problem arises

// code

/*

* CS 1337 Lab Assignment 11

*

* Dynamic Programming Example

*

* Use dynamic programming do make the Fib() function more usable for large

* values of n. This program has a timer that will calculate the time it

* takes to run the function.  Use that to compare the before and after times.

*

*/

#include <iostream>

#include <chrono>

#include <vector>

// ...

using namespace std::chrono;

milliseconds get_time()

{

    milliseconds ms = duration_cast<milliseconds>(

        system_clock::now().time_since_epoch());

    return ms;

}

using namespace std;

int Fib(int n)

{

    // Keep track of calculated values in a static array.

    static int values[100] = {0};

    values[1] = 1;

    if (values[n] != 0 || n == 0)

        return values[n];

    values[n] = Fib(n - 1) + Fib(n - 2);

    return values[n];

}

/*

*

*/

int main(int argc, char **argv)

{

    int val, n;

    duration<double> time_span;

    high_resolution_clock::time_point start, end;

    cout << "Enter a number less the 100: ";

    cin >> n;

    start = high_resolution_clock::now();

    val = Fib(n);

    end = high_resolution_clock::now();

    time_span = end - start;

    cout << "Fib(" << n << ") is : " << val << endl;

    cout << "Elapsed time in milliseconds: " << time_span.count() << endl;

    return 0;

}

Output using previous program:

Output using dynamic programming:


Related Solutions

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...
Lab 1 Write a C program for a grade calculation to run on ocelot. The source...
Lab 1 Write a C program for a grade calculation to run on ocelot. The source file should have your name & PantherID included in it and it should have an affirmation of originality stating something like: “I affirm that I wrote this program myself without any help form any other people or sources from the internet.”. Code should be nicely indented using a consistent style and commented appropriately. An array should be used for each student, but it is...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
10.8 LAB*: Program: Data visualization (1) Prompt the user for a title for data. Output the...
10.8 LAB*: Program: Data visualization (1) Prompt the user for a title for data. Output the title. (1 pt) Ex: Enter a title for the data: Number of Novels Authored You entered: Number of Novels Authored (2) Prompt the user for the headers of two columns of a table. Output the column headers. (1 pt) Ex: Enter the column 1 header: Author name You entered: Author name Enter the column 2 header: Number of novels You entered: Number of novels...
6.30 LAB*: Program: Authoring assistant (1) Prompt the user to enter a string of their choosing....
6.30 LAB*: Program: Authoring assistant (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be more shuttle...
Add an item to a Text File / C++ the program will prompt for a filename...
Add an item to a Text File / C++ the program will prompt for a filename AND then prompt for a text file item. text file items always contain an unknown amount of spaces, (see getline). Allow the user to put both the filename and the extensions, like groceries.txt or retirementToDo.clist Don't add any directory information to the filenames, use them as typed. If the file does exist, the item will be APPENDED to the end of the file. If...
I am writing a shell program in C++, to run this program I would run it...
I am writing a shell program in C++, to run this program I would run it in terminal like the following: ./a.out "command1" "command2" using the execv function how to execute command 1 and 2 if they are stored in argv[1] and argv[2] of the main function?
8.30 LAB*: Program: Authoring assistant. PYTHON PLEASE!! (1) Prompt the user to enter a string of...
8.30 LAB*: Program: Authoring assistant. PYTHON PLEASE!! (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be...
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT