here are the numbers for the input file:
-0.043200 -0.003471 0.000000
-0.040326 -0.004851 -0.000737
-0.018204 -0.004246 -0.001530
0.022249 0.008891 0.004870
0.074892 0.044237 0.032171
0.129600 0.100233 0.089016
0.174747 0.160100 0.161792
0.200242 9.199106 0.214417
0.174747 0.160100 0.161792
#include <stdio.h>
main()
{
/* pointer to input and output file */
FILE *inp,
*outp;
/* declare variables */
double sum = 0;
double average = 0;
double a1, a2,a3;
double input_status;
double c1, c2, c3;
double sum_c1, sum_c2, sum_c3;
int i;
int count_lines;
/* set the increment starting at zero */
i = 0;
sum_c1 =0;
sum_c2 =0;
sum_c3 =0;
/* open the input and output files */
inp = fopen("number1.dat", "r");
outp = fopen("number1.out", "w");
/* displaying data */
fprintf(outp, " #1 #2 #3 Sum Average\n");
fprintf(outp, " _________ __________ _________ _________ _________\n");
/* getting numbers from input file */
input_status = fscanf(inp, "%lf %lf %lf", &a1, &a2, &a3);
/* first while loop to compute sum and average */
while (input_status != EOF, i<=0)
{
i += 1;
sum = a1 + a2 + a3;
average = sum / 3;
fprintf(outp, " %d %lf %lf %lf %lf %lf\n", i, a1, a2, a3, sum, average);
input_status = fscanf(inp, "%lf %lf %lf", &a1, &a2, &a3);
}
while (input_status != EOF, i<=2)
{
i += 1;
sum = a1 + a2 + a3;
average = sum / 3;
fprintf(outp, " %d %lf %lf %lf %lf %lf\n", i, a1, a2, a3, sum, average);
input_status = fscanf(inp, "%lf %lf %lf", &a1, &a2, &a3);
}
/* created second while loop so the columns would line up after numbers were positive */
while (input_status != EOF, i>=2 && i<=8)
{
i += 1;
sum = a1 + a2 + a3;
average = sum / 3;
fprintf(outp, " %d %lf %lf %lf %lf %lf\n", i, a1, a2, a3, sum, average);
input_status = fscanf(inp, "%lf %lf %lf", &a1, &a2, &a3);
}
fprintf(outp, " _________ __________ _________ _________ _________\n\n");
while (input_status != EOF && i>= 0 && i <= 8)
{
input_status = fscanf(inp, "%lf %lf %lf", &c1, &c2, &c3);
sum_c1 = sum_c1 + c1;
sum_c2 = sum_c2 + c2;
sum_c3 = sum_c3 + c3;
i+=1;
}
fprintf(outp, "Sum %lf %lf %lf\n", &sum_c1, &sum_c2, &sum_c3);
fprintf(outp, " _________ __________ _________ _________ _________\n\n");
fprintf(outp, "Avg.\n");
fprintf(outp, "\n");
/* close the input and output files */
fclose(inp);
fclose(outp);
system("pause");
}
This post has been edited by js500: 05 November 2009 - 10:01 PM

New Topic/Question
Reply




MultiQuote




|