Debug assertion error with C++

I have written some code for a photon simulation involving different skin types.
I am using windows visual studio (visual C++) for windows 8 (x86). The code is quite long and it build fine (compiles) but when I run I get an assertion error.

It says:

"
Debug Asserion Failed!
Program: C:\WINDOWS\SYSTEM32\MSVCP140D.dll
File: c:\program files (x86)/microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\vector

Line: 1796

Expression: vector subscript out of range

"

I went to line 1796 in my very long code but I have nothing there but:
1796- cauchyY[i] = p1.getCauchy(yVal[i], getMedianY, 1.0);
(these are vectors that store doubles)

The only other place in my code where I think my cause an issue is where I have used functions involving vectors to do integration (which is used to calculate power).

Function regarding Trapezoid Rule:

double functIntegralTrap() {

int i, j;
double sum = 0;
double integral = 0;
cout << "Please enter the number of subintervals for integration ";
cin >> subIntervals;
vector <double> x(subIntervals + 1);
vector <double> result(subIntervals + 1);
timeDiff = static_cast<double>(getTimeForPower());
termCurr = termVolt / resistance;
phaseDiff = thetaVol - thetaCurr;
intermediary = (termVolt*termCurr) / 2.0;
deltax = timeDiff / static_cast<double>(subIntervals);
for (i = 0; i < subIntervals + 1; i++) {
x.at(i) = lowerTime + i*deltax;
result.at(i) = x.at(i)*intermediary*cos(phaseDiff*PI / 180) + intermediary*cos(phaseDiff*PI / 180)*cos(2.0*getangularfreq()*x.at(i)) - intermediary*sin(phaseDiff*PI / 180)*sin(2.0*getangularfreq()*x.at(i));
}

for (i = 1; i < subIntervals; i++) {
sum = sum + deltax*result.at(i); //need to be careful of this section regarding result.at.i
}
integral = (deltax / 2.0)*(result.at(0) + result.at(subIntervals)) + sum;
cout << "The definite integral is " << integral << endl;
return integral;
}

Function Involving Riemann sums:

double sumRiemannIntegral() {

double cumsum = 0.0;
int i, j;
double integral = 0.0;
cout << "Please enter the number of subintervals (rectangles):";
cin >> subIntervals;
vector <double> x(subIntervals);
vector <double> result(subIntervals);
vector <double> rectangArea(subIntervals);
timeDiff = static_cast<double>(getTimeForPower());
termVolt = 20.0;
resistance = 6.0;
termCurr = termVolt / resistance;
phaseDiff = thetaVol - thetaCurr;
intermediary = (termVolt*termCurr) / 2.0;
deltax = timeDiff / static_cast<double>(subIntervals);

for (i = 0; i < subIntervals; i++) {

x.at(i) = lowerTime + i*deltax;
result.at(i) = result.at(i) = x.at(i)*intermediary*cos(phaseDiff*PI/180) + intermediary*cos(phaseDiff*PI/180)*cos(2.0*getangularfreq()*x.at(i)) - intermediary*sin(phaseDiff*PI/180)*sin(2.0*getangularfreq()*x.at(i));

}

for (i = 0; i <subIntervals; i++) {
rectangArea.at(i) = deltax * result.at(i); //This changed from rectangArea.at(i) = x.at(i) * result.at(i);
cumsum = cumsum + rectangArea.at(i);
}

return cumsum;
}

In debug mode the vector class will throw an out_of_range error when your code uses the subscript operator[] to access an element outside the valid range i.e. 0 - size() -1
`1796- cauchyY[i] = p1.getCauchy(yVal[i], getMedianY, 1.0);`
if cauchyY or yVal are vectors you need to check the value of i and the size of cauchyY and yVal

These are all the pieces of code involving yVal and cauchyY

-START OF CODE BLOCK

Note: cr1.photon belongs to a class called runMC an equals 10000 -I want to

vector<double> yVal(cr1.photonN);
yVal.reserve(cr1.photonN);

for (int i = 0; i < p1.current_Phot; i++) {//opening for
xVal[i] = x;
yVal[i] = y;
zVal[i] = z;

}//closing for

for (int i = 0; i < cr1.photonN; i++) {
cauchyY[i] = p1.getCauchy(yVal[i], getMedianY, 1.0);
proposalY[i] = yVal[i] + unifNum1; //unifNum1 is a random number
}

getmedianPropY = p1.CalcMedian(proposalY, cr1.photonN);
for (int i = 0; i < cr1.photonN; i++) {
ratioCauchyY[i] = p1.getCauchy(proposalY[i], getmedianPropY, 1.0) / cauchyY[i];

if (unifNum2 < ratioCauchyY[i]) {
acceptY[i] = yVal[i + 1];

}//if for y
else {
acceptY[i] = yVal[i];
}//else for y

acceptYArr[i] = acceptY[i];
}
double getCauchy(double Val, double median, double scaleParam) {

//calculate cauchy pdf used for the proposal

return (1 / PI)*(scaleParam / (pow(Val - median, 2.0)) + pow(scaleParam, 2.0));
} //Here the median acts as the location parameter

