Project one - Sequencing numbers

Hi im a student currently attending a c++ programming class at Fairmont State University. My class and i are instructed to create a program that uses 5 numbers to input and to determine what sequence it is. In the second part we are to loop this program. I cant figure out how to do this. Right now i am also trying to learn from tutorials and talking to my fellow classmates on how to start the project. I have gotten no where and i have until the 10th to complete the first part. I teacher gave us a program to do before on how to determine the number of slices of pizza in the size of the diameter it is in. He told us to copy the code by typing it. I am aware that there is alot of codes, formulas and statements. but like i said i have no clue. so i have to look for outside help on understanding this. i wish he would give assignments in class and allow us to learn in class instead of trying to read the notes and scratching my brain at the same time. here is the instructions.

COMP 1102 Project One -- Part One
Assigned: September 29, 2014
Due: October 10*, 2014
* Part Two will be due on October 15
* There may be one or two questions on Test 2 that pertain to this project.
Classifying Numeric Sequences:
Write a program that will:
1) Prompt the user to enter five (5) consecutive terms of a numeric sequence.
Note that these terms are not necessarily integers.
2) Determine and report the type of sequence given (if any).
Your program should check for all of the following types of sequences:
a) Arithmetic sequence : Each term, except the first, is obtained by adding
d, the common difference, to the previous term. That is,
Example: -9, -2, 5, 12, 19, …
Note: the nth term of an arithmetic sequence is also given non-recursively by:

b) Geometric sequence : Each term, except the first, is obtained by multiplying
the previous term by r, the common ratio. That is,
Example: ¼, 1, 4, 16, 64, …
Note: the nth term of an geometric sequence is also given non-recursively by:



c) A sequence of Triangular numbers : 1, 3, 6, 10, 15, …
There are a number of ways to consider these numbers. One is as the number
of dots required to create a triangle of a given number of rows:
1 3 6 10 15
Note that the sequence need not start with 1.
Note: If a sequence of triangular numbers does begin with 1, the nth term
of the sequence will be given by: .
Another way to define the triangular numbers is: A number x is triangular if
and only if 8x + 1 is a square. Try it out with the numbers above!
A third characteristic is that the difference between consecutive triangular
numbers increases by 1 with each additional term.
(3 - 1 = 2; 6 - 3 = 3; 10 - 6 = 4; etc.)
d) A sequence of Square numbers : 1, 4, 9, 16, 25, …
e) Fibonacci numbers (or “Fibonacci-like” numbers) : 1, 1, 2, 3, 5, 8, 13, …
You are to consider a sequence to be “Fibonacci-like” if the following is satisfied:
Each term, after the first two, is equal to the sum of the previous two terms.
That is,
So, another example of a “Fibonacci-like” sequence is: 3, 8, 11, 19, 30, …f) The sequence: 1, 3, 7, 15, 31, …
You should be able to tell me a few things about this sequence! Give it an
appropriate name.
All output should be properly labeled and be in a neat form.
Some other sample results :
1) With input: 36 49 64 81 100
the output should be something like: This is a sequence of Square numbers.
2) With input: -2 4 2 6 8
the ouput should be something like: This is a sequence of Fibonacci-like numbers.
3) With input: 0.25 -0.5 1 -2 4
the output should be something like: This is a geometric sequence with r = -2
4) With input: 45 55 66 78 91
the output should be something like: This is a sequential of Triangular numbers.
Your source code file should contain appropriate documentation and you should use
meaningful identifiers. You should also observe the format conventions mentioned in
class (indention, etc.).
Your source code file should be named sequences.cpp.
Turn in your project by submitting it as an attachment to a Blackboard message. Do
NOT send your executable file.
As with all projects, to receive a grade you must discuss your project with me in my
office.
Do it one step at a time. The first one is to read 5 numbers.
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.
Last edited on
This is actually become clearer to me now. I will use what i just learned so far to incorporate into my programming. These forums should help me out now thanks. I will check back with my new code and see if you guys like what i made.
So this is what i have so far. I cant seem to get my if else statements to work correctly. I have tried different ways but come up with errors. Once i find that out then i will attempt to finish the last 3 sequence problems.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	int a,b,c,d,e;			//number input reference 
	
	cout<<"Please enter five numbers of a consecutive sequence."<<endl;			//begin code sequence
	
	cout<<"1st number=";			
	cin>>a;					//1st number
	
	cout<<"2nd number=";
	cin>>b;					//2nd number
	
	cout<<"3rd number=";
	cin>>c;					//3rd number
	
	cout<<"4th number=";
	cin>>d;					//4th number
	
	cout<<"5th number=";
	cin>>e;					//5th number
	
			//sequences
			if	(e-d == d-c == c-b == b-a)				//Arithmetic
			{
				cout<<"This is a sequence of Arithmetic numbers.";
			
			}else if	(e/d == d/c == c/b == b/a)			//Geometric
			{
					cout<<"This is a sequence of Geometric numbers.";
			
			}else if	(1)		//Triangular
			{		
					cout<<"This is a sequence of Triangular numbers.";
			
			}else if	(1)		//Square
			{
				cout<<"This is a sequence of Square numbers.";
			
			}else if	(1)		//Fibonacci
			{
				cout<<"This is a sequence of Fibonacci numbers.";
			}	
				return 0;
}
Last edited on
I only started working with C++ recently and unless there was some formatting issues with the copy and paste, you should take a closer look at your code. By trying to compile this you will notice an issue with missing bool statements within the parenthesis, misplaced brackets, and missing else if statements.

