Code
/*
Write a program with the following features:
(1) Has a function that reads in a vector stored as a text file, and returns
the allocated array. It should return a pointer to a double (the
dynamically allocated array), and take as arguments the file name to
read in from, and an integer pointer which will pass back the read-in
vector length. The file consists of a integer on the first line, which
indicates how long the array is, and each subsequent line has one floating
point value on it (as in earlier exercises).
(2) Has another function that will sort the input array into least-to-greatest
order.
(3) Has another function that writes the sorted array out to another file.
in the same format as the input file.
(4) Main should take the filename to read and to create as arguments,
and call the above functions appropriately.
* If two arguments (in addition to program name) aren't given, print
an informative error message and halt execution abnormally.
--> Be sure to close all files you open, and free all memory you allocate
--> You should check your work by using an example out-of-order file you
create, and cat/vi the resulting output file
--> Make sure you can change the filenames from the commandline!
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
/*
* This function reads a vector from a file with the first number being the
* length of the vector (N), and the remaining entries be N separate floating
* point values in the vector.
* RETURNS: pointer to allocated N-length array;
* ON EXIT: *N is set to length as read in from file
*/
double *readVecFromFile
(
char *file, /* filename to read from */
int *N /* ON OUTPUT: length of vec read in from file */
)
{
FILE *fp;
double *V;
int i;
fp = fopen(file, "r");
assert(fp != NULL);
assert(fscanf(fp, "%d", N) == 1); /* N is a ptr, so use N, not &N here */
assert(*N > 0);
V = malloc(*N * sizeof(double));
assert(V); /* same as assert(V != NULL) */
for (i=0; i < *N; i++)
assert(fscanf(fp, "%lg", &V[i]) == 1);
fclose(fp); /* close the file once array is read in */
return(V);
}
/*
* Writes the length and vector V to file file
*/
void writeVecToFile
(
char *file, /* filename to read from */
int N, /* length of vector V */
double *V /* vector to dump to file */
)
{
FILE *fpout;
int i;
fpout = fopen(file, "w");
assert(fpout);
fprintf(fpout, "%d\n", N);
for (i=0; i < N; i++)
fprintf(fpout, "%lg\n", V[i]);
fclose(fpout);
}
/*
* This function uses bubble sort to sort the array in least-to-greatest order
*/
void sortDoubles
(
int N, /* length of array V */
double *V /* Array to be sorted */
)
{
int i, j;
for (i=0; i < N-1; i++)
for (j=N-1; j > i; j--)
if (V[j] < V[j-1])
{
double tmp=V[j];
V[j] = V[j-1];
V[j-1] = tmp;
}
}
int main(int nargs, char *args[])
{
int N;
double *A;
if (nargs != 3)
{
fprintf(stderr, "USAGE: %s <input file> <output/sorted file>\n", args[0]);
return(1);
}
A = readVecFromFile(args[1], &N);
sortDoubles(N, A);
writeVecToFile(args[2], N, A);
free(A);
return(0);
}
This post was edited by eagl3s1ght on Nov 9 2013 04:47am