#Geometric sequence in cpp

54 messages · Page 1 of 1 (latest)

trail zealot
#

I've been tasked to create a function for geometric sequence with recursion. I don't know how to start.

heavy pondBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

trail zealot
#
#include <iostream>

int geometricSequence(int firstTerm, int ratio, int n) {
    
    
    return geometricSequence(firstTerm, ratio, n);
}
int main() {
    
    geometricSequence();
    return 0;
}
#

i have this so far

neat summit
neat summit
#

I haven't solved nor particularly helped you, yet, have I?

trail zealot
#

no no i meant thank you in advance for helping out

neat summit
#

Okay, so do you know how a recursive function generally looks like?
Like, what are the 2 important parts every recursive function has?

trail zealot
#

i do not

#

ik it calls itself

#

perhaps

neat summit
trail zealot
#

i do not know the 2nd one

neat summit
#

But as you can see, right now your function just infinitely calls itself

trail zealot
#

an end ?

#

after its done iterating ?

heavy pondBOT
#

@trail zealot Has your question been resolved? If so, type !solved :)

neat summit
#

Yes. The "end" in a recursive function is the simplest problem to which we already know the solution: The base case.

For example, here is a recursive implementation of the fibonacci sequence:

int fibonacci(unsigned n) {
    if (n == 0) return 0;  // base case 1
    if (n == 1) return 1;  // base case 2

    int fib_n_minus_1 = fibonacci(n - 1);  // recursive call 1
    int fib_n_minus_2 = fibonacci(n - 2);  // recursive call 2
    return fib_n_minus_1 + fib_n_minus_2;
}

// or the same function, but a bit shorter:
int fibonacci(unsigned n) {
    if (n <= 1) return n;  // base cases
    
    return fibonacci(n - 1) + fibonacci(n - 2);  // recursive calls
}

When the input is 0 or 1, then we know the answer already, because these are the most simple cases. Because we don't want to hardcode EVERY answer, we'll use recursion to define fibonacci numbers for higher n. Recursion takes a hard problem like fibonacci(2) and turns it into an easier problem (or in this case 2 easier problems), that we can then immediately solve.

The key trick with recursion is to just assume the recursive call works and only worry about the base cases first.

trail zealot
#

ahh

#

so in this case, it would be the first term

neat summit
#

Btw, the formula for the fibonacci sequence is:

trail zealot
#

right

#

basically the sum of the 2 numbers before it

#
#include <iostream>

int geometricSequence(int firstTerm, int ratio, int n) {
    if (n == 1) {
        return firstTerm;
    }

    return geometricSequence(firstTerm, ratio, n);
}
int main() {
    int firstTerm, ratio, n;
    geometricSequence(firstTerm, ratio, n);
    return 0;
}

this is what i have so far

#

if nth number is 1, retun the first term

#
#include <iostream>
using namespace std;

int geometricSequence(int firstTerm, int ratio, int n) {
    // Base Case
    if (n == 1) {
        return firstTerm;
    }
    // Recursive case
    return geometricSequence(firstTerm, ratio, n-1);
}


int main() {
    geometricSequence(1, 2, 3);
    return 0;
}
neat summit
# trail zealot ```c++ #include <iostream> int geometricSequence(int firstTerm, int ratio, int ...

So with a recursive call the problem always needs to become simpler.

But tbh, looking at the geometric formula rn, I'm a bit stumped as to how we are expected to use recursion here.
The problem is that right now the solution is just:

int geometricSequence(int firstTerm, int ratio, int n) {
    return firstTerm * std::exp(ratio, n - 1);
}
```I'm a bit confused, do they want you to essentially implement the exponentiation function?


Ah, quick Wikipedia search clarified things.
You're approaching this with the incorrect formula right now.
trail zealot
#

i will forward what my coach said

#

is that okay?

neat summit
#

So you're approaching this with the function:

a_n = a * r^(n - 1)
```but for recursion you need something in the form:
```matlab
a_n = ... a_n_minus_1 ...
trail zealot
#

ahh

neat summit
trail zealot
#

oops

neat summit
#

screenshot is fine

trail zealot
#

we use this formula?

neat summit
#

yup

#

yup

trail zealot
#

got it

neat summit
#

Now, given the fibonacci formula and my example implementation of it, and also given the proper recursive geometric progression formula, try to create the code

trail zealot
#
#include <iostream>
using namespace std;

int geometricSequence(int firstTerm, int ratio, int n) {
    // Base Case
    if (n == 1) {
        return firstTerm;
    }
    // Recursive case
    return geometricSequence(firstTerm,ratio, n - 1) * ratio;
}


int main() {
    
    return 0;
}
neat summit
heavy pondBOT
#
Why Is `using namespace std` Considered Bad Practice?

using namespace std will import all the symbols from std into the enclosing namespace. This can easily lead to name collisions, as the standard library is filled with common names: get, count, map, array, etc.

A key concern with using namespace std; is not what is imported now but rather what may suddenly be imported in the future.

While using namespace std; is alright for tiny projects, it is important to move away from it as soon as possible. Consider less intrusive alternatives:

// OK: *only* import std::vector
using std::vector;
// OK: namespace alias
namespace chr = std::chrono;
chr::duration x;
trail zealot
neat summit
trail zealot
#

How do u turn off the ai shit on clion

#

Ill look it up

#

Now I just call the function and give it parameters right

#
#include <iostream>
 

int geometricSequence(int firstTerm, int ratio, int n) {
    // Base Case
    if (n == 1) {
        return firstTerm;
    }
    // Recursive case
    return geometricSequence(firstTerm,ratio, n - 1) * ratio;
}


int main() {
    std::cout << geometricSequence(2,5,3);
    return 0;
}
#

!solved