Trouble with displaying vector values.

Trying to display the greatest and smallest difference by taking the previous entry and comparing it to the present. The smallest value is always zero. The answers also keep piling on for some reason.

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
  // Display the greatest and smallest distance between cities.
 // Display the sum of all

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

int b = 0;
double input = 0;

vector<double>distance;

cout << "Enter distances between cities" << endl;

while (cin>>input){
	distance.push_back(input);
	double greatest_distance = 0;
	double smallest_distance = 0;
	double sum_distance;
	double mean_distance;
	for (int a = 0; a < distance.size(); ++a){
	sum_distance += distance[a]; 
		cout << "Sum of distances is " << sum_distance << endl;
                cout << "Mean is " << sum_distance/distance.size() << endl;
		if (a >=2){
                if (distance[a-1] > distance[a]){
			if (distance[a-1]-distance[a] > greatest_distance)
				greatest_distance = distance[a-1]-distance[a];
				cout << "Greatest distance between two cities is " << greatest_distance << endl;
			}
		else if (distance[a-1] < distance[a]){
			if (distance[a]-distance[a-1] > greatest_distance)
				greatest_distance = distance[a]-distance[a-1];
				cout << "Greatest distance between two cities is " << greatest_distance << endl;
			}
		
		if (distance[a-1] > distance[a]){
			if (distance[a-1]-distance[a] < smallest_distance)
				smallest_distance = distance[a-1]-distance[a];
				cout << "Smallest distance between two cities is " << smallest_distance << endl;
			}
		else if (distance[a-1] < distance[a]){
			if (distance[a]-distance[a-1] < smallest_distance)
				smallest_distance = distance[a]-distance[a-1];
				cout << "Smallest distance between two cities is " << smallest_distance << endl;
			         }
		           }
	            }
               }
          }
	
				
				
Last edited on
an example:
sum_distance += distance[a];

you are adding sum_distance to distance[a], then putting the value back into sum_distance, which is fine. however, you don't initialise sum_distance when you declare it on line 25. So when you first come to do this addition the initial value of sum_distance is most likely full of rubbish.

in fact, when i try and step over line 28 in my debugger (vs2010), i get this:

Run-Time Check Failure #3 - The variable 'sum_distance' is being used without being initialized.
I didn't realize that I would get a garbage value if I didn't initialize but there's still the issue of the stacking results and and the smallest distance always being 0. It's a little weird but if you test the code you'll see that after a while of inputting values things begin to really pile up and loop over loops.
i'm not sure how this is even working for you, cuz it's not for me.

This for example:
if (distance[a-1] > distance[a]){

The first pass through your loop, a will be zero. So what do you think happens when you try and element '-1'?
It's still able to be compiled but I added a tester to make sure that atleast two values are stored in the vector before executing the distance portion of the code if (a >=2) {body} Right above line 31.
Last edited on
so you want help with code you haven't posted?
Edited the topic to include.
Topic archived. No new replies allowed.