Click here to Skip to main content
15,912,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :
C++
//filee.c

// Creating files and writing data into them


#include<stdio.h>
#include<conio.h>
#define SIZE 30000
void main()
{

FILE *fp;  //fp is the file pointer 

int i , y[SIZE];  // y[SIZE] is the array that I am having issues with (discussed above)
clrscr();

//creating file num1.txt and writing into it

fp=fopen ("num1.txt", "w");
for (i=0;i<1000;i++)
{
  y[i]=rand()/100;
  fprintf (fp,"%d\n",&y[i]);
}
close(fp);

//creating file num2.txt and writing into it

fp=fopen ("num2.txt", "w");
for (i=0;i<10000;i++)
{
  y[i]=rand()/100;
  fprintf (fp,"%d\n",&y[i]);
}
close(fp);


//creating file num3.txt and writing into it


fp=fopen ("num3.txt", "w");
for (i=0;i<20000;i++)
{
  y[i]=rand()/100;
  fprintf (fp,"%d\n",&y[i]);
}
close(fp);

//creating file num4.txt and writing into it


fp=fopen ("num4.txt", "w");
for (i=0;i<30000;i++)
{
  y[i]=rand()/100;
  fprintf (fp,"%d\n",&y[i]);
}
close(fp);


getch();
}



// Implemting heapsort   


#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; // file pointer

time_t t1,t2;
clrscr();

//User input to select the file according to the size

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);  /* Gets system time */

  createheap(y,size); // heap sort functions
  heapsrt(y,size); //heapsort functions

  t2 = time(NULL); /* Gets system time
			   again */

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();
}


/*function definitions*/

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;
}
}
Posted
Updated 13-Dec-14 16:10pm
v2

1 solution

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
Member 11287281 15-Dec-14 12:40pm    
Sure OriginalGriff I will give it another shot. Thank you.
Member 11287281 15-Dec-14 14:04pm    
OriginalGriff, may I ask you for a good reference material/book that deal deeply in file handling stuffs in C.
OriginalGriff 15-Dec-14 14:13pm    
I haven't got a clue! :laugh:
it's been 20+ years since I least read a "C" book on anything!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900