OK - I've managed to resolve this issue, only to find another later down the line! When working out the minimum value stored within the two arrays I get an unexpected output for the first column of '0.000000' yet the code works for the second column. Here is my current code:
Code:
/*
* File: newmain.c
* Author: Paul R. Smith
*
* Created on 09 February 2010, 19:37
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main() {
FILE *file;
float col1[100000];
float col2[100000];
/* make sure it is large enough to hold all the data! */
int arr_ele, j, k, l, m, n, o, gotNum;
float sum_col1, sum_col2;
float average_col1, average_col2;
float max_col1, max_col2, min_col1, min_col2;
if ((file = fopen("/home/paul/assessment2_data.txt", "rt")) == NULL) {
printf("\nError opening file\n");
return 1;
}
arr_ele = 0;
do {
gotNum = 0;
gotNum = fscanf(file, "%f%f", &col1[arr_ele], &col2[arr_ele]);
if (gotNum > 1)
printf("\n%f %f", col1[arr_ele], col2[arr_ele]);
arr_ele++;
} while (gotNum > 1);
printf("\nAmount of numbers read: %d\n\n", arr_ele);
printf("The numbers are:\n");
sum_col1 = 0;
sum_col2 = 0;
for (k = 0; k < arr_ele; k++) {
sum_col1 += col1[k];
}
for (k = 0; k < arr_ele; k++) {
sum_col2 += col2[k];
}
printf("The sum of Column 1 is: %f\n", sum_col1);
printf("The sum of Column 2 is: %f\n", sum_col2);
average_col1 = 0;
average_col2 = 0;
average_col1 = sum_col1 / arr_ele;
average_col2 = sum_col2 / arr_ele;
printf("The average of Column 1 is: %f\n", average_col1);
printf("The average of Column 2 is: %f\n", average_col2);
for (m = 0; m < arr_ele; m++) {
if (col1[m] > max_col1) {
max_col1 = col1[m];
} else if (col1[m] < min_col1) {
min_col1 = col1[m];
}
}
for (m = 0; m < arr_ele; m++) {
if (col2[m] > max_col2) {
max_col2 = col2[m];
} else if (col2[m] < min_col2) {
min_col2 = col2[m];
}
}
printf("The maximum value in Column 1 is: %f\n", max_col1);
printf("The maximum value in Column 2 is: %f\n", max_col2);
printf("The minimum value in Column 1 is: %f\n", min_col1);
printf("The minimum value in Column 2 is: %f\n", min_col2);
fclose(file);
return 0;
}
The input text file remains the same as per my earlier post. I simply can't fathom out for why, any help greatly appreciated!