Passing an array through multiple functions/heap sort

Any help on this would be appreciated. I have been trying to get this heap sort to work by passing my original array from function to function. Whenever I attempt to compile, it tells me that the identifier is not available for each instance of trying to pass the array to another function.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int recordLength = 23;
int menuSelection = 0;

void heapSort(string recordArray[], int length, int rows) {

heapify(recordArray, length);
int end = length - 1;

while (end > 0) {
swapVal(recordArray, end, 0);
siftDown(recordArray, 0, (end - 1));
end--;
}
}

void heapify(string recordArray[], int numOfRows) {
int start = numOfRows/2 - 1;

while (start >= 0) {
siftDown(recordArray, start, (numOfRows-1));
start++;
}
}

void siftDown(string recordArray[], int start, int end) {
int root;
int left;
int right;
int swap;

root = start;
while ((root*2 + 1) <= end) {
left = root*2 + 1;
right = root*2 + 2;
swap = root;

if (recordArray[swap] < recordArray[left]) {
swap = left;
}

if (swap != root) {
swapVal(recordArray, root, swap);
root = swap;
} else {
}
}
}

void swapVal(string recordArray[], int root, int swap) {
string tempString;
tempString = recordArray[root];
recordArray[root] = recordArray[swap];
recordArray[swap] = tempString;
}


int menuDisplay() {
int selection = 0;

while (selection == 0) {
cout << "1. Sort short.txt" << endl
<< "2. Print sortedShort.txt" << endl
<< "3. Sort long.txt" << endl
<< "4. Print sortedLong.txt" << endl
<< "5. Exit" << endl << endl
<< "Enter selection:";
cin >> selection;
if (selection >= 1 && selection <= 5) {
return selection;
} else {
cout << endl << "Invalid menu selection" << endl << endl;
selection = 0;
}
}
}

int main() {

menuSelection = menuDisplay();

string input;
string output;


switch (menuSelection) {
case 1:
//open files
int rows = 0;
fstream infile("short.txt", ios::in);
fstream outfile("sortedShort.txt", ios::out);

//get number of rows
infile.seekg(0, ios::end);
rows = infile.tellg() / recordLength;
infile.seekg(0, ios::beg);
//create array and load values into the array
string* recordArray;
recordArray = new string[rows];

for (int i = 0; i < rows; i++) {
getline(infile, recordArray[i]);
}

heapSort(recordArray, rows);

for (int j = 0; j < rows; j++) {
outfile << recordArray[j] << "\n";
}

infile.close();
outfile.close();


}

system("pause");
return 0;
}

Consider declaring the variable recordArray outside of the switch statement. Currently its only local to each case statement.

What is the exact error you are getting?
Thanks for the response. I attempted what you said and I am receiving the same errors.

1>c:\users\aric\documents\visual studio 2010\projects\assignment6\assignment6\pass7_4.cpp(12): error C3861: 'heapify': identifier not found
1>c:\users\aric\documents\visual studio 2010\projects\assignment6\assignment6\pass7_4.cpp(16): error C3861: 'swapVal': identifier not found
1>c:\users\aric\documents\visual studio 2010\projects\assignment6\assignment6\pass7_4.cpp(17): error C3861: 'siftDown': identifier not found
1>c:\users\aric\documents\visual studio 2010\projects\assignment6\assignment6\pass7_4.cpp(26): error C3861: 'siftDown': identifier not found
1>c:\users\aric\documents\visual studio 2010\projects\assignment6\assignment6\pass7_4.cpp(48): error C3861: 'swapVal': identifier not found

You need to put some prototypes in at the top of your source code.
That was the problem! Thank you. I had some other errors in the code, but putting my prototypes allowed me to debug the rest of it. Why do some functions work without using prototypes while others (like the ones in this program), require them?
If the compiler has already seen a function definition or declaration (i.e. prototype) before the point at which the function is called, it can understand the function call. So either the declaration is in a header file that you've included, or the function was defined earlier in the file.

If you want a function you've defined to be callable by code in other files (which will probably be the case if you go on to write bigger programs), then you should put the prototypes for those functions in a header file. That way, the other files can just include the header and get those prototypes easily.
Topic archived. No new replies allowed.