problem with vector.size()

We're just starting to use vectors, and have to write a simple program to enter data into a vector, and then use a for loop with the .size() member function to scroll through the vector and find the sum, ave, highest, lowest, etc.
My problem is that after I use push_back to add 5 elements to the vector, and then loop, x.size() is returning 10 instead of 5. Here is the code
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

const int NUM_DAYS = 5;   // Number of days
	vector<double> sales(NUM_DAYS);
   //double sales[NUM_DAYS],   // Holds the daily sales amounts
         double total;             // Holds the week's total sales
         double average;           // Holds the average daily sales
         double highest;          // Holds the highest daily sales
         double lowest;           // Holds the lowest daily sales 
         double range;			// highest - lowest
	 double amount;	
	 double dailyAmount;
	
	
   // Get the sales data
	cout << "Enter the sales for this week.\n";
	for (int day = 1; day <= NUM_DAYS; day++)
	{	cout << "Day " << day <<": ";
		cin  >> dailyAmount;
		sales.push_back(dailyAmount);
	}

	// Get total sales 
	total = sumVector(sales);
	// get average
	average = getAverage(sales);



double sumVector(vector<double> &x)
{
	double total = 0.0;  // Accumulator

	for (int count = 0; count < x.size(); count++)
		total += x[count];//this loop executes 10 times, should be 5
	return total;
}
double getAverage(vector<double>& x)
{
	double result;
	double total;
	total = sumArray(x);
	result = total/double(x.size());//this divides by 10 instead of 5
	return result;
}


When I step through, I can see that x.size() is returning 10, and the for loops repeat 10 times, which totally messes up the average, among other things. I was under the impression that x.size() should return 5, or the number of items that have been put into the vector. Am I incorrect in that assumption, or have I made a simple mistake somewhere?


This is cut and pasted from the autos window in VS2005 when I enter the
values 10,20,30,40,50. Where are the 0.00's coming from?


- x [10](0.00000000000000000,0.00000000000000000,0.00000000000000000,
0.00000000000000000,0.00000000000000000,10.000000000000000,
20.000000000000000,30.000000000000000,40.000000000000000,50.000000000000000) std::vector<double,std::allocator<double> > &
The point is that that when you've created a vector with
vector<double> sales(NUM_DAYS);
instruction, you have got a vector with NUM_DAYS elements, each of which is initialized with zero-value (due to default constructor). Then you add another 5 elements into the vector in your cycle.

Maybe, you've ment the following initialization of your vector:
vector<double> sales;

Then you may reserve memory for several elements in advance for faster insertion:
sales.reserve(NUM_DAYS);
- this won't change the vector's size.
I was under the impression that using
vector<double> sales(NUM_DAYS);
allocated(reserved) space for the vector, without initializing any of the values.
as soon as I removed that, everything started working.
Thanks
>I was under the impression that using
>vector<double> sales(NUM_DAYS);
>allocated(reserved) space for the vector, without initializing any of the >values

Not without initializing, but WITH initializing - with zeroes (with default constructor strictly saing). This is standard behaviour for the STL containers.
Topic archived. No new replies allowed.