help please. vector subscript out of range error.

I am writing this function for merging two vectors in order.
each vector should be in order already before merging, so check that in the beginning.
after that I start to copy two vectors in order, to third vector using loop.
However when I implement this in main function and run this program,
an error pops up saying vector subscript out of range error..... i have been struggling with this more then 10 hours... please help

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

bool mergeTwoSortedVectors
(vector<double> & vecA, vector<double> & vecB, vector<double> & vecC)
{

	int sizeA = vecA.size();
	int sizeB = vecB.size();

	//if vecA or vecB is not sorted, return false
	for (int i = 0; i < sizeB && i< sizeA; i++)
	{
		if ((vecB[i]>vecB[i + 1]) || (vecA[i]>vecA[i + 1]))
		{
			return false;
		}
	}
	
	for (int i = 0; i < sizeB; i++)
	{
		if (vecB[i]>vecB[i + 1])
		{
			return false;
		}
	}

	//for (int i = 0; i < sizeA; i++)
	//{
		//if (vecA[i]>vecA[i + 1])
		//{
			//return false;
		//}
	//}


	//resizing space of vecC to fit all the contents of vecA and vecB
	vecC.resize(sizeA + sizeB);
	

	//initializing counts to 0 before start merging
	int countA = 0, countB = 0, countC = 0;

	//copy copy elements whatever is smaller to vecC from vecA or vecB and increase count that is copied
	//untill there one of the vector's elements are all copied. 
	for (; (countA < sizeA) && (countB < sizeB); countC++)
	{
		if (vecA[countA] <= vecB[countB])
		{
			vecC[countC] = vecA[countA];
			countA++;
		}
		else if (vecA[countA] > vecB[countB])
		{
			vecC[countC] = vecB[countB];
			countB++;
		}
	}


	//if there is element left in countA copy all the elements to vecC
	while (countA < sizeA)
	{
		vecC[countC] = vecA[countA];
		countA++, countC++;
	}

	//if there is element left in countB copy all the elements to vecC
	while (countB < sizeB)
	{
		vecC[countC] = vecB[countB];
		countB++, countC++;
	}

	return true;
}

int main()
{	
	int end;
	int sizeA, sizeB;
	vector<double> vecA;
	vector<double> vecB;
	vector<double> vecC;

	cout << "please enter sizes of two vectors" << endl << endl;
	cout << "Size of vector 1 ";
	cin >> sizeA;
	cout << endl << "Size of vector 2 ";
	cin >> sizeB;
	cout << endl;

	vecA.resize(sizeA);
	vecB.resize(sizeB);
	vecC.resize(vecA.size()+vecB.size());

	cout <<endl<< "Please enter values in the first vector";
	
	
	for (int i=0; i < sizeA; i++)
	{
		cout << endl << "Element#" << i+1 << ": ";
		cin >> vecA[i];
		
	}

	cout <<endl<< "Please enter values in the second vector";
	for (int i = 0; i < sizeB; i++)
	{
		cout << endl << "Element#" << i+1<< ": ";
		cin >> vecB[i];
	}

	if ((mergeTwoSortedVectors(vecA, vecB, vecC)) == false)
	{
		cout << "values are not sorted";
	}
	
	else
	{

		cout << endl;

		for (int i = 0; i < sizeA + sizeB; i++)
		{
			cout << vecC[i];
		}
	}
	cin >> end;
}
Last edited on
See anything wrong on line 15 or 23?
Overall, the loop on lines 13-19 has very strange logic, not just in line 15.


I presume that you intentionally want to implement the merge, rather than using std::merge (which is about 5 lines)?
Topic archived. No new replies allowed.