A
Army1987
Absolutely not.I've got one more error that I can't seem to get worked out. I have a
segmentation fault and upon playing with my code, I think that it
crashes when I try to deallocate the "date" array.
Here's the most recent version of my code. Everything else seems to
work properly.
I'd use size_t (if for some reason I had to avoid the preprocessor)...#include <stdio.h>
#include <stdlib.h>
#include <math.h> Why?
int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5;
Why the hell do you need it?const int errorcode = -1;
These casts don't buy anything.float sum = 0;
int count = 0;
int condition = 1;
float* precipvals = (float*)NULL;
float* precip3hrs = (float*)NULL;
long* date = (long*)NULL;
if (argv < 3) {FILE *fpin;
FILE *fpout;
fprintf(stderr, "Usage:\t%s infile outfile\n", argv[0]);
return EXIT_FAILURE;
}
/* or fpin = stdin, fpout = stdout, or ask the user what to do...*/
if (fpin == NULL) { /* handle... */ }fpin = fopen(argv[1],"r");
same as above...fpout = fopen(argv[2],"w");
Why the hell sizeof(int)? And why do you need the cast?/* Array Allocation */
precipvals = (float*)malloc(sizeof(int) * arraylength);
while (condition < 2)precip3hrs = (float*)malloc(sizeof(int) * arraylength);
date = (long*)malloc(sizeof(int) * arraylength);
/* Calculation Section */
while(condition != EOF) {
It is well possible that the file is ill-formed and you'll be able
to read date[count] but not precipvals[count]...
if (count >= arraylength) break;condition = fscanf(fpin,"%ld,%f", &date[count],
&precipvals[count]);
if(count == 0) {
precip3hrs[count] = precipvals[count];
}
if(count == 1) {
precip3hrs[count] = precipvals[count] + precip3hrs[count-1];
}
if(count > 1) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}
count++;
Couldn't you bring these two out of the loop and make the loop}
/* Output section */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
count from 2?
if (fclose(fpout) != 0)if(count > 1) {
printf("One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
/* Write to file */
for(count = 0; count < arraylength; count++) {
if(count == 0) {
fprintf(fpout, "One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count > 1) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
fclose(fpin);
fclose(fpout);
perror("Something went wrong");
Why didn't you use free((void *)precipvals); this time?printf("Testing\n");
/* Deallocation */
free(precipvals);