I'm supposed to write a program in C that stores data in an array and finds the sum/average, max and min. Each of the functions are supposed to be a separate file and the array global but I'm not supposed to use the array as an argument.
I have 3 functions written but I'm having trouble with the linking part (maybe I don't know if that's what its called) When I gcc main.c I get errors saying undefined reference to each function and my array is undeclared in the other functions. Is there a way to do this without using #include .h or .c? I was told to use extern but I'm confused on how that works.
Can someone explain what I'm missing here?
Thanks
Here's what I have
average.c
Code:
#include <stdio.h>
void findAverage(int n)
{
int i=0;
int sum=0;
for(i=0; i<n ; i++)
{
sum = numbers[i] + sum;
}
printf("%s%d\n","The Sum is: ", sum);
double average = sum/n;
printf("%s%d\n", "The Average is: ", average);
}
max.c
Code:
#include <stdio.h>
void findMax(int n) //n = size
{
int i;
int max = -2147483647;
for(i = 0; i<n; i++)
{
if(numbers[i]>max){
max = numbers[i];
}
else{
max = max;
}
}
printf("%s%d\n", "The Maximum Value is: ", max);
}
min.c
Code:
#include <stdio.h>
void findMin(int n) //n = size
{
int i;
int min = 2147483647;
for(i = 0; i<n; i++)
{
if(numbers[i]<min){
min = numbers[i];
}
else{
min = min;
}
}
printf("%s%d\n", "The Minimum Value is: ", min);
}
main.c
Code:
#include <stdio.h>//#include "average.c"
//#include "min.c"
//#include "max.c"
int numbers[5] ={45, 53, 75, 36, 11};
extern numbers[];
int main()
{
findAverage(5);
findMin(5);
findMax(5);
return 0;
}
~