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.
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.
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 }
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.
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.
Within your main function, perform the following tasks in order to calculate and display the value for the X5 term of the sequence.
Protip: Make sure you output what is expected, nothing more and nothing less. Otherwise the automated grading script will subtract points!
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.
$ ./program2
Enter a value > 2
Enter a value > 3
The result is 99.50