SIGSEGV Segmentation Fault in Array Sorter

Hi all, I am very new to programming and I am writing a short program that generates an array of random values between 0 and 500, prints the array, sorts the array, and then prints the array. I am getting a SIGSEGV when I try and sort the array. It occurs at line 26 when the two values are compared.

The first part of the program executes correctly but after the system("pause") (yes I know system is evil) I get the segmentation fault. I am using longs for my integers because I intend for this system to sort very large sets of numbers.
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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

long tempHolder;
long cellNumber = 0;
const long arrayWidth = 5;
const long arrayHeight = 2;

void printArray(long testArray[arrayWidth][arrayHeight]) {
    for (long arrayPositionVertical = 0; arrayPositionVertical <= arrayHeight - 1; ++arrayPositionVertical){
        cout << endl;
        for (long arrayPositionHorizontal = 0; arrayPositionHorizontal <= arrayWidth - 1; ++arrayPositionHorizontal) {
            cout << testArray[arrayPositionHorizontal][arrayPositionVertical] << " ";
        }
    }
}

void sortArray(long testArray[arrayWidth][arrayHeight]) {
    for (long arrayPositionVertical = 0; arrayPositionVertical < arrayHeight; ++arrayPositionVertical){
        for (long arrayPostitionHorzontal = 0; arrayPostitionHorzontal < arrayWidth; ++arrayPostitionHorzontal) {
            for (long testPositionVertical = arrayPositionVertical; testPositionVertical < arrayHeight; ++arrayPositionVertical){
                for (long testPositionHorizontal = arrayPostitionHorzontal + 1; testPositionHorizontal < arrayWidth; ++testPositionHorizontal){
                  if (testArray[arrayPostitionHorzontal][arrayPositionVertical] > testArray[testPositionHorizontal][testPositionVertical]) {
                        tempHolder = testArray[arrayPostitionHorzontal][arrayPositionVertical];
                        testArray[arrayPostitionHorzontal][arrayPositionVertical] = testArray[testPositionHorizontal][testPositionVertical];
                        testArray[testPositionHorizontal][testPositionVertical] = tempHolder;
                    }
                }
            }
        }
    }
    printArray(testArray);
}
int main()
{
    long myArray [arrayWidth][arrayHeight];
    for (long arrayPositionVertical = 0; arrayPositionVertical <= arrayHeight -1; ++arrayPositionVertical){
        for (long arrayPositionHorizontal = 0; arrayPositionHorizontal <= arrayWidth - 1; ++arrayPositionHorizontal) {
            cellNumber = rand() % 500;
            myArray[arrayPositionHorizontal][arrayPositionVertical] = cellNumber;
        }
    }
    printArray(myArray);
    cout << endl << "Array Created!" << endl;
    system("pause");
    sortArray(myArray);
    return 0;
}


If anybody could help at all I would greatly appreciate it. I am very new to programming (this is my second full program) and I do not understand a lot of the higher concepts of c++.
Thanks!
Last edited on
I'm guessing that the array index is out of bounds. I suggest you familiarize yourself with a debugger.
Topic archived. No new replies allowed.