/* In this program i am trying to Read these info from a text file called unsorted saved in D
These info are
A201456 23.15
B209356 6.58
C201232 7.5
D201172 1.2
E201653 3.68
1)i want to sort them in ascending order of the real number example (23.15 ).
2)I want also to upgrade it to give the user a choice wether to sort according
to the code or the real number followed i must use merge sort algorithm
I did this but when i try to excute it closes the program nothing is done , i dont know
what is wrong Please could any one correct it if it is possible to do it that way
or write a better way if he or she could
Please i just beginner to C
Thanks alot brothers & sisters
*/
//start
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define N 5
/* The type representing a student's record */
typedef struct student {
char code[N];
int marks;
} Student;
//Functions prototypes
//To sort a content of a file in ascending order of real numbers
void sort(Student *arr,int low,int mid,int high);
//To Rewrite the file sorted
int WriteF(FILE *f,Student *strup);
//To check wether a file is open properlly
int openfile(FILE *f,int mode);
//To split array
void partition(Student *arr,int low,int high);
//int checkF(FILE *f);
//The main interface
int main(){
FILE *fp;
Student classA[N];
char temp[15];
int i;
if(openfile(fp,0)!=1)
printf("Unable to open file");
else {
//To Laod file to an array of structures
for(i=0;i<N&& (fgets(temp,13,fp))!=NULL;i++)
sscanf(temp,"%8s%f",classA[i].code,classA[i].marks);
fclose(fp);
}
/* To pass the address of the array of structures and sort it */
partition(classA,0,N-1);
/*Write sorted file */
if(openfile(fp,1)!=1)
printf("Unable to open file");
else
WriteF(fp,classA);
fclose(fp);
return 0;
} //End of main
//function to Write
int WriteF(FILE *f, Student *strup){
int i;
for(i=0;i<N;i++)
fprintf(f,"%s %f\n",strup[i].code,strup[i].marks);
fclose(f);
}
//function to sort the file
void sort(Student *arr,int low,int mid,int high)
{
int i,j,k,l;
Student b[20];
l=low;
i=low;
j=mid+1;
while((l<=mid)&&(j<=high))
{
if(arr[l].marks<=arr[j].marks)
{
b[i].marks=arr[l].marks;
strcpy(b[i].code,arr[l].code);
l++;
}
else
{
b[i].marks=arr[j].marks;
strcpy(b[i].code,arr[j].code);
j++;
}
i++;
}
if(l>mid)
{
for(k=j;k<=high;k++)
{
b[i].marks=arr[k].marks;
strcpy(b[i].code,arr[k].code);
i++;
}
}
else
{
for(k=l;k<=mid;k++)
{
b[i].marks=arr[k].marks;
strcpy(b[i].code,arr[k].code);
i++;
}
}
for(k=low;k<=high;k++)
{
arr[k].marks=b[k].marks;
strcpy(arr[k].code,b[k].code);
}
}
//For partition
void partition(Student *arr,int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
//Sort them
sort(arr,low,mid,high);
}
}
//To open file
int openfile(FILE *f,int mode)
{
//To make sure the file is open for reading
if(mode ==0){
if((f=fopen("D:\\unsortedfile.txt","r"))==NULL)
return 0;
else
return 1;
}
else if(mode==1){
if((f=fopen("D:\\unsortedfile.txt","w"))==NULL)
return 0;
else
return 1;
}
}
//End