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

Obiective: Write a program that will manipulate a one dimensional array several

ID: 3704712 • Letter: O

Question

Obiective: Write a program that will manipulate a one dimensional array several different ways Reference:Chapter 7 of the textbook and lecture notes. Problem: Write a program that will read data into an array and keep track of how many integer values are read in. There is a maximum of 100 values in this type of file. Your program is going to use functions from lecture and modify them to accomplish what needs to be done. 1. 2. Read in the data Echo out how many values have been read in. Locate the average of all values (function) . Locate the median value (function) 5. Output how many values are above the average (function) 6, Output how many values are above the median (SAME function you use in #5H) Upload your source code and a copy of the output to Canvas Hints: . Your program will have a main function and 4 other functions All output statements will be located in the main function. All variables and prototypes are local and NOT global. Should have at least one local constant Graded On . Comments in your code . Neat and readable code (including indenting) Good programming style .Logical problem solving Variable names are unique & meaningful Function headers (5 parts including source) Functions Comments INSIDE the functions-does it explain the logic . Efficient array manipulation . Good use of data types .Output-clearly labeled & formatted

Explanation / Answer

#include<stdio.h>

#include<conio.h>

//Method to read the values from the file

int readFile(char *fileName, int array[]) {

FILE *fp = fopen(fileName, "r");

int i = 0;

int n = 0;   

while(!feof(fp)) {

fscanf(fp, "%d", &array[i]);

n++;

i++;

}

  

fclose(fp);

return n;

}

//Metod to find the avrage of the array

float average(int array[], int n) {

float sum = 0.0;

float avg = 0.0;

int i;

for(i = 0; i < n; ++i)

sum += array[i];

return sum / n;

}

//Metod to find the median of the array

int median(int a[],int n) {

int i,j,temp;

for(i=0;i<n-1;i++) {

for(j=0;j<n-i-1;j++) {

if(a[j]>a[j+1])

swap(&a[j],&a[j+1]);

}

}

n = (n+1) / 2 - 1; // -1 as array indexing in C starts from 0

return a[n];

}

void swap(int *p,int *q) {

int t;

t=*p;

*p=*q;

*q=t;

}

//Metod to find the number of elements greater than the average

int greaterThanAverage(int array[], int n, float avg) {

int i, c = 0;

for(i = 0; i < n; ++i) {

if(array[i]>avg)

c++;

}

return c;

}

//Metod to find the number of elements greater than the median

int greaterThanMedian(int array[], int n, int med) {

int i, c = 0;

for(i = 0; i < n; ++i) {

if(array[i]>med)

c++;

}

return c;

}

void main() {

int n, i;

float num[100];

  

int array[100] = {0};

char fileName[50];

printf("Enter the file name: ");

scanf("%s", &fileName);

  

n = readFile(fileName,array);

  

printf("Total values read from the file: %d", n);

float avg = average(array,n);

printf("Average of the values is: %f ", avg);

  

int med = median(array,n);

printf("Median of the values is: %d ", med);

int c = greaterThanAverage(array,n,avg);

printf("Number of values greater than the average: %d ", c);

c = greaterThanMedian(array,n,med);

printf("Number of values greater than the median: %d ", c);

  

getch();

}