In the following program the inputs are taken randomly
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
FILE *fp;
int i,n,x,a[45],l=0,m,o,y,z,j;
clrscr();
randomize();
fp=fopen("list1.txt","w");
if(fp==NULL)
{
puts("cannot open file");
exit(0);
}
n=rand()%14;
for(i=1; i<=n; i++)
{
x=rand()%1000;
a[i]=x;
fprintf(fp,"%d\t",x);
}
fclose(fp);
fp=fopen("list2.txt","w");
if(fp==NULL)
{
puts("cannot open file");
exit(0);
}
m=rand()%14;
for(i=1; i<=m; i++)
{
y=rand()%1000;
a[n+i]=y;
fprintf(fp,"%d\t",y);
}
fclose(fp);
fp=fopen("list3.txt","w");
if(fp==NULL)
{
puts("cannot open file");
exit(0);
}
o=rand()%14;
for(i=1; i<=o; i++)
{
z=rand()%1000;
a[n+m+i]=z;
fprintf(fp,"%d\t",z);
}
fclose(fp);
/* reading of first files starts here*/
fp=fopen("list1.txt","r");
if(fp==NULL)
{
puts("cannot open file");
exit(0);
}
printf("\nfirst list:----\n");
while(fscanf(fp,"%d",&x)!=EOF)
printf("%d\t",x);
fclose(fp);
/*reading of second file starts here*/
fp=fopen("list2.txt","r");
if(fp==NULL)
{
puts("cannot open file");
exit(0);
}
printf("\nsecond list:----\n");
while(fscanf(fp,"%d",&y)!=EOF)
printf("%d\t",y);
fclose(fp);
/*reading of third file starts here*/
fp=fopen("list3.txt","r");
if(fp==NULL)
{
puts("cannot open file");
exit(0);
}
printf("\nthird list:----\n");
while(fscanf(fp,"%d",&z)!=EOF)
printf("%d\t",z);
fclose(fp);
printf("\n----------------------------------\n");
/*sorting all elements of array*/
printf("\nfinal sorted list-----------------\n");
for(i=1;i<=n+m+o;i++)
{
for(j=1;j<=n+m+o;j++)
{
if(a[j]>=a[j+1])
{
int temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
/*storing values in final file*/
fp=fopen("list4.txt","w");
if(fp==NULL)
{
puts("cannot open file");
exit(0);
}
for(i=1; i<=m+n+o; i++)
{
x=a[i];
fprintf(fp,"%d\t",x);
}
fclose(fp);
/*reading final file*/
fp=fopen("list4.txt","r");
if(fp==NULL)
{
puts("cannot open file");
exit(0);
}
while(fscanf(fp,"%d",&x)!=EOF)
printf("%d\t",x);
fclose(fp);
getch();
}
How do we modify this to take input from user
that are 3 list of numbers one by one
then we may get output( merged and sorted list)
how do i modify this program to
Started by
Guest_irfath_*
, Mar 05 2007 12:11 AM
1 reply to this topic
#1
Guest_irfath_*
Posted 05 March 2007 - 12:11 AM
Guest_irfath_*
|
|
|
#2
Posted 10 March 2007 - 08:05 AM
Why in the world are you using files as temporary storage? You can do all of that work in memory and avoid all kinds of complexity:
#include <stdio.h>
#include <stdlib.h>
#define lengthof(a) (sizeof (a) / sizeof (*a))
void insertion_sort ( int a[], int n )
{
int i;
for ( i = 1; i < n; i++ ) {
int j, save = a[i];
for ( j = i; j >= 1 && a[j - 1] > save; j-- ) {
a[j] = a[j - 1];
}
a[j] = save;
}
}
int main ( void )
{
int a[45];
int i;
// Read the numbers
for ( i = 0; i < lengthof ( a ); i++ ) {
if ( scanf ( "%d", &a[i] ) != 1 ) {
perror ( "Input error" );
return EXIT_FAILURE;
}
}
// Sort the numbers
insertion_sort ( a, lengthof ( a ) );
// Print the result
for ( i = 0; i < lengthof ( a ); i++ ) {
printf ( "%d ", a[i] );
}
printf ( "\n" );
return EXIT_SUCCESS;
}


Sign In
Create Account

Back to top










