Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C (not C++) program called ave3 that reads in the first number from a fi

ID: 3760300 • Letter: W

Question

Write a C (not C++) program called ave3 that reads in the first number from a file. This first int number indicates the number of subsequent floating point numbers that appear in the file. Read the floating point numbers into an array, and then calculate and report their average. Use dynamic memory allocation (do bot use a static/statically size array). Your programs should gracefully handle errors. They should not abort upon error; your programs should detect the error, report a message, and gracefully exit. Use the exit function similar to Java's System.exit function.

Explanation / Answer

#include <stdio.h>
#include<conio.h>
#include<errno.h>

extern int errno ;

void main()
{
   int numberArray[16];
   int i,n,s=0;
   int errnum;

   float av;
   FILE *myFile;
   myFile = fopen("somenumbers.txt", "r");

   //read file into array
   n=fscanf(myFile,"%d",&n);

   for (i = 0; i < n; i++)
   {
       fscanf(myFile, "%d", &numberArray[i]);
       s=s+numberArray[i];
   }
   av=s/n;

   printf("Average is %f",av);
   getch();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote