The code should be pretty close to where i need it to be. It's just a sorting code for an input file that contains 6 numbers. What am i doing wrong?
#include <iostream>
#include <fstream>
#include <iomanip>
#include <math.h>
using namespace std;
/****** Variables *****/
/* Size of heap for BuildMaxHeap */
int heapSize;
double doubleNumber;
float floatNumber;
/* Vars for MaxHeapify */
int l, r, largest;
float temp;
/* Vars for HeapSort */
float temp2;
/* Iterator */
int i, j;
/*MaxHeapify Procedure */
void MaxHeapify(float A[], int i)
{
l=2*i;
r=2*i+1;
if ((l <= heapSize) && (A[l] > A[i]))
largest=l;
else
largest=i;
if ((r <= heapSize) && (A[r] > A[largest]))
largest=r;
if (largest != i)
{
temp=A[i];
A[i]=A[largest];
A[largest]=temp;
MaxHeapify(A, largest);
}
}
/* BuildMaxHeap Procedure */
void BuildMaxHeap(float A[])
{
heapSize=sizeof(A);
floatNumber=sizeof(A)/2;
doubleNumber=(double)floatNumber;
doubleNumber=floor(doubleNumber);
floatNumber=(float)doubleNumber;
for(i=floatNumber;i=1;i--)
MaxHeapify(A,i);
}
/* HeapSort Procedure */
void HeapSort(float A[])
{
for (i=heapSize;i=2;i--)
{
temp2=A[1];
A[1]=A[i];
A[i]=temp2;
heapSize=heapSize-1;
MaxHeapify(A,1);
}
}
int main(int argc, char *argv[])
{
/* Extract value for # of elements in array */
int d;
sscanf (argv[2], "%d", &d);
/* Create Input and Output Streams */
ifstream fin;
ofstream fout;
/* Open Input Stream */
fin.open("numlist.dat");
/* Test to ensure file opened */
if(fin.fail())
{
cerr << "Input did not open\n";
exit(2);
}
/* Construct New Array for float storage*/
float *floatArray= new float[d];
/* Put numbers from list into array */
float item;
for (j=0; j<d;j++)
{
fin >> item;
floatArray[j]=item;
printf("floatArray[%d] is %f\n", j, floatArray[j]);
}
/* Call to HeapSort */
HeapSort(floatArray);
/* Display Array After Sort */
for (j=0; j<d;j++)
{
printf("floatArray[%d] is %f\n", j, floatArray[j]);
}
/* Close file for Reading */
fin.close();
/* Open Output Stream */
fout.open("numlist.dat.srt");
/* Tests to ensure files opened */
if(fout.fail())
{
cerr << "Input did not open\n";
exit(2);
}
/* Write Output */
for (i=0;i<d;i++)
fout << floatArray[i] << endl;
/* Close File */
fout.close();
/* Keep Window Open */
getchar();
}