Finding the Nth term of a sequence

Hello I am new to C++ and do not really know anything. I am trying to make a program that will tell you a user inputted term of a sequence. IE

1
2
3
4
5
6
7
cout << "Enter at leased 3 numbers of the sequence." << endl;
cin >>  ??? >> endl;             
             
cout << "What term do you want to find?" << endl;
cin >> ??? >> endl;
             
cout << "The " << ??  << "th term is " <<  ?? << "." << endl << endl;



The math for an arithmetic sequence is:

an = a1 + (n āˆ’ 1)d

a geometric sequence is:

an = rnāˆ’1a1

where an is the term you want to find, a1 is the first term in the sequence, n is the number in the sequence, and d (in arithmetic) is how much each term in the sequence is raised or lowered, and r is what the first number is multiplied by.
That's nice and all, but what's your question?
How would I code for that?
closed account (D80DSL3A)
My math in this area is a little unsure but it appears to me that not all 3 numbers entered would necessarily fit either sequence.
The 3rd number would have to be "right".

The 3rd # determines if we have:
1) arithmetic sequence
2) geometric sequence
3) neither

Clearly, the 1st # entered is a1.
Once a 2nd # is entered we can calculate values for what the 3rd # would have to be to fit one or the other sequence.

If 2nd # = a2 then:
d = a2 - a1 so int a3a = a1 + 2*(a2 - a1) = 2*a2 - a1 if arithmetic sequence
or
r = a2/a1 so int a3g = a1*(a2/a1)^2 = a2*a2/a1 if geometric

User enters a3 now:
if( a3 == a3a )
cout << "arithmetic series ";
else if( a3 == a3g )
cout << "geometric series ";
else
cout << "that fits neither series. Bye-bye! ";

Now, if a3 was a fit then we can prompt for n and calculate an.

Am I seeing this wrong?

EDIT: I wondered if it could be indeterminate for any values of a1, a2.
Yes. If a2 == a1 then both of the above formulas for the expected value of a3 give:
a3a = a3g = a1 (see above formulas)
so a3 = a1 is required.

If user enters a3 and it is = a1, then either sequence may apply.
That's OK though because with a1 = a2 we get d = 0 and r = 1 so in either case it is the constant sequence: a1, a1, a1, a1,...
ie an = a1

So add a 4th case up at the top of this post
4) either
Last edited on
Yes this is pretty much exactly what I'm asking. But honestly I don't even know where to start. If I could get a little jump start on the code that would be nice. :D
closed account (D80DSL3A)
Sure.
Write a program which:

1) prompts the user for 3 integer values and reads these values into some variables.
2) Write these values back to the screen.

That would be a good start.

EDIT:
Actually, I expected you to get a lot more than just "DUH" out of my 1st post.
I feel like a stooge for wasting my time writing it now.

fun2code - out!
Last edited on
No it helped a lot thank you. I am going to try and work it my self i will have more specific questions thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
             cout << "Enter at leased 3 numbers of the sequence." << endl;
             cin >> a1 >> a2 >> a3;
             
             cout << "Enter the term do you want to find?" << endl;
             cin >> an;
             
             if (a2 - a1 == a3 - a2)
             {
                    d = a2 - a1
                    
                    ana = a1 + (n - 1) * d
             
                    cout << "The "  << an << "th term is " << ana << "." << endl;
                    }
             else if (a2 / a1 == a3 / a2)
             {
                  r = a2 / a1
                  ang = r ^ (an - 1) * a1
                  
                  cout << "The " << an << "th term is " << ang << "." << endl;
                  }


it says ana = a1 + (n-1) * d like is incorrect?
it says ana = a1 + (n-1) * d like is incorrect?


I don't understand your question, and you haven't posted enough of your code for us to see what might be wrong.

Are you getting a compiler error? A run-time error? Incorrect results?
Sorry. Here is it all
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
using namespace std;

int main(void)
{
    double a1, a2, a3, d, r, ana, ang;
    int an;

             cout << "Enter at leased 3 numbers of the sequence." << endl;
             cin >> a1 >> a2 >> a3;
             
             cout << "Enter the term do you want to find?" << endl;
             cin >> an;
             
             if (a2 - a1 == a3 - a2)
             {
                    d = a2 - a1
                    
                    ana = a1 + (an - 1) * d
             
                    cout << "The "  << an << "th term is " << ana << "." << endl;
                    }
             else if (a2 / a1 == a3 / a2)
             {
                  r = a2 / a1
                  ang = r ^ (an - 1) * a1
                  
                  cout << "The " << an << "th term is " << ang << "." << endl;
                  }
}

this is my error.
C:\First Progam.cpp In function `int main()':
74 C:\First Progam.cpp expected `;' before "ana"
81 C:\First Progam.cpp expected `;' before "ang"
Last edited on
You need to add a semi-colon at the end of the lines numbered:

17
19
25
26
Thank you.

How do I use a power function? Do I need to <include> something else? like in line 26?

Thank you for your help.
How do I use a power function? Do I need to <include> something else? like in line 26?


See pow function in math.h


pow(raiseThisNumber, toThisExponent);

ang = pow(r, an - 1) * a1;
closed account (D80DSL3A)
Nice job on the program so far.
I like how you expressed the tests directly for which series type it is (lines 15 and 23) vs. my method of calculating values for ana and ang to compare with a3.
In your method there isn't even a need for the variables ana or ang since the values could be calculated in the output line:
cout << a1*pow(r,an-1);// expression will be evaluated and the result output.

You may run into trouble with pow(int,int). If so just cast one argument to float.

Sorry for going off on you a bit back there. You caught the brunt of a continuing type of frustration which I encounter too often here.
I am glad to see you were fully capable of solving the problem yourself.
Thanks that shortened the code nicely :D Yes I am capible of figuring it out But it took me a long time to figure out where to start I just wanted a start point.

Thanks everyone for you help
Topic archived. No new replies allowed.