double CalcMedian(vector <double> v1, int size) {//function start -(IMPORTANT)
// Allocate an array of the same size and sort it.
vector<double> doub_sorted;
doub_sorted.reserve(sizeof v1);
// double* doub_sorted = new double[size];IMPORTANT (ALLOCATE MEMORY FOR NEW 1D ARRAY THAT STORES DOUBLES)
for (int i = 0; i < size; ++i) {
doub_sorted[i] = v1[i];
}
for (int i = size - 1; i > 0; --i) {
for (int j = 0; j < i; ++j) {
if (doub_sorted[j] > doub_sorted[j + 1]) {
double store = doub_sorted[j];
doub_sorted[j] = doub_sorted[j + 1];//THIS IS TO GET THE NUMBERS OF THE ARRAY IN NUMERICAL (ASCENDING ORDER) ORDER WHICH IS NEEDED TO CALCUALTE THE MEDIAN
doub_sorted[j + 1] = store;
}//end if
}//end for
}//end for

// Middle or average of middle values in the sorted array.
double median_m = 0.0;
if ((size % 2) == 0) {
median_m = (doub_sorted[size / 2] + doub_sorted[(size / 2) - 1]) / 2.0;
}
else {
median_m = doub_sorted[(int)size / 2];//IMPORTANT: CALCULATING THE MEDIAN DEPENDENT ON WHETHER WE HAVE ODD OR EVEN NUMBERS (eg if odd and you have 5 numbers size/2= 2); if even you value
//above and below half-way.
} //IMPORTANT (WHEN CREATING MEMORY DYNAMICALLY WITH NEW YOU NEED TO DELETE THE ARRAY AFTERWARDS (AS FAR AS I KNOW WITHIN METHODS)
return median_m;
}//function end

-END OF CODE BLOCK

JUST SOME MORE INFORMATION IF ANYONE HAS ANY OTHER HELP TO GIVE ME
Hi,

Consider using the debugger to look at the values you have when running the program, it will save you days of staring at code wondering why it doesn't work.

So you have:

 ``1234`` ``````for (int i = 0; i < cr1.photonN; i++) { cauchyY[i] = p1.getCauchy(yVal[i], getMedianY, 1.0); proposalY[i] = yVal[i] + unifNum1; //unifNum1 is a random number }``````

What is the value of` cr1.photonN`, this is the limit of the for loop, so it's important. Is it bigger than the size of `yVal` ?

When you wish to process an entire container, consider using a ranged based for loop, it is less error prone:
http://en.cppreference.com/w/cpp/language/range-for

 ``123`` ``````for (const auto& item : container) { //item is one of the things in container such as a vector // do something with item }``````

Don't use `new`, use an STL container instead it does all the memory management itself.

There are sort algorithms in the STL, one doesn't have to write their own.

Good Luck !!
Value of cr1.photonN is 10000 it is to be used as part of a loop to track photon positions in 3D (X, Y AND Z) and in the azimuthal and zenith angles theta and psi.
By the way do you realize that the following:
 It says: " Debug Asserion Failed! Program: C:\WINDOWS\SYSTEM32\MSVCP140D.dll File: c:\program files (x86)/microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\vector Line: 1796 Expression: vector subscript out of range "

appears to be referring to the "vector" include file, not your source files. You probably need to back trace with your debugger to somewhere in your code.

I hope it does not mean the vector include file was corrupted during installation of visual studio 2017 community version at the path mentioned on the c drive. Sorry for the slight typo error in spelling... it is Debug Assertion Failed!.
 I hope it does not mean the vector include file was corrupted during installation of visual studio 2017 community version at the path mentioned on the c drive.

Not at all, the bug is only in your program. The code in the <vector> justs reports that you did sth. wrong.
No, that is just where the program crashed the actual problem is somewhere before the line indicated. You need to run the program with your debugger. The debugger should be able to let you backtrack from the crash location to a point in your code. Then you can step through the problematic area watching the variables as you step.

Thanks for the confirmation regarding the bug guys.
I will just mention this again:

If you use STL containers and range based for loops, I suspect a lot of the problems will go away.

Another thing:

 ``123456`` ``````for (int i = 0; i < p1.current_Phot; i++) {//opening for xVal[i] = x; yVal[i] = y; zVal[i] = z; }//closing for ``````

This shows you have 3 separate vectors for the xyz coordinates of points, consider creating a struct to represent a Point. Then have a std::vector of Point.

 ``1234567`` ``````struct Point { double x; double y; double z; }; std::vector AllThePoints = {};``````

In C++ a `struct` is almost the same as a `class` (it's members are `public` by default), so you could add constructors, operators etc if you want.

That way you could have one ranged based for loop to process a container, not 3, and not 3 sets of containers everywhere. Also the functions that operate on points can all be in one place - in the struct or class. I am guessing the whole code will be shorter, easier to understand and less error prone.
I know what you mean I read some more on the STL and went on the GeeksForGeeks website where they have a nice STL section involving all the sets, arrays, maps vectors and the functions that come under the include <algorithm> header. The link for range based for loop was good too for getting the indexing right. I am making the modifications too my long code (2000 lines) and will enquire for further information if needed. Thanks for the help from you all.