Vector Indexing

Hi I am writting this code for my proffesor and this is my first time working with 2 dimmensional vectors.

Here is my 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
int main()
{
	vector<float>times;
	vector<int>bins;
	float y =0;
	float time;
	int aCount =0;
	float binSize = 0.39;
	float mean;
	float x = binSize;
	int numberOfBins;
	float binMean =0;
	vector<float>values;
	vector<string>fileNames;
	vector<int>pos;
	pos.push_back(0);

	cout<<"********************************TamStar 1.0********************************\n"
		<< "Welcome to TamStar please create a file called TamStar in your C:"
		<< "drive and name our txt file TamStar before begining analysis"<< "\n\n";
	cout<< "What would you like your mean to be?" << endl;
	cin>> mean;
	

	ifstream in;
	in.open("C:\\TamStar\\TamStar.txt" );

	string jack;
	string jackOld;
	float jill= 0;
	float jillOld =0;

	in>> jack >> jill;

	jackOld=jack;
	jillOld=jill;

	values.push_back(jill);

	/* 
	This is the Fileuploading portion The user deamed
	that they wanted one long collum and the position
	are pushed back on to a vector to create itterato
	rs later
	*/
	while(in >> jack >> jill )
	{
		if(jackOld!=jack)
		{
			fileNames.push_back(jackOld);
			pos.push_back(values.size()-1);
		}
		
		values.push_back(jill);
		jackOld = jack;
		jillOld = jill;
		
	}
	fileNames.push_back(jackOld);	
	cout<< "values.size(),fileNames.size(): " << values.size()<<"," << fileNames.size() << endl;

	/*
	The Ka for loop uses the positions to take the 
	portions of the file that we want and analyze 
	files one at a time outputting them later
	*/
	for(int ka =0; ka<pos.size()-1; ++ka)
	{

		int vBegin = pos[ka];
		int vEnd = pos[ka+1];

		times.clear();
		bins.clear();
		times.resize(vEnd-vBegin+1);
		copy(values.begin()+vBegin,values.begin()+vEnd,times.begin());
		sort(times.begin(),times.end());
		binSize = (times.back()/times.size())*mean;
		x = binSize;
		y = 0;
		aCount= 0;



		// Algorithim
		for(int i =0; i<times.size(); ++i)                
		{
			if(times[i]>=y && times[i]<=x)
			{
				++aCount;
				if(i==times.size()-1)
				{
					bins.push_back(aCount);
				}
			}
			else
			{
				while(times[i]>=x)
				{
					bins.push_back(aCount);
					x =x+binSize;
					y =y+binSize;
					aCount =0;
					if(times[i]>=y && times[i]<=x)
					{
						++aCount;
					}
				}
				if(i==times.size()-1)
				{
					bins.push_back(aCount);
				}
			}
		}


		/*
		Calculates Bin Mean
		*/
		binMean=0;
		for(int i =0; i<bins.size(); ++i)
		{
			binMean = binMean+bins[i];
			if(i == bins.size()-1)
			{
				binMean = binMean/bins.size();
			}
		}


		/* 
		This is the statistical output 
		*/


		cout<< ka <<": "<< fileNames[ka]<< " Mean: " << binMean << "\n"; 
		std::sort (bins.begin(), bins.end());
		vector< vector<int> > binCount(bins.size(), vector<int>(bins.size()));
		
		// WHERE ERORR IS OCCURING
		for(int j=0; j<=bins.back(); j++)
		{
			int mycount;
			mycount = (int) count(bins.begin(), bins.end(),j);
			binCount[j][0]=j;
			binCount[j][1]= mycount;
		}

		/*
		The Data Output to a txt File
		*/

		ofstream myfile;
		myfile.open ("TamStarOutput.txt",ios::app);


		myfile<< fileNames[ka] << endl;
		for(int i=0; i<=bins.back(); ++i)
		{
			myfile<< binCount[i][0] << " appeared: " << binCount[i][1] << endl;
		}
		myfile.close();
	}
	in.close();

	for(int i=0; i<fileNames.size(); i++)
	{
		cout<< i << "."<< fileNames[i] << endl;
	}

	cout<< "Your Program is complete thank you for choosing TamStar drive safely"
		<< endl;
}


The for loop at line 144 which is indicated by the comment in caps "WHERE ERROR IS OCCURING", is indexing a vector out of its range in a particular instance.

The paramater that the user has control over is the binMean the user has a choice of making it any whole integer.

My error is occuring when I change the mean from "1" to '2' or '3'. I'm sure it is an indexing problem.

Thanks in Advance
for(int j=0; j<=bins.back(); j++)

is not how you go through a vector, bins.back() returns the last element of the vector, not it's index. Try j<bins.size().
The resason I am using bins.back() is because of the nature of the code.

The bins vector contains counted sequences of "bins".
Example of bins vector elements:
0 1 2 3 0 2 3 7
When sorted looks like this:
0 0 1 2 2 3 3 7.

When I use less than bins.back() it retrieves the elements 3, and we search for every number 0 to 7 in the vector.

Other wise I would be searching for every number,0,1,2,3,4...bins.size() which is usually equal to 14000+.

How could I keep the concept in tact without messing with the integrity of the code?
Topic archived. No new replies allowed.