Homework Help

Given any four positive integers a, b, c, d, you can find four more integers as:

| a - b |, | b - c |, | c - d |, | d - a |
Find the absolute value of the difference of pairs to create a new group of a, b, c, d. If you repeat this, the numbers will eventually converge to one common value.
For example, given 1, 3, 5, 9:

1 3 5 9
2 2 4 8 (1 step)
0 2 4 6 (2 steps)
2 2 2 6 (3 steps)
0 0 4 4 (4 steps)
0 4 0 4 (5 steps)
4 4 4 4 (6 steps)
This set converges to the value 4 in 6 steps.
Write a program that allows the user to enter a group of four numbers, then compute and display the converged value and number of steps.

Enter four numbers: 1 3 5 9

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
 #include <iostream>
#include <iomanip>
using namespace std;

int main(){
	
	int a, b, c, d, x = 0;
	bool flag = true;
	flag == (a == b && a == c && a == d);
	
	cout << "Enter four numbers: ";
	cin >> a >> b >> c >> d;
	
	cout << endl;
	
	while(flag ){
		a -= b;
		b -= c;
		c -= d;
		d -= a;
		x++;
		
		if(a < 0)
			a *= -1;
		
		
		if(b < 0)
			b *= -1;
		
		
		if(c < 0)
			c *= -1;
			
		if(d < 0)
			d *= -1;
		
	}
	
	cout << "This group converges to value " << a << " after " << x << " steps.";
} 


i also tried this

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
#include <iostream>
#include <iomanip>
using namespace std;

int main(){
	
	int a, b, c, d, x = 0;

	cout << "Enter four numbers: ";
	cin >> a >> b >> c >> d;
	
	cout << endl;
	
	while(a != b && a != c && a != d ){
		a -= b;
		b -= c;
		c -= d;
		d -= a;
		x++;
		
		if(a < 0)
			a *= -1;
		
		
		if(b < 0)
			b *= -1;
		
		
		if(c < 0)
			c *= -1;
			
		if(d < 0)
			d *= -1;
		
	}
	
	cout << "This group converges to value " << a << " after " << x << " steps.";
}
Last edited on
You did not include any question.


You do compute the falue of flag on line 9. That is the only point, where the value of flag might change.
(It is true before the assignment. Values of a, b, c, and d are unknown, so value of flag is unknown after line 9.)

You can thus see only two behaviours:
1. the loop is skipped, because flag is false
OR
2. loop continues forever, because flag is true

In other words, you have to update the flag inside the loop.


1
2
a -= b;
d -= a;

Lets start with the a=1, b=3, d=9:
1
2
a = 1 - 3; // a == -2
d = 9 - (-2); // d == 11 

Summary: You cannot update a before you have computed |d - a|
Thank you that makes sense. My question which i forgot to mention is why my while loop is only running through once. I fixed the part where i was modifying d with the already modified a, but it still only runs once so i get the out put " value 2 after 1 time.
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
#include <iostream>
#include <iomanip>
using namespace std;

int main(){
	
	int a, aa, b, c, d, x = 0;

	cout << "Enter four numbers: ";
	cin >> a >> b >> c >> d;
	
	cout << endl;
	
	while(a != b && a != c && a != d ){
		aa = a;
		a -= b;
		b -= c;
		c -= d;
		d -= aa;
		x++;
		
		if(a < 0)
			a *= -1;
		
		
		if(b < 0)
			b *= -1;
		
		
		if(c < 0)
			c *= -1;
			
		if(d < 0)
			d *= -1;
		
	}
	
	cout << "This group converges to value " << a << " after " << x << " steps.";
}

Last edited on
I suppose that you do use the 1 3 5 9 as test input.
1 3 5 9
2 2 4 8 (1 step)

First time 1 != 3 and 1 != 5 and 1 != 9
Second time 2 != 4 and 2 != 8, but 2 == 2

On boolean AND expression:
x && y && z
The result is false if any of the three is false.

You need it remain true as long as any of the three is true.

You want the loop to end when all four values are equal.
When a == b && a == c && a == d
The while needs the opposite:
!(a == b && a == c && a == d)

Complex? Yes, there is another way to say it. The boolean OR:

x || y is true if at least one of the operands is true. Any of {x,y} is true.
Last edited on
Thank you.
Topic archived. No new replies allowed.