Something that I use to test my statements is to either comment out (either by using // for single lines or /* text */ for a body of text) what you are not currently working on or testing. This way you can narrow down the possible position for your error.

I added the missing brackets and commented out after the geometric sequence (also added a } after the else which you will need to remove if you are to continue with a chain of else if).

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
//sequences
	if (e - d == d - c == c - b == b - a)				//Arithmetic
	{
		cout << "This is a sequence of Arithmetic numbers.";

	}
	else if (e / d == d / c == c / b == b / a)			//Geometric
	{
		cout << "This is a sequence of Geometric numbers.";
	}
	else;/*if ()		//Triangular
		 {
		 cout<<"This is a sequence of Triangular numbers.";
		 }
		 else if ()		//Square
		 {
		 cout<<"This is a sequence of Square numbers.";
		 }
		 else if ()		//Fibonacci
		 {
		 cout<<"This is a sequence of Fibonacci numbers.";
		 }
		 else
		 {
		 cout<<"There is not a consecutive sequence, please ty again.";
		 }*/
	;
	return 0;
}


Again, I'm not too experienced so hopefully this helps and does not confuse you any further.
Last edited on
Nice progress so far. :P What are the exact errors?
So far so good no errors but i need to figure out the equations for square root and fibonacci. Please test vigorously to ensure that the first 3 are working fine.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <iomanip>

using namespace std;

int main()

{
	float a,b,c,d,e;			//number input reference 
	
	cout<<"Please enter five numbers of a consecutive sequence."<<endl;			//begin code sequence
	
	cout<<"1st number=";			
	cin>>a;					//1st number
	
	cout<<"2nd number=";
	cin>>b;					//2nd number
	
	cout<<"3rd number=";
	cin>>c;					//3rd number
	
	cout<<"4th number=";
	cin>>d;					//4th number
	
	cout<<"5th number=";
	cin>>e;					//5th number
	
			//sequences
			if	(e-d == d-c == c-b == b-a)				//Arithmetic
			{
				cout<<"This is a sequence of Arithmetic numbers.";
			
			}else if	(e/d == d/c == c/b == b/a)			//Geometric
			{
					cout<<"This is a sequence of Geometric numbers.";
			
			}else if	(e-d == 5 && d-c == 4 && c-b == 3 && b-a == 2)		//Triangular
			{		
					cout<<"This is a sequence of Triangular numbers.";
			
			}else if	(a,b,c,d,e)		//Square
			{
				cout<<"This is a sequence of Square numbers.";
			
			}else if	(a+b == b+c == c+d == d+e)		//Fibonacci
			{
				cout<<"This is a sequence of Fibonacci numbers.";
			}	
			return 0;
}
Last edited on
Your first two appear to be fine. The algorithm for triangular numbers is incorrect, and it's not "general" enough to be trustworthy.

