Background

A sequence is an ordered list of numbers (called terms), whose values can be defined in terms of previous terms in the sequence. As an example:

Xn = 2·Xn−1

In this sequence, the term Xn is defined as two times the previous term Xn−1. If we are given an initial value X1 for the sequence, we are then able to determine any term Xn using this sequence definition. As an example, consider the case when X1 = 3. We can then explicitly list the terms as follows:

3 , 6 , 12 , 24 , 48 , ...

Here you can see that each number is twice the value of the last number in the sequence. We can also define sequences that rely on n preceding terms. In this case, we need to define the first n terms in the sequence to determine the next value. Consider the following example, in which Xn is defined in terms of the previous two terms.

Xn = 2·Xn−2 + Xn−1 X1 = 5 X2 = 2 5 , 2 , 12 , 16 , 40 , ...

Here we can see that 12 = 2·5 + 2, 16 = 2·2 + 12, 40 = 2·12 + 16 and so on. Note that we now need to explicitly define both X1 and X2 before we can determine values for X3 and greater.

Overview

For this lab you will write a program that computes the fifth term (X5) in the sequence

Xn = (Xn - 2)/2 + 3 * Xn-1

In additional to your main function, you will need design several additional custom functions. You must solve this problem using the functions described below. When using real values, you can choose either a float or double data type. Remember to use the top-down design process while writing your program.

Input Function

Write a function called get input to retrieve input from the user. This function should prompt the user for a number and scan the results into a variable. The function should then return the value given by the user.

Protip: If your function does not require any parameters, use the void keyword.
double get_input(void) { // your code here }

Sequence Function

Write a function called get next that gets the next value in the sequence Xn = Xn−2 2 +3·Xn−1.The input to this function should be the previous two values, Xn−2 and Xn−1, and the output should be the calculated next term, Xn.

Print Function

Write a function called print result that will display a number as a result, rounded to two decimal places. The function should receive a real value as input, and print it in the following format:

The result is XXX.XX

where the XXX.XX is the number you received from the input parameter to your print result function. You should format this number to two decimal places using printf.

Protip: Use printf to format your numbers for you. For instance, to limit to four digits after the decimal point, use the format specifier %.4lf in your printf command.
You can read more about formatting options for printf here.

Main Function

Within your main function, perform the following tasks in order to calculate and display the value for the X5 term of the sequence.

  • Make two calls to your get input function and store the results in variables X1 and X2. These will be the initial values for your sequence.
  • Make a call to your function get next function to get X3 using the appropriate arguments. Continue this process, performing function calls until you have a value for X5.
  • Pass your final value X5 to your print result function to complete the program.
Protip: Make sure you output what is expected, nothing more and nothing less. Otherwise the automated grading script will subtract points!

Test

Test your program! You should be able to compare your program’s results against hand generated results to ensure everything is working properly.

Protip: Test your program with various values and make sure it works in all cases. You do not know which values with which we will test your program.

Example Execution

$ ./program2
Enter a value > 2
Enter a value > 3
The result is 99.50
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.