Question

This is a C programming question. We have a file containing floating point numbers (we don't...

This is a C programming question.

We have a file containing floating point numbers (we don't know how many). Write a complete C program that alternately averages only the floating point numbers (for example 3.50, 12.507, 123.60,5.98 would lead to the result (12.507+5.98)/2), ignore the whole numbers. Use file input, assuming the file is named "numbers.data". The program should be compatible with files that contain several numbers on separate lines.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

int main() {
    FILE *fp = fopen("numbers.data", "r");
    if (fp) {
        double total = 0, count = 0, n1, n2;
        while (fscanf(fp, "%lf", &n1) != EOF && fscanf(fp, "%lf", &n2) != EOF) {
            total += n2;
            ++count;
        }
        printf("Average of alternate numbers is %lf\n", total/count);
        fclose(fp);
    } else {
        printf("numbers.data does not exists\n");
    }
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
This is a C programming question. We have a file containing floating point numbers (we don't...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT