Vector subscript out of range

This program reads in a list of numbers and sorts those numbers from low to high. After that it counts each unique number and the frequency of each unique number. In the end I am trying to output a histogram where if the input is:

20
30
4
20
30
30

The output should be:

Number of 4's: 1
Number of 20's: 2
Number of 30's: 3

The problem is in the last iteration of the count_repeats function within the count_uniques_and_repeats function. I keep getting an error message saying
expression: vector subscript out of range
.


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
 #include <iostream>
#include <vector>
using namespace std;

void get_input(vector<int>& v);
void swap_values(int& v1, int& v2);
int index_of_smallest(vector<int> list, int start_index);
void sort(vector<int>& list);
int count_repeats(vector<int> list, int start_index);
int count_total_uniques(vector<int> list);
int index_of_unique(vector<int> list, int& start_index);
void count_uniques_and_repeats(vector<int> list, int total_uniques,
	vector<int>& uniques_list, vector<int>& times_repeated_list);

int main()
{
	vector<int> input, uniques_list, times_repeated_list;
	int total_uniques;
	get_input(input);
	for (int i = 0; i < input.size(); i++)
		cout << input[i] << " ";
	cout << endl;
	sort(input);
	total_uniques = count_total_uniques(input);
	count_uniques_and_repeats(input, total_uniques, uniques_list, times_repeated_list);
	cout << "Sorted input:\n";
	for (int i = 0; i < input.size(); i++)
		cout << input[i] << " ";
	cout << endl;
	cout << "Uniques list:\n";
	for (int i = 0; i < uniques_list.size(); i++)
		cout << uniques_list[i] << " ";
	cout << endl;
	cout << "Times repeated list:\n";
	for (int i = 0; i < times_repeated_list.size(); i++)
		cout << times_repeated_list[i] << " ";
	cout << endl;
	system("pause");
	return 0;
}
void get_input(vector<int>& v)
{

	int next;
	cout << "Enter a list of numbers. End list with -1.\n";
	cin >> next;
	while (next != -1)
	{
		v.push_back(next);
		cin >> next;
	}
}
void swap_values(int& v1, int& v2)
{
	int temp = v1;
	v1 = v2;
	v2 = temp;
}
int index_of_smallest(vector<int> list, int start_index)
{
	int min = list[start_index];
	int index_of_min = start_index;
	for (int index = start_index + 1; index < list.size(); index++)
		if (list[index] < min)
		{
			min = list[index];
			index_of_min = index;
			//min is the smallest of list[start_index] through list[index].
		}
	return index_of_min;
}
void sort(vector<int>& list)
{
	int index_of_next_smallest;
	for (int index = 0; index < list.size() - 1; index++)
	{
		index_of_next_smallest = index_of_smallest(list, index);
		swap_values(list[index], list[index_of_next_smallest]);
		//list[0] <= list[1] <= ... <= list[index] are the smallest of the original
		//vector elements. The rest of the elements are in the remaining positions.
	}
}
int count_total_uniques(vector<int> list)
{
	int count = 1;
	for (int i = 0; i < list.size() - 1; i++)
		if (list[i] != list[i + 1])
			count++;
	return count;
}
int index_of_unique(vector<int> list, int& start_index)
{
	for (int i = start_index + 1; i < list.size(); i++)
		if (list[start_index] != list[i])
		{
			start_index = i;
			return i;
		}
}
int count_repeats(vector<int> list, int start_index)
{
	int count = 1;
	int index = start_index + 1;
	while ((list[start_index] == list[index]) && (index < list.size()))
		//if I have the parameter be ((list[start_index] == list[index])&& (index < list.size() - 1 ))
		//Then 
	{
		count++;
		index++;
	}
	return count;
}
void count_uniques_and_repeats(vector<int> list, int total_uniques,
	vector<int>& uniques_list, vector<int>& times_repeated_list)
{
	int start_index = 0;
	uniques_list.push_back(list[0]);
	times_repeated_list.push_back(count_repeats(list, start_index));
	cout << "Total_Uniques = " << total_uniques << endl
	<< "uniques_list = " << uniques_list[0] << endl
	<< "times_repeated_list = " << times_repeated_list[0] << endl;
	system("pause");

	
	for (int index = 1; index < total_uniques; index++)
	{
		int index_of_next_unique = index_of_unique(list, start_index);
		uniques_list.push_back(list[index_of_next_unique]);
		times_repeated_list.push_back(count_repeats(list, start_index));
		//The problem is right here in the count_repeats function.
		cout << "Index = " << index << endl
		<< "uniques_list = " << uniques_list[index] << endl
		<< "times_repeated_list = " << times_repeated_list[index] << endl;
		system("pause");
	}
}
Last edited on
The crash happens when index == 3 in count_uniques_and_repeats.
count_repeats() startindex is then 3 and index = 4 and list.size() == 4.

It seems to work when you check first if index is < list.size() in count_repeats().
while ((index < list.size()) && (list[start_index] == list[index]))
Thank you! That one change fixed the whole problem.
Topic archived. No new replies allowed.