Hello everyone!! I have this exercise and i want a small help to understand how the heap sorting of an array works. Actually my exercise says to make a file (input.txt) put inside some random numbers with first number of the file the length of the numbers (for example if we ask the user how many numbers wants in the file and let's say says 5 the first number will be 5 and then the random numbers ) and then take from the file these random numbers and put them in an array, sort them (with the heap sort) and print the array in another file (output.txt). I managed to do the first part of the exercise but i stuck in the part of sorting the array!!! I search it lots of times but i am little bit confused with the way of doing it.
This is my code of creating the file and put the random numbers in it. Then i take the numbers and i put them in the Array[].
Every small help is exepted. Thank you :) :)Code:
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <time.h>
# define LIMIT 1000
void create_file(FILE *fp,int N)
{
int i, random_number;
if ( fp == NULL )
{
printf ( "The file can not be created." ) ;
getch() ;
exit (0) ;
}
srand ( time(NULL) );
fprintf(fp, "%d ", N);
printf("%d ", N);
for ( i = 1; i < N; i++ )
{
random_number = rand()%LIMIT;
fprintf(fp, "%d ",random_number);
printf( "%d ", random_number) ;
}
}
void put_inArray(FILE *fp, int *Array, int N)
{
int i;
if ( fp == NULL )
{
printf ( "The file can not open for reading." ) ;
getch() ;
exit (0) ;
}
for(i=0; i<N; i++)
{
fscanf(fp, "%d ", Array[i]);
}
}
void heap_create(int *Array, int N)
{
//I don't know what to put here. I just want your help
}
int main()
{
char file_name[50];
int N;
FILE *fp;
int i;
printf("Please give name of the file you want to create: ");
scanf("%s", file_name);
printf("Please give how many numbers you want to put in the array: ");
scanf("%d", &N);
int Array[N];
fp = fopen(file_name, "w");
create_file(fp,N);
printf("Please give the name of the file you want to open: ");
scanf("%s", file_name);
fp = fopen(file_name, "r"); //I am trying to open the file to read it and take the numbers and put them in the Array[].
put_inArray(fp, Array,N);
// heap_create(); i am not sure for this function
return 0;
system("pause");
}