#include <stdio.h> 
#define SIZE 100 
#include <iostream.h>

// Global variables: None 
// Prototypes: 
FILE * open_file(char *); 
int read_data(FILE *, int []); 
int findmax(int [], int ); 
void swap(int *, int *); 
void sort_table(int [], int ); 
void write_data(FILE* , int [], int ); 

/******************************************************************/ 
// Function main: handles input, sort, output and possible errors 
int main() 
{int table[SIZE];  // Array is declared here, must have fixed size 
int count; 
FILE* fpointer; 

printf("Input file: "); 
fpointer=open_file("r");  // Open input file for read 
count=read_data(fpointer, table);  // Read data into array 
fclose(fpointer);         // Close file 
  // Error handling: exit program 
 if(count== -1) printf("Read error\n");
 else{                   // If no error found: 
       sort_table(table,count);       // Sort the table 
       printf("Output file: "); 
       fpointer=open_file("w");       // Open output file for write. 
       write_data(fpointer, table, count);  // Output sorted sequence of numbers 
       fclose(fpointer);            // Close the file 
 }   
cout<<table[1];
	cin.ignore();
 
return 0;                     // Exit the program 
} 
/******************************************************************/ 

//  Function open_file: opens a data-file, handles error. 
//  Parameter: file access mode (r, w or a) 
//  Returns: filepointer 
FILE * open_file(char *mode) 
{ char fname[50]; 
  FILE *data; 
  do{printf("Enter filename: "); // Asks for filename 
   scanf("%s",fname); 
   if((data=fopen(fname,mode))==NULL) // If file is not found.. 
     printf("File not found. Try again.\n"); // get correct name 
} while(data==NULL); 
  return data;           // If file is open return filepointer 
} 
/******************************************************************/ 

// Function read_data: reads numbers from file 
// Parameters: filepointer, float type array 
// Returns: Number of elements read into array 
// File format: number of elements (int) 
//              followed by numbers to be stored (float) 
int read_data(FILE *fp, int numbers[]) 
{int count=100, k; 
 
	if(fscanf(fp,"%d",&count)!=1) return -1; // Error-code: read failed 
	 if(count>SIZE) return -2;          // Error-code: Array too small 
	 

 for(k=0; k<count;k++) 
  if(fscanf(fp,"%d",&numbers[k])!=1) return 1;  // Error-code: read failed 
 return count;          // Return: number of elements stored 
} 
/******************************************************************/ 

// Function findmax: Finds location (subscript) of largest element in array 
// Parameters: float type array, number of elements in array 
// Returns: subscript of largest element found. 
int findmax(int  number[], int size) 
{int max; int subsc, k; 
for(k=1, max=number[0], subsc=0; k<size; k++) 
  if(max<number[k]) {max=number[k]; subsc=k;} 
return subsc; 
} 
/******************************************************************/ 

// Function swap: interchanges values of two float variables 
// Parameters: pointers to two variables 
// Returns: nothing 
void swap(int *a, int *b) 
{int temp; 
temp= *a; 
*a= *b; 
*b=temp; 
} 
/******************************************************************/ 

// Function sort_table: sorts float numbers into increasing order 
// Algorithm used: Selection sort (modified) 
// Parameters: float type array, number of elements in array 
// Returns: nothing 
void sort_table(int number[], int count) 
{int size, maxsub; 
for(size=count; size>1; size--) // Find largest number, move it to end of array 
 { maxsub=findmax(number,size); // Reduce size of array by one. 
   if (maxsub!= size-1) swap(&number[size-1],&number[maxsub]); 
} 
 } 
/******************************************************************/ 

// Function write_data:  outputs sequence of numbers from array to file 
// Parameters: filepointer, float type array, number of elements in array 
// Returns: nothing 
void write_data(FILE* fp, int number[], int size) 
{int k; 
for(k=0; k<size; k++) 
fprintf(fp,"%d\n",number[k]); 
} 