a, b, c, d, e = 1, 3, 6, 10, 15.
a + 1 = 1 + 1 = 3
b + 2 = 3 + 2 = 5
etc.

Let's say even if this was correct:
a + 2 = 1 + 2 = 3
b + 3 = 3 + 3 = 6
etc.

What if your user input larger triangular number sequence: 15, 21, 28, 36, 45. Your code has assumed the user will start from the first triangular number, and when you code, you should never assume the user is going to do what you think they should do. :P

In regards to the square root, #include <cmath> and then use the sqrt function.
http://www.cplusplus.com/reference/cmath/sqrt/
Last edited on
Note:
1
2
3
4
5
6
7
if ( e-d == d-c == c-b )

// is same as

bool X = (e-d == d-c);
float H = c-b;
if ( X == H )

There is not one, but two fundamental problems:
1. Comparison of a bool to a number
2. Equality comparison of floating point numbers. A float is hardly ever == 0, but its absolute value could be less than epsilon.

On geometric: division by 0 may require consideration.
I talked to my teacher and he was impressed but the fibonaci sequence is not working correctly. I cannot loop my program either. Please help.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cstdlib>

using namespace std;

int main(){
char response;	
do
{
	int a,b,c,d,e,r;			//number input reference 
	
	cout<<"Please enter five numbers of a consecutive sequence."<<endl;			//begin code sequence
	
	cout<<"1st number=";			
	cin>>a;					//1st number
	
	cout<<"2nd number=";
	cin>>b;					//2nd number
	
	cout<<"3rd number=";
	cin>>c;					//3rd number
	
	cout<<"4th number=";
	cin>>d;					//4th number
	
	cout<<"5th number=";
	cin>>e;					//5th number
	
			//sequences
			if	(e-d == d-c && d-c == c-b && c-b == b-a)				//Arithmetic
			{
				cout<<"This is a sequence of Arithmetic numbers."<<endl;
			
			}else if	(r == e/d && r == d/c && r == c/b && r == b/a)			//Geometric
			{
					cout<<"This is a sequence of Geometric numbers."<<endl;
			
			}else if	((pow((int)sqrt((8*e+1)),2)) == (8*e+1) && (pow((int)sqrt((8*d+1)),2)) == (8*d+1) && (pow((int)sqrt((8*c+1)),2)) == (8*c+1)&& (pow((int)sqrt((8*b+1)),2)) == (8*b+1) && (pow((int)sqrt((8*a+1)),2)) == (8*a+1))		//Triangular
			{		
					cout<<"This is a sequence of Triangular numbers."<<endl;
			
			}else if	((pow((int)sqrt(e),2)) == e && (pow((int)sqrt(d),2)) == d && (pow((int)sqrt(c),2)) == c && (pow((int)sqrt(b),2)) == b && (pow((int)sqrt(a),2)) == a)		//Square
			{
				cout<<"This is a sequence of Square numbers."<<endl;
			
			}else if	((a + b) == c && (b + c) == d && (c + d) == e)		//Fibonacci
			{
				cout<<"This is a sequence of Fibonacci numbers."<<endl;
		
			}else
			{
				cout<<"There is no sequence!"<<endl;}
			{			
				cout<<"Would you like to try again. Please enter y or Y to continue."<<endl;
				response = getchar();
    			getchar();
    		}
    		while(response != 'Y' || response != 'y');
    		{
    			cout<<"Have a nice day!";
			}return 0;
			}
Topic archived. No new replies allowed.