Segmentation Fault

I still pretty bad with pointers and passing by reference but i know a segmentation fault is a problem with memory somewhere. I am getting a segfault error any ideas why

this is the function thats causing it

void findStatsRef(double salesArray[], double& sum, double& mean, double& median)
{
double Array[12] = {0};

for (int index =0; index <= 12; index++)
Array[index] = salesArray[index+1];

sortArray(Array);

}

void sortArray(double Array[])
{
for (int pass=0; pass=NUM_MONTHS-1; pass++)
{
int fpass = pass; //assume this is the smallest element

for (int i = pass+1; i < NUM_MONTHS; i++)
{
if (array[i] < array[fpass])
{
fpass = i;
}
}
int temp = Array[pass];
Array[pass] = Array[fpass];
Array[fpass] = temp;
}
}
An array A[12] has 12 values, indexed by 0..11.
for (int index =0; index <= 12; index++)
is therefore wrong.
As always, I strongly recommend the usage of stl containers instead of C arrays. In this case, the same error would have occurred, but you could have used std::vector<T>::at() which would have thrown an exception (std::out_of_range) that would have pointed you to the exact location of the error (Stroustrup recommends a Vector:public std::vector which hides operator[] and replaces it with at(). Be careful not to use this Vector polymophically (through a base class pointer), though. It would be disastrous, since std::vector has no virtual destructor).

Edit:
though not contained in your code, you can compute:
1
2
3
4
sum = std::accumulate(Array, Array+11, 0);
mean = sum/12.0;
std::nth_element(Array, Array+5, Array+12);
median = Array[5];

nth_element changes Array, but should have complexity of O(n), which is faster than sorting (which needs Ω(n log n))
Last edited on
i do appreciate your help but i really didnt understand anything you said
(a) your Array (I would tell you the line in your code if you had used the "code"-tags) has 12 fields. The first is named Array[0], the last is Array[11]. You are trying to access Array[12] (and salesArray[13]):
1
2
for (int index =0; index <= 12; index++)
Array[index] = salesArray[index+1];

index becomes 12 (you should write ...index < 12..., not <=) and then you add one in salesArray[index+1] (which , I presume, has 12 fields, too).
This is the actual cause for the error you get.

(b) C++ contains a library, the STL (Standard Template Library), in which containers, iterators and algorithms are defined. Learn how to use the stl, it will reduce the ammount of work you have to do by ~80%. It is contained in the C++ standard, so it is available on every platform and every C++ compiler.
What I suggested was that you substitute your C-style array Array[12] (and the parameters like double array[]) by std::vector<double>, thereby using the container "vector" (which behaves exactly like an array, but is "smarter" - e.g., it has a function "size()" which returns the number of elements in the vector). Then I suggested that you use algorithms of the stl, namely accumulate and nth_element, which sum the contents of any container or writes the nth element on the position it would be if the sequence was sorted, respectively (the median would be the middle element, thus the parameters I specified).
Since the containers are different and the algorithm is not re-written for each container, iterators are used as "glue" between containers and algorithms in the stl (in the example above, pointers are used as iterators). However, this is somewhat complicated for in the beginning and you should read a book to learn it properly. I reccommend the C++ Primer as a first reading (just seach the name on bookzilla.org or alike).
More briefly, then:
Replace
for (int index =0; index <= 12; index++)
with
for (int index =0; index < NUM_MONTHS; index++)

And also
for (int pass=0; pass=NUM_MONTHS-1; pass++){
with
for (int pass=0; pass<NUM_MONTHS-1; pass++){

Finally,
1
2
3
        int temp = Array[pass];
        Array[pass] = Array[fpass];
        Array[fpass] = temp;

with
1
2
3
4
5
    if (Array[fpass]<Array[pass])){
        int temp = Array[pass];
        Array[pass] = Array[fpass];
        Array[fpass] = temp;
    }
I get what your saying ... that worked thanks
Topic archived. No new replies allowed.