Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

## Purpose In this lab you will explore sequences ( arithmetic and geometric ) by implementing explicit and recurrence formulas using for loops and recursive

## Purpose
In this lab you will explore sequences (arithmetic and geometric) by implementing explicit and recurrence formulas using for loops and recursive functions.
## Code Block
Look over the code below. It is written in C not C++ but it should be fairly straightforward to read if you know C++. Answer the questions below and write the code. I will refer to the for statments by the order in which they appear in the code for instance block one refers to the first for loop.
#include
int a(int a0, int d, int i)
{
return d*i + a0;
}
int recurrencea(int anminusone, int d)
{
return anminusone + d;
}
int fibb(int n)
{
if(n ==0|| n ==1){
return 1;
}
return n*fibb(n-1);
}
int main()
{
for(int i=0; i<=5; i++){
printf("%d
", i);
}
printf("
");
for(int i=0; i<=5; i++){
printf("%d
",a(17,3, i));
}
printf("
");
int a =17;
printf("%d
", a);
for(int i=0; i<5; i++){
printf("%d
", recurrencea(a,3));
a = recurrencea(a,3);
}
printf("
");
for(int i=0; i<6; i++){
printf("%d
", fibb(i));
}
return 0;
}
0
1
2
3
4
5
17
20
23
26
29
32
17
20
23
26
29
32
1
1
2
6
24
120
## Questions
1. Blocks 2 and 3 generate the same output but their `for` loops are not the same. How are the loops different and why? Could you rewrite block 3 so that the bounds of the loop are the same as in block 2?
1. Implement a `for` loop which generates and prints the 16th through 27th terms of the geometric sequence that starts at 18.25 and has a common ratio of 1.5.
1. Implement a function that uses a recurrence relation to achieve the same as above.
1. Write a short program which stores the 3rd through 99th terms of the arithmetic sequence which starts at 54321 and has a common difference of -33.2 in an array. Generate the terms using a function that implements a recurrence relation using the previous element of the array.
1. Write a short program using a recursive function that generates the terms of the sequence a_n = a_n-1* a_n-2. Generate the first twenty terms of the sequence if a_1=3 and a_2=0.5. Store the elements in an array. Print the elements in the array.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

4. Identify the challenges facing todays organizations

Answered: 1 week ago