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

I am to extend the code by adding a function maximum that computes the maximum o

ID: 3729279 • Letter: I

Question

I am to extend the code by adding a function maximum that computes the maximum of the input array, a function minimum that computes the minimum of the input array, a function average that computes the average of the input array and a sum function that computes the sum of the input array. The function for the minimum should be located in file min.c (with the header file min.h); the function for the maximum should be located in file max.c (with the header file max.h); the function for the sum should be located in file sum.c (with the header file sum.h) and the function for the average should be located in file avg.c (with the header file avg.h). Have each printed in main. Also create a makefile for these functions.

//main.c

#include

#include "avg.h"

#include "sum.h"

#define N 10

int main() {

int i; float a[N]; srand(123);

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

a[i] = ((double) rand())/ RAND_MAX;

printf("%.2f ",a[i]);

}

printf(" "); printf("average: %.2f ",avg(a,N)); printf("sum: %.2f ",sum(a,N));

return 0;

}

Explanation / Answer

Given below are the files. compile using
gcc sum.c avg.c min.c max.c main.c

sum.h
------


#ifndef sum_h
#define sum_h

float sum(float a[], int n);
#endif /* sum_h */

sum.c
------


#include "sum.h"
float sum(float a[], int n)
{
int i;
float s = 0;
for(i = 0 ; i < n; i++)
s += a[i];
return s;
}


min.h
------


#ifndef min_h
#define min_h

float min(float a[], int n);

#endif /* min_h */

min.c
------

#include "min.h"
float min(float a[], int n)
{
int i;
float m = a[0];
for(i = 1 ; i < n; i++)
{
if(a[i] < m)
m = a[i];
}
return m;
}

max.h
------

#ifndef max_h
#define max_h

float max(float a[], int n);

#endif /* max_h */

max.c
------

#include "max.h"
float max(float a[], int n)
{
int i;
float m = a[0];
for(i = 1 ; i < n; i++)
{
if(a[i] > m)
m = a[i];
}
return m;
}

avg.h
------


#ifndef avg_h
#define avg_h

float avg(float a[], int n);

#endif /* avg_h */


avg.c
------


#include "avg.h"
float avg(float a[], int n)
{
int i;
float average = 0;
for(i = 0 ; i < n; i++)
{
average += a[i];
}

if(n != 0)
average /= n;

return average;
}


main.c
------
//main.c

#include "min.h"
#include "max.h"
#include "avg.h"
#include "sum.h"
#include <stdlib.h>
#include <stdio.h>

#define N 10


int main() {

int i; float a[N]; srand(123);

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

a[i] = ((double) rand())/ RAND_MAX;

printf("%.2f ",a[i]);

}
printf(" "); printf("minimum: %.2f ",min(a,N)); printf("maximum: %.2f ",max(a,N));
printf("average: %.2f ",avg(a,N)); printf("sum: %.2f ",sum(a,N));

return 0;

}


output
0.00 0.18 0.94 0.41 0.53 0.93 0.79 0.50 0.55 0.97

minimum: 0.00
maximum: 0.97
average: 0.58
sum: 5.80

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