Answer to Question #274147 in C for Sparrow

Question #274147

Code a C program to read five integer values from data.txt to console; calculate and display the sum and average thereof. Use the following data to create your text file: 6,45,12,67,9.

1
Expert's answer
2021-12-01T10:36:06-0500
#include <stdio.h>
#define N 5

int main() {
    int x, sum=0, i;
    double avg;
    FILE *fin;


    fin = fopen("data.txt", "r");
    for (i=0; i<N; i++) {
        fscanf(fin, "%d,", &x);
        printf("%d ", x);
        sum += x;
    }
    fclose(fin);

    printf("\nSum is %d,", sum);
    avg = (double) sum / N;
    printf(" avrage is %.2lf\n", avg);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS