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

For C++ Download the attached program template. The programs calculates the amount of profit or loss...
For C++ Download the attached program template. The programs calculates the amount of profit or loss from a stock transaction. Using the values initialized by the program, calculate and print out the following results: "Total paid for stock" "Purchase commission " "Total received for stock" "Sales commission" "Amount of profit or loss" Use the following formulas: Total_paid = (total purchase price) + purchase_commission; Total_received = (total sales price) - sales_commission; profit = Total_received - Total_paid /* * COSC 1337 Programming...
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...
Write a C program Your program will prompt the user to enter a value for the...
Write a C program Your program will prompt the user to enter a value for the amount of expenses on the credit card. Retrieve the user input using fgets()/sscanf() and save the input value in a variable. The value should be read as type double. The user may or may not enter cents as part of the input. In other words, expect the user to enter values such as 500, 500.00 and 500.10. The program will then prompt the user...
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...
write a program to perform the following in C Your program should prompt the user to...
write a program to perform the following in C Your program should prompt the user to enter ten words, one at a time, which are to be stored in an array of strings. After all of the words have been entered, the list is to be reordered as necessary to place the words into alphabetical order, regardless of case. Once the list is in alphabetical order, the list should be output to the console in order. The program should execute...
C++ please Instructions Download and modify the Lab5.cpp program. Currently the program will read the contents...
C++ please Instructions Download and modify the Lab5.cpp program. Currently the program will read the contents of a file and display each line in the file to the screen. It will also display the line number followed by a colon. You will need to change the program so that it only display 24 lines from the file and waits for the user to press the enter key to continue. Do not use the system(“pause”) statement. Download Source Lab 5 File:...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT