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: 3805443 • Letter: P

Question

Please help with C! and many thanks

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

included the file name in the program if need it!! and dont copy and paste other solutions please

(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

Please refer below code

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

double find_average(int *ptr, int size)
{
int i;
int sum = 0;
for(i = 0; i < size; i++)
{
sum += ptr[i];
}
return sum/size;
}
int main()
{
FILE *fp;
int *ptr,*ptr_new;
double average;
int number,arr_size,step=0,i=0,count=0;
fp = fopen("aboverage.txt","r"); //reading file in C

ptr = (int *)calloc(10, sizeof(int));
printf("Allocated 10 integers ");

//reading numbers from file
while(fscanf(fp, "%d ", &number) > 0)
{

if(i >= ((1 << step)* 10))
{
step++;
ptr_new = (int *)realloc(ptr, (sizeof(int) * 10 * (1 << step)));
ptr = ptr_new;
printf("Reallocated to %d integers ", (10 * (1 << step)));

}
ptr[i] = number;
i++;
}
arr_size = i;
//calculating average
average = find_average(ptr,i);

//finding numbers above average
for(i = 0; i < arr_size; i++)
{
if(ptr[i] > average)
count++;
}
printf("%d elements are above average of %f ", count,average);
free(ptr);
free(ptr_new);
printf("Dynamic array freed ");
return 0;

}

Please refer below output

Allocated 10 integers
Reallocated to 20 integers
Reallocated to 40 integers
Reallocated to 80 integers
21 elements are above average of 444.000000
Dynamic array freed

Process returned 0 (0x0) execution time : 0.030 s
Press any key to continue.

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