I don't get what is happening here. I used this exact code for another algorithm and it works but here i only get large negative numbers for an input, what gives?

My output is:
floatArray[0] is -107374176.000000
floatArray[1] is -107374176.000000
floatArray[2] is -107374176.000000
floatArray[3] is -107374176.000000
floatArray[4] is -107374176.000000
floatArray[5] is -107374176.000000
floatArray[0] is -107374176.000000
floatArray[1] is -107374176.000000
floatArray[2] is -107374176.000000
floatArray[3] is -107374176.000000
floatArray[4] is -107374176.000000
floatArray[5] is -107374176.000000

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");


	/* Tests to ensure files opened */
	if(fin.fail())
	{
		cerr << "Input did not open\n";
		exit(2);
	}
	
	 /* Construct New Array */
	float *floatArray= new float[d];

	/* Put numbers from list into array */
	float item;
	// printf("Put Numbers from list into array\n"); 
	for (j=0; j<d;j++)
	{	
		fin >> item;
		// printf("item is %f\n", item);
		floatArray[j]=item;
		printf("floatArray[%d] is %f\n", j, floatArray[j]);
	}
	
	/* Call to HEapSort */
	HeapSort(floatArray, len);

	/* 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();

	getchar();
}

Hmm. Can you show us your file?
Don't forget to delete []floatArray;

Edited 7 Years Ago by twomers: n/a

This article has been dead for over six months. Start a new discussion instead.