Write a C program that will ask the user for a file name Con

Write a C program that will ask the user for a file name. Contained in this file is a series of integers separated by white space. Read in the numbers, displaying them on the screen, adding them up and then calculating their average.

Please remember that you will have to create the input file and it is easiest to read it if you use its entire path name, i.e., \"a:\\\\input.txt\".

Solution

#include <stdio.h>

int main()
{
   int tmp,sum,count;
   sum=0;
   count=0;
   FILE *fp = fopen(\"input.txt\",\"r\");
   while(fscanf(fp,\"%d\",&tmp)!=EOF)
   {
       printf(\"%d \",tmp);
       sum = sum+tmp;
       count++;
   }
   printf(\"\ The sum of all numbers is %d\ \",sum);
   double average = (sum*1.0)/count;
   printf(\"The avrage of all numbers is %lf\ \",average);
}

Write a C program that will ask the user for a file name. Contained in this file is a series of integers separated by white space. Read in the numbers, displayi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site