Created
November 22, 2020 16:43
-
-
Save Roytangrb/d86501a4e3a7b3a77494e267c0567635 to your computer and use it in GitHub Desktop.
File I/O
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Read int from file and calc avg | |
| * By RT | |
| * 23 Nov, 2020 | |
| */ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| int main(int argc, char *argv[]){ | |
| FILE *ifp, *ofp; | |
| ifp = fopen(argv[1], "r"); | |
| ofp = fopen(argv[2], "w"); | |
| int count; | |
| fscanf(ifp, "%d", &count); | |
| int data[count]; | |
| int sum = 0, max = 0; | |
| for (int i = 0; i < count; i ++) { | |
| int datum; | |
| fscanf(ifp, "%d", &datum); | |
| data[i] = datum; | |
| // set max value | |
| if (datum > max) max = datum; | |
| //calc sum | |
| sum += datum; | |
| } | |
| printf("Max value of data is %d\n", max); | |
| printf("Avg value of data is %lf\n", 1.0 * sum / count); | |
| fprintf(ofp, "Max value of data is %d\n", max); | |
| fprintf(ofp, "Avg value of data is %lf\n", 1.0 * sum / count); | |
| fclose(ifp); | |
| fclose(ofp); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment