Really interesting project. I'm assuming you can do the trivial task such as prompting the user for five numbers, so I'll just discuss the mathematics behind determining a sequences (which I think is what you're really asking for). The biggest thing to note is that all of these types of sequences are just that... sequences, so you need code that can identify the pattern. Arrays and loops are your friend for finding patterns. I might get a bit mathy here, but I can explain the patterns.
(1) Arithmetic sequence. You provided: -9, -2, 5, 12, 19. Now take note:
19 - 12 = 7
12 - 5 = 7
5 - (-2) = 7
-2 - (-9) = 7
So what you need to find if your code is if
numbers[i] - numbers[i-1] always equal the same result.
(2) Geometric. You provided: ¼, 1, 4, 16, 64. The pattern is:
64 / 16 = 4
16 / 4 = 4
4/ 1 = 4
1 / (1/4) = 4
A sequence is geometric if numbers[i]/numbers[i-1] equal the same result.
(3) Triangular sequence. You provided: Triangular numbers : 1, 3, 6, 10, 15.
15 - 10 = 5
10 - 6 = 4
6 - 3 = 3
3 - 1 = 2
The difference between two adjacent successive numbers changes by one each time. So check if number[i] - number[i-1] = numbers[i-1] - number[i - 2] + 1
(4) Square numbers. You provided: 1, 4, 9, 16, 25.
sqrt(1) = 1
sqrt(4) = 2
sqrt(9) = 3
sqrt(16) = 4
sqrt(25) = 5
Just check to see if the sqrt function gives you an integer value and if the results of the sqrt function increase linearly.
(5) Fibonacci numbers. You provided: 1, 1, 2, 3, 5, 8, 13.
1 + 1 = 2
1 + 2 = 3
2 + 3 = 5
3 + 5 = 8
5 + 8 = 13.
The formula for this is number[i] = number[i-1] + number[i-2]
Just check for those patterned conditions. Loop through an array of numbers provided by the user and test each iteration against the result of the first one. So for example (using arithmetic sequence):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
//checking for arithmetic pattern
int numbers[] = {1, 2, 3, 4, 6};
int pattern = numbers[1] - number[0]; //result is 1
bool flag = true;
for (int i = 1; i < 3; i++) {
int test = numbers[i +1] - numbers[i];
if ( test != pattern ) { // will be true at numbers[4] - numbers[3]
flag = false;
break;
}
}
if ( flag )
cout << "Arithmetic sequence!" << endl;
else
cout << "Not an arithmetic sequence!" << endl;
|
So that's the mathematics behind it all. Now you've got to code it.