Quick question about storing values.

I'm writing this drill program to store differences between cities.
For some reason the greatest distance or greatest "difference" is always
equal to my input. I'm sure it's logical but I've reviewed the code many times.

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
65
66
67
68
69
70
71
72
73
74
75
76
 // Store distances in a vector. 
//Display mean and greatest distance between two cities(adjacent values in a vector) etc.

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
inline void keep_window_open(){char ch; cin>>ch;}
int main() {  

double input = 0;
double input1 = 0;
double input2 = 0;
double sum_of_distances = 0;
double greatest_distance = 0;
double smallest_distance = 0;
double distance_mean = 0;
double largest = 0;
double smallest = 0;
double amount = 0;
		

vector<double>distances;

cin >> input1;
distances.push_back(input1);
sum_of_distances += input1;
cin >> input2;
distances.push_back(input2);
sum_of_distances += input2;
amount = 2;
if (input1>input2){
	largest = input1;
	smallest = input2;
}
else if (input1<input2){
	largest = input2;
	smallest = input1;
}
double mean = sum_of_distances/distances.size();
cout << "Sum is " << sum_of_distances << endl;
cout << "Mean is " << mean << endl;
cout << "Amount of distances is " << amount << endl;
greatest_distance = largest-smallest;
smallest_distance = largest-smallest;
cout << "Greatest distance is " << greatest_distance << endl;
cout << "smallest distance is " << smallest_distance << endl;
while (cin >> input){
		distances.push_back(input);
		sum_of_distances += input;
		double mean = sum_of_distances/distances.size();
		++amount;
		cout << "Sum is " << sum_of_distances << endl;
		cout << "Mean is " << mean << endl;
		cout << "Amount of distances is " << amount << endl;
		if (distances[amount-1]>distances[amount]){
			largest = distances[amount-1];
			smallest = distances[amount];
		}
		else if (distances[amount-1]<distances[amount]){
			largest = distances[amount];
			smallest = distances[amount-1];
		}
		if (amount >= 2){
			if (largest-smallest>greatest_distance)
				greatest_distance = largest-smallest;
				cout << "Greatest distance is " << greatest_distance << endl;
			if (largest-smallest<smallest_distance && greatest_distance)
				smallest_distance = largest-smallest;
				cout << "Smallest distance is " << smallest_distance << endl;
		}
	}
}
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>

using namespace std;

inline void keep_window_open(){char ch; cin>>ch;}

int main() 
{  

double input = 0;
double input1 = 0;
double input2 = 0;
double sum_of_distances = 0;
double greatest_distance = 0;
double smallest_distance = 0;
double distance_mean = 0;
double largest = 0;
double smallest = 0;
double amount = 0;
		

vector<double>distances;

cin >> input1;
distances.push_back(input1);
sum_of_distances += input1;

cin >> input2;
distances.push_back(input2);
sum_of_distances += input2;

amount = 2;

if (input1>input2){
	largest = input1;
	smallest = input2;
}

else if (input1<input2){
	largest = input2;
	smallest = input1;
}
double mean = sum_of_distances / distances.size();

cout << "Sum is " << sum_of_distances << endl;
cout << "Mean is " << mean << endl;
cout << "Amount of distances is " << amount << endl;

greatest_distance = largest-smallest;
smallest_distance = largest-smallest;

cout << "Greatest distance is " << greatest_distance << endl;
cout << "smallest distance is " << smallest_distance << endl;

while (cin >> input)
{
		distances.push_back(input);
		sum_of_distances += input;
		mean = sum_of_distances/distances.size();
		++amount;

		cout << "Sum is " << sum_of_distances << endl;
		cout << "Mean is " << mean << endl;
		cout << "Amount of distances is " << amount << endl;

		if (distances[amount-1] > largest)
		{
			largest = distances[amount-1];
			//smallest = distances[amount];
		}

		else if (distances[amount-1] < smallest)
		{
			//largest = distances[amount];
			smallest = distances[amount-1];
		}
		
		// greatest distance will always be between largest and smallest
		greatest_distance = largest-smallest; 
		
		
		/** we have to check the distance of our latest input with each already-typed
		    input **/
		for(int i = 0; i < distances.size() - 1; i++) 
		{
			if (abs(distances[amount-1]-distances[i]) < smallest_distance) 
			{	
				smallest_distance = abs(distances[amount-1]-distances[i]);
				break;  // we have found the smallest distance 
			}
		}
		
		cout << "Greatest distance is " << greatest_distance << endl;					
		cout << "Smallest distance is " << smallest_distance << endl;
		

	}
}
Last edited on
The problem is this portion.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
		amount++;
		cout << "Sum is " << sum_of_distances << endl;
		cout << "Mean is " << mean << endl;
		cout << "Amount of distances is " << amount << endl;
		if (distances[amount-1]>distances[amount-2]){
			largest = distances[amount-1];
			smallest = distances[amount-2];
		
		}
		else if (distances[amount-1]<distances[amount-2]){
			largest = distances[amount-2];
			smallest = distances[amount-1];
			cout<<"else if"<<endl;
		

In your code you were comparing distance[amount-1] to distance[amount]. Amount has a value of 3 but is pointing to element 4 which doesn't exist in your vector. Change it to the above code.

1
2
3
4
5
6
7
8
9
10
		if (amount >= 2){
			if (largest-smallest>greatest_distance)
				greatest_distance = largest-smallest;
				cout << "Greatest distance is " << greatest_distance << endl;
			if (largest-smallest<smallest_distance&& greatest_distance)
				smallest_distance = largest-smallest;
				cout << "Smallest distance is " << smallest_distance << endl;
		}
	}
}


Here, the statement if(amount>=2) is pointless because you require two inputs before the while loop is even executed, thus this statement will always be true. Also the "&&" portion of your last if statement is pointless as well because the smallest_distance should always be less or equal to greatest_distance.

EDIT: rich's code looks nicer so use his lol
Last edited on
Thanks for the help. This new code is kind of difficult to understand but I'll get to it eventually. Thanks again.
Topic archived. No new replies allowed.