0

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();
}
1
Contributor
2
Replies
3
Views
8 Years
Discussion Span
Last Post by kingcrim05
0

I suppose i should post my output as well:

'qsort.exe': Loaded 'C:\Users\Jeff\Documents\Visual Studio 2008\Projects\qsort\Debug\qsort.exe', Symbols loaded.
'qsort.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll'
'qsort.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll'
'qsort.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.21022.8_none_96748342450f6aa2\msvcp90d.dll', Symbols loaded.
'qsort.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.21022.8_none_96748342450f6aa2\msvcr90d.dll', Symbols loaded.
First-chance exception at 0x65e3f8bc (msvcr90d.dll) in qsort.exe: 0xC0000005: Access violation reading location 0x555c3a63.
Unhandled exception at 0x65e3f8bc (msvcr90d.dll) in qsort.exe: 0xC0000005: Access violation reading location 0x555c3a63.

0

anyone? I've been stuck on this all day and have no idea where it is wrong.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.