a) My aim is to create files num1.txt, num2.txt, num3.txt, num4.txt, num5.txt of integer numbers of sizes
1000, 10000, 20000, 30000, 50000 respectively without the help of array.
b) After creating them I need to write only once into the above files , the random integer numbers using
rand() function for loop and fprintf and leave them as it is.
After that I need to implement heapsort using the data of the above files.
So in order to implement it I have taken the below steps:
1) Select the file (files cretaed and written in steps a and b respectively above) according to the user input
of data size (using if else).
2) After the appropriate file has been selected, read the data (in this case read the integer numbers) form
the file directly (without help of any array).
3) Then use heapsort algo to sort it and find the program run time. I have used difftime() library function to
do this job.
4) After sorting the data write the result into some another integer number file (result.txt).
5) Read and print the sorted file (result.txt) from the directly without the help of any array.
My issue :
From (a and b above) I am able to create and write files named num1.txt, num1.txt, num2.txt, num3.txt,
num4.txt of integer numbers of sizes 1000, 10000, 20000, 30000 respectively - "but only with the help of
an integer array y[]".
I dont want to use an integer array y[]. I wish I could directly write into the file "without" the array being an
intermediary. The reason I donot want to use the array is because when I use an integer array to write
into the above named files, the C compiler doesnot accept more than 30,000 array integer numbers. I
guess that is the maximum limit of the amout of space that the memory can reserve during program
execution time
So I am stuck with a maximum of 30,000 integer numbers.
Same issue with steps 2, step 4, step 5.
To summarize, I donot want to use an array as an intermediary for writing into and reading from the file.
Please advise how could this be implemented without using an array.
Below is the code :
#include<stdio.h>
#include<conio.h>
#define SIZE 30000
void main()
{
FILE *fp;
int i , y[SIZE]; clrscr();
fp=fopen ("num1.txt", "w");
for (i=0;i<1000;i++)
{
y[i]=rand()/100;
fprintf (fp,"%d\n",&y[i]);
}
close(fp);
fp=fopen ("num2.txt", "w");
for (i=0;i<10000;i++)
{
y[i]=rand()/100;
fprintf (fp,"%d\n",&y[i]);
}
close(fp);
fp=fopen ("num3.txt", "w");
for (i=0;i<20000;i++)
{
y[i]=rand()/100;
fprintf (fp,"%d\n",&y[i]);
}
close(fp);
fp=fopen ("num4.txt", "w");
for (i=0;i<30000;i++)
{
y[i]=rand()/100;
fprintf (fp,"%d\n",&y[i]);
}
close(fp);
getch();
}
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<time.h>
void createheap (int x[],int n);
void heapsrt (int x[],int n);
void main()
{
int y[30000];
int i,size;
FILE *fp;
time_t t1,t2;
clrscr();
printf ("\nEnter the size of the data : ");
scanf ("%d", &size);
if (size > 0 && size <=1000)
{
fp=fopen ("num1.txt", "r");
for (i=0;i<size;i++)>
{
fscanf (fp,"%d",&y[i]);
printf ("%d ", y[i]);
}
}
else if (size > 1000 && size <=10000)
{
fp=fopen ("num2.txt", "r");
for (i=0;i<size;i++)>
{
fscanf (fp,"%d",&y[i]);
printf ("%d ", y[i]);
}
}
else if (size > 10000 && size <=20000)
{
fp=fopen ("num3.txt", "r");
for (i=0;i<size;i++)>
{
fscanf (fp,"%d",&y[i]);
printf ("%d ", y[i]);
}
}
else if (size > 20000 && size <=30000)
{
fp=fopen ("num4.txt", "r");
for (i=0;i<size;i++)>
{
fscanf (fp,"%d",&y[i]);
printf ("%d ", y[i]);
}
}
else
{
printf ("\nInvalid entry!!");
exit (1);
}
t1 = time(NULL);
createheap(y,size); heapsrt(y,size);
t2 = time(NULL);
fp = fopen ("result.txt","w");
for (i=0;i<size;i++)>
{
fprintf (fp, "%d\n", y[i]);
}
fclose (fp);
printf ("\nHeap sort : \n");
fp = fopen("result.txt","r");
for (i=0;i<size;>{
fscanf (fp,"%d",&y[i]);
printf ("%d ",y[i]);
}
printf("\n The sorting done in : %f seconds\n",difftime(t2,t1));
getch();
}
void createheap (int arr[], int num)
{
int i,numval,s,f;
for (i=0;i<num;i++)>
{
numval=arr[i];
s=i;
f=(s-1)/2;
while (s>0&&arr[f]<numval)>
{
arr[s] = arr[f];
s=f;
f=(s-1)/2;
}
arr[s]=numval;
}
}
void heapsrt (int arr[], int num)
{
int i,s,f,val;
for (i=num-1;i>0;i--)
{
val = arr[i];
arr[i]=arr[0];
f=0;
if (i==1)
s=-1;
else
s=1;
if (i>2 && arr[2]>arr[1])
s=2;
while (s>0 && val < arr[s])
{
arr[f]=arr[s];
f=s;
s=2*f+1;
if (s+1 <= i-1 && arr[s] < arr[s+1])
s++;
if (s > i-1)
s=-1;
}
arr[f]=val;
}
}