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

Please help with C! and many thanks also the file is aboverage.txt I added the l

ID: 3803891 • Letter: P

Question

Please help with C! and many thanks

also the file is aboverage.txt I added the list in the bottom

(100 pts) Write a program to find the number of elements above the average value in a file. Load the file to an array, compute average using the array and find the number of elements above average using the array. Use dynamic memory allocation using calloc to allocate the array and if the allocated size is not enough, use realloc to double the size of the array. Start with an array of size 10. Make sure you free the memory you allocated before you end the program. You can implement the following functions in your program. double average (int *ptr, int size) int above average int ptr, int size, double average) Sample execution is given below. Print a message on the screen when you allocate, reallocate and free the memory as shown below. Use argc, argv and file operations for this recitation. fox01> recitation a.txt Allocated 10 integers Reallocated to 20 integers Reallocated to 40 integers Reallocated to 80 integers Reallocated to 160 integers 47 elements are above average of 477.455446 Dynamic array freed

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

#define SIZE 100

void readArray();
double findAverage(int [] , int);
int countAboveAverage(int [] , int , double);

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int getCount()
{
   /*function to count number of integers in the file*/
   std::fstream myfile("D:\data.txt", std::ios_base::in);

int a;
int count = 0;
while (myfile >> a)
{
printf("%d ", a);
count++;
}
return count;
}

int* readArray() {
  
   /*function to read the Integers from file into an array*/
FILE *myFile;
myFile = fopen("somenumbers.txt", "r");

//read file into array
  
int* numberArray = (int*) calloc(10, sizeof(int));
/*Allocated 10 Integer*/
printf("Allocated 10 Integers);
  
int limiter = 10;
int count = getCount();
  
while (limiter > count)
{
   limiter = 2*limiter;
   numberArray = realloc(numberArray, limiter);
   /*reallocating subsequent integers*/
   printf("reallocated %d Integers",limiter);
   }
  
int i;

for (i = 0; i < getCount(); i++)
{
fscanf(myFile, "%d", &numberArray[i]);
}
  
return numberArray;
}

double findAverage(int arr[], int n) {
  
   /*function to find average*/
int sum = 0;
int i;
for(i=0; i<n; i++)
sum+=arr[i];
return (double)sum / n;
}

int countAboveAverage(int arr[], int n, double average) {
  
   /*function to find count above average*/
int count=0;
int i;

for(i=0; i<n; i++)
count = arr[i] > average ? count+1: count;
return count;
}


int main(int argc, char* argv []) {
//declare an array of size 100
int arr[SIZE];
int n;

printf("Enter a value for n: ");
scanf("%d", &n);

if(n==0) {
fprintf(stderr, "Your size of n is too big. Run the program again. ");
exit(1);
}


int* arr = readArray();
//this is a function to find the average
double average = findAverage(arr, n);
//this is a function that will count the number above average
int count = countAboveAverage(arr, n, average);

printf("The number of elements above the average is %d:");

free(arr);

return EXIT_SUCCESS;
}

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