confirmation

Hi everyone :)

I want to confirm my suspicions, when I execute this code, when I print
line (Middle with double) I am getting correct result 2.5

However line below with Middle int? results is 2

is this because temps.size() returns integer?

thanks :)

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
#include "stdafx.h"
#include "std_lib_facilities.h"
#include <iostream>



int main()
{
	vector<double>temps = { 1.2, 3.4, 5.6, 7.8, 9.0 };

	double middle = temps.size();
	double x =  2;
	cout << middle / x << " Middle with double " << endl;

	double middle1 = (temps.size() / 2); 
	cout << middle1 << " Middle int? " << endl;


	for (int i = 0; i < temps.size(); i++)
	{
		cout << "This is vector: " << temps[i] << endl;
	}

	system("pause"); 
    return 0;
}
Last edited on
It is because the type of both number is integer. Change

double middle1 = (temps.size() / 2.0);

and you will get the correct result.
Thank you Coder777 :)
Topic archived. No new replies allowed.