Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-05-2007, 03:11 AM
irfath irfath is offline
Newbie
 
Join Date: Mar 2007
Posts: 1
Rep Power: 0
irfath is on a distinguished road
Default how do i modify this program to

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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 03-10-2007, 11:05 AM
Code Goddess's Avatar   
Code Goddess Code Goddess is offline
Newbie
 
Join Date: Mar 2007
Age: 30
Posts: 9
Rep Power: 0
Code Goddess is on a distinguished road
Default

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:
Code:
#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;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Best program for SQL database manipulation Rhadamanthys Database & Database Programming 3 07-02-2007 03:32 PM
How do I Program another Program? ! bosco General Programming 1 06-15-2007 12:15 PM
Need help w/ word count program (ASAP) siren C and C++ 1 04-23-2007 09:14 AM
How to modify a program written in .NET 2.0? jackyjack C# Programming 7 03-27-2007 01:26 PM


All times are GMT -5. The time now is 02:21 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads