For loop problem

Hey everyone,

I'm working on an exercise that is intended to display items in a vector in quartiles based on the values of the items. The first quartile should display the largest numbers in the first quarter of the vector and so on. I found a way to do this using while loops, but I thought I could make the code cleaner and more efficient using a for loop.

Area of interest is specifically line 23 - 25. I checked the values of i and the value of size and they are correct but for some reason the math just isn't happening because when I check the value of pct and ipct, I get all zero's. (The end of line 26 is my last error check before posting)

Can anybody explain what I am doing wrong and why the values are zero?

Thanks cpp community!

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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
	double x;
	vector<double> quarts;
	vector<double>::size_type size;
	cout << "Enter numbers: ";
	while(cin >> x)
	quarts.push_back(x);
	size = quarts.size();
	cout << endl;
	if (size == 0){
		cout << "You didn't enter any numbers!\n"
				"Better luck next time";
		return 1;
	}
	sort(quarts.begin(), quarts.end());
        cout << "First Quarter\n";
	for(int i = (size - 1); i >= 0; --i){
		double pct = i / size;
		int ipct = pct * 100; //converting to int since switch wont accept doubles
		cout << quarts[i] << "\t" << i << endl;
		switch(ipct){
			case 75:
				cout << "Second Quarter\n";
				break;
			case 50:
				cout << "Third Quarter\n";
				break;
			case 25:
				cout << "Fourth Quarter\n";
				break;
		}
	}
	/*double qt = size / 4;
	double fq = (qt * 4) - 1;
	cout << "***First Quarter***\n";
	while(fq != (qt * 3) - 1){
		cout << quarts[fq] << endl;
		--fq;
	}
	fq = (qt * 3) - 1;
	cout << "***Second Quarter***\n";
	while(fq != (qt * 2) - 1){
		cout << quarts[fq] << endl;
		--fq;
	}
	fq = (qt * 2) - 1;
	cout << "***Third Quarter***\n";
	while(fq != (qt * 1) - 1){
		cout << quarts[fq] << endl;
		--fq;
	}
	fq = (qt * 1) - 1;
	qt = 0;
	cout << "***Fourth Quarter***\n";
		while(fq >= qt){
			cout << quarts[fq] << endl;
			--fq;
		} */
		return 0;
}
Last edited on
hello again.
It looks like there is a situation similar to the one in your previous thread. On line 24, you have 3 variables, each with a different type. The problem is with the right side of the assignment; i and size are ints, and integer division is kinda funny sometimes.

i will always be less than size, so the expression i / size is 0.
(do you see why?) Therefore, pct = 0, and ipct = pct * 100 = 0 as well.

The first solution that comes to mind is casting i and size to doubles before the division, so that the result is not 0.
Thanks again atropos.

I see the mistake now. I guess what i'm really learning today more than anything is data types really have to match up in all situations.

Thanks a lot for helping out this newbie yet again!
This fixed everything!

Thanks again atropos! :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cout << "First Quarter\n";
	for(int i = (size - 1); i >= 0; --i){
		double dsize = size;
		double di = i;
		double pct = di / dsize;
		int ipct = pct * 100;
		cout << quarts[i] << "\t" << ipct << endl;
		switch(ipct){
			case 75:
				cout << "Second Quarter\n";
				break;
			case 50:
				cout << "Third Quarter\n";
				break;
			case 25:
				cout << "Fourth Quarter\n";
				break;
		}
	}
Topic archived. No new replies allowed.