Write a program to find the number of elements above the average value in a file
ID: 3802065 • Letter: W
Question
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 callow to allocate the array and if the allocated size is not enough, use reallot 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 (double *ptr, int size) int above average (double *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. fox01> recitation8 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 freedExplanation / Answer
Answer:
double average(double *ptr, int size)
{
double total;
int i;
while (i < size)
{
total+=ptr[i];
i++;
}
return (total / size);
}
int aboveaverage(double *ptr, int size, double average)
{
int incrementer=0, input;
for (int i = 0; i < size; i++)
{
input = (int)ptr[i];
if (input > average)
incrementer++;
}
return incrementer;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.