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.
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
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
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
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;
}
elseif (a2 / a1 == a3 / a2)
{
r = a2 / a1
ang = r ^ (an - 1) * a1
cout << "The " << an << "th term is " << ang << "." << endl;
}
#include<iostream>
usingnamespace 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;
}
elseif (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"
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.