Given the following C program, write all of the functions used in the program so
ID: 3683372 • Letter: G
Question
Given the following C program, write all of the functions used in the program so it works correctly. A simple data file containing positive integers greater than 0 can be found at
http://liberty.egr.vcu.edu/~resler/lab9data.dat ; you may assume that the number of ntergers n in this file is such that 1<= n <= 10000, and that the lastvalue in the file is -1. Input the data fromm this file using the redirection on the command line.
#include <stdio.h>
#include <stdlib.h>
#include #include int inputData(int [])
int largest(int [], int);
int smallest(int [], int);
double average(int [], int);
int numEven(int [], int);
int numOdd(int [], int);
int main (void) {
int list[10000], size;
printf("total = %d ", size = inputData(list));
printf("last one: %d ", list[size-1]);
printf("largest = %d ", largest(list,size));
printf("smallest = %d ", smallest(list,size));
printf("average = %.3lf ", average(list,size));
printf("number even = %d ", numEven(list,size));
printf("number odd = %d ", numOdd(list,size));
}
Code needs to be in C program
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int inputData(int []);
int largest(int [], int);
int smallest(int [], int);
double average(int [], int);
int numEven(int [], int);
int numOdd(int [], int);
int main (void) {
int list[10000], size;
printf("total = %d ", size = inputData(list));
printf("last one: %d ", list[size-1]);
printf("largest = %d ", largest(list,size));
printf("smallest = %d ", smallest(list,size));
printf("average = %.3lf ", average(list,size));
printf("number even = %d ", numEven(list,size));
printf("number odd = %d ", numOdd(list,size));
return 0;
}
int inputData(int arr[]){
FILE *read;
read = fopen("lab9data.dat","r");
int num = -1;
fscanf(read, "%d", &num);
int size = 0;
while(num != -1){
arr[size] = num;
fscanf(read, "%d", &num);
size++;
}
return size;
}
int largest(int arr[], int size){
int max = arr[0];
int i;
for(i=1; i<size; i++){
if(arr[i] > max)
max = arr[i];
}
return max;
}
int smallest(int arr[], int size){
int min = arr[0];
int i;
for(i=1; i<size; i++){
if(arr[i] < min)
min = arr[i];
}
return min;
}
double average(int arr[], int size){
double sum = 0;
int i;
for(i=0; i<size; i++){
sum = sum +arr[i];
}
return sum/size;
}
int numEven(int arr[], int size){
int num = 0;
int i;
for(i=0; i<size; i++){
if(arr[i]%2 == 0)
num++;
}
return num;
}
int numOdd(int arr[], int size){
int num = 0;
int i;
for(i=0; i<size; i++){
if(arr[i]%2 != 0)
num++;
}
return num;
}
/*
Output:
total = 5203
last one: 107
largest = 1088
smallest = 1
average = 548.840
number even = 2595
number odd = 2608
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.