| |
/* Cprogramming/ordering-commented.c
*
* Copyright (C) 2012 Prita Pant and M P Gururajan
*
* This program is a free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY of FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*
*
*/
/** Include the standard libraries and
library for string manipulations **/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/** The main function of a C program always
returns an integer; if the returned integer
is zero, the Operating system knows
that the program execution was successful.
In our case, the main program does not
take any input parameters; so, we write
void for the input parameters **/
int main(void){
FILE *fp; /** File pointer **/
int n=25; /** Number of lines of data **/
int i,j; /** Integer variables for looping **/
int AtomicNo[n];
/** Intger array to store the atomic numbers **/
double Density[n];
/** Double array to store the densities **/
char ChemSymbol[n][4];
/** Char array to store the chemical symbols **/
int TempAtomicNo;
/** Temporary integer variable **/
double TempDensity;
/** Temporary double variable **/
char TempChemSymbol[4];
/** Temporary char variable **/
/** Condition opening of files; if opening of files
is not successful for any reason, the code will inform
the user so **/
if( (fp=fopen("elements.dat","r")) == NULL){
printf("Unable to open elements.dat. \n");
printf("Exiting\n");
exit(0);
}
else{
fp = fopen("elements.dat","r");
}
/** Read the input data from the file "elements.dat"; note
that we use fgets for reading the string **/
for(i=0; i<n; ++i){
fscanf(fp,"%d",&AtomicNo[i]);
fgets(ChemSymbol[i],4,fp);
fscanf(fp,"%lf",&Density[i]);
}
/** Close the opened file **/
fclose(fp);
/** The implementation of bubble sort; note, how,
the indentation helps in improving the readability
of this block of code **/
for(i=1; i<n; i++){
for(j=n-1; j>=i; j--){
if(Density[j] > Density[j-1]){
TempDensity = Density[j];
Density[j] = Density[j-1];
Density[j-1] = TempDensity;
TempAtomicNo = AtomicNo[j];
AtomicNo[j] = AtomicNo[j-1];
AtomicNo[j-1] = TempAtomicNo;
strncpy(TempChemSymbol,ChemSymbol[j],4);
strncpy(ChemSymbol[j],ChemSymbol[j-1],4);
strncpy(ChemSymbol[j-1],TempChemSymbol,4);
}
}
}
/** Open an output file to write the data ordered according to density **/
if( (fp=fopen("DensityOrdered.dat","w")) == NULL){
printf("Unable to open DensityOrdered.dat. \n");
printf("Exiting\n");
exit(0);
}
else{
fp = fopen("DensityOrdered.dat","w");
}
/** Write the data **/
for(i=0; i<n; ++i){
fprintf(fp,"%d %lf %s\n",AtomicNo[i],Density[i],ChemSymbol[i]);
}
/** Close the file **/
fclose(fp);
/** Bubble sort for arranging alphabetically **/
for(i=0; i<n-1; i++){
for(j=i+1; j<n; j++){
if(strcmp(ChemSymbol[i],ChemSymbol[j]) > 0){
strcpy(TempChemSymbol,ChemSymbol[i]);
strcpy(ChemSymbol[i],ChemSymbol[j]);
strcpy(ChemSymbol[j],TempChemSymbol);
TempDensity = Density[j];
Density[j] = Density[j-1];
Density[j-1] = TempDensity;
TempAtomicNo = AtomicNo[j];
AtomicNo[j] = AtomicNo[j-1];
AtomicNo[j-1] = TempAtomicNo;
}
}
}
/** Open a file for writing the alphabetised list **/
if( (fp=fopen("Alphabetised.dat","w")) == NULL){
printf("Unable to open Alphabetised.dat. \n");
printf("Exiting\n");
exit(0);
}
else{
fp = fopen("Alphabetised.dat","w");
}
/** Write the data **/
for(i=0; i<n; ++i){
fprintf(fp,"%d %lf %s\n",AtomicNo[i],Density[i],ChemSymbol[i]);
}
/** Close the file **/
fclose(fp);
/** The job is done; return 0 to the operating system
signaling the successful completion of the program **/
return 0;
} |
|
|
|