Problem with counting

Hi, I have this kind of problem: My input looks like this:

2
w 3 4
d 4 5

And after end of first iteration for i=0 i get s=4.12311
but!! if i change the line:
double s = pow((s1*s1+s2+s2),0.5);
for this:
double r = s1*s1 + s2*s2;
double s = pow((r),0.5);
I get s=5, good answer. Can someone explain me why first method gives wrong answer?


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
103
104
105
  #include<iostream>
#include<cmath>
#include<vector>
#include<string>
#include<sstream>
using namespace std;

void QuickSort(){
	int r;//amount of rows
	/*cout << "Write down the number of columns" << endl;*/
	cin >> r;

	int k = 3;//amount of columns
	//Dynamic memory allocation
	string **T = new string *[r];
	for (int i = 0; i < r; ++i)
		T[i] = new string[k];

	for (int i = 0; i < r; i++)
	{
		string a, b, c;
		cin >> a;
		cin >> b;
		cin >> c;
		T[i][0] = a;
		T[i][1] = b;
		T[i][2] = c;

	}


	vector<double> S;
	for (int i = 0; i < r; i++)
	{
		
			double s1 = 0, s2 = 0;
			string p1 = T[i][1], p2 = T[i][2];

			cout << "p1= " << p1 << endl;
			cout << "p2= " << p2 << endl;

			for (int h = 0; h < p1.length(); h++){
				s1 += (p1[h]-48);
				cout << "s1" << h << "]= " << s1 << endl;

			}
			for (int h = 0; h < p2.length(); h++){
				s2 += (p2[h]-48);
				cout << "s2[" << h << "]= " << s2 << endl;
			}
			cout << "s1*s1=" << s1*s1 << endl;
			cout << "s2*s2=" << s2*s2 << endl;
			double r = s1*s1 + s2*s2;


			double s = pow((s1*s1+s2+s2),0.5);

				cout << s << endl;
				system("pause");
				S.push_back(s);
			
				
	}

	//sort from min to max	
	double pom;

	for (int i = 0; i<S.size(); i++)
	for (int j = 0; j<S.size() - i - 1; j++) 
	if (S[j]>S[j + 1])
	{
		//changing positions
		pom = S[j];
		S[j] = S[j + 1];
		S[j + 1] = pom;
	}

	for (int i = 0; i < r; i++)
	{
		for (int j = 0; j < k; j++)
		{
			cout << T[i][j] << " ";
		}
		cout << endl;
	}


}


void wyw(){
	int t;
	cin >> t;//number of tests
	for (int i = 0; i < t; i++){

		cout << endl;
	}

}

int main(){
	/*wyw();*/
	QuickSort();
	return 0;
}
Topic archived. No new replies allowed.