Hi everyone. I am almost finished with this assignment but I am getting some extra numbers at the very end of my output file after the integers are sorted. the output file looks like:
Code:
0
1
2
3
.
.
.
98
99
1297040174
My main looks like this:
Code:
#include "header.h"
int main(int argc, const char *argv[]) {
int i = 0, N = 0, *array, k, dummy;
//hrtime_t start, end;
FILE *input;
FILE *output;
input = fopen("100_random.txt", "r");
if (input == NULL) {
fprintf(stderr, "Cannot open file for reading\n");
exit(EXIT_FAILURE);
}
while (!feof(input)) {
fscanf(input, "%d", &dummy);
N++;
}
if (fclose(input)) {
fprintf(stderr, "Error closing the file.\n");
exit(EXIT_FAILURE);
}
array = (int*) malloc(N* sizeof(int));
if(array == NULL)
printf("Error - could not allocate an array.\n");
else {
input = fopen("100_random.txt", "r");
for(k = 0; k < N; k++)
fscanf(input, "%d", &(array[k]));
fclose(input);
}
//start = gethrtime();
//insertion_sort(array,i);
//end = gethrtime();
//printf("The average time for the insertion_sort function was: %11d nsec.\n", (end - start)/n);
//selection_sort(array, i);
bubble_sort(array, k);
//mod_bubble_sort(array, i);
//quick_sort(array, 0, i - 1);
//merge_sort(array, 0, i -1);
output = fopen("out.txt", "w+");
for (i = 0; i < N; i++){
fprintf(output, "%d\n", array[i]);
}
free (array);
array = NULL;
return (EXIT_SUCCESS);
}
I have to sort 3 different sized arrays:
1) 100 ints
2) 1000 ints
3) 10000 ints
I noticed I do NOT get the extra numbers at the end of the 10000 int sort. Just the 100 and 1000.
I have to say, that the output is not do to the sorting functions. It happened when I just wrote to the file NOT using the sort funtions.
Anyone have any idea how to solve this and what might be wrong?
The sort functions should be correct as I have tested them. But if you need to see them, let me know.
UPDATE: I ran this on our unix system and I do NOT get the extra line with numbers on it. I'm thinking it might be CodeBlocks. If I am incorrect, I am still willing to listen to reasons and solutions.