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

How to create multiple .c and .h files connected to a Makefile in bash The main.

ID: 3915593 • Letter: H

Question

How to create multiple .c and .h files connected to a Makefile in bash

The main.c is shown below:

//main.c

#include <stdio.h>

#include <stdlib.h>

#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;

}

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). After implementing these four functions, extend the code for the main function to also print out the maximum and minimum. The executable file should be named stats.

Example output: 0.21 0.69 0.78 0.40 0.53 0.10 0.64 0.35 0.72 0.03

average: 0.44

sum:4.44

max:0.78

min:0.03

The final code should thus be split into the following source and header files: main.c, avg.c, avg.h, sum.c, sum.h, min.c, min.h, max.c, max.h

Include a working makefile so that all I have to do is to run make and then run your code. Use target ‘all’ that makes the executable, stats.The program does not expect any input from the user.

Explanation / Answer

You need to simply create 4 files in the same folder because of #include "avg.h"

" " is simply look into current folder, else write < > if your file is not within the folder

CODE:

#include <stdio.h>
#include <stdlib.h>
#include "avg.h"
#include "sum.h"
#include "max.h"
#include "min.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 ) );
printf( "max: %.2f ", max( a, N ) );
printf( "min: %.2f ", min( a, N ) );
return 0;
}

avg.h

double avg(float a[],int N );

avg.c

#include "avg.h"
double avg(float a[],int N ){
double avg = 0.0;
for (int i = 0; i < N; ++i)
{
avg = avg + a[i];
}
return avg/N;
}?

sum.h

double sum(float a[],int N );?

sum.c

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

max.h

double max(float a[],int N );

max.c

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

min.h

double min(float a[],int N );?

min.c

#include "min.h"
double min(float a[],int N ){
double min = 2147483647.00;
for (int i = 0; i < N; ++i)
{
if(a[i] < min)
min=a[i];
}
return min;
}?

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