The following should help - it fixes the problem with the loop index (go from 0 to 9, not 1 to 10), and generates random numbers between 10 and 20. There is a small error - using the modulus operator you get very slightly uneven distribution. If you care enough you can create a function that eliminates random numbers above, say, 32758. Then it will be "completely fair".
EDIT I have modified this program so it is split into a number of functions - one that generates the array, another that prints the array, and a third that takes the average of the array. It also includes a custom function that makes a "fair" random distribution. I think your professor will think you had help (and if he googles any phrase in the code he will land on this page). So use this as inspiration, then write your own code. Learning from examples is good; plagiarism is not good.
EDIT Modified the fillArray
function. Now uses the space allocated in the main program, rather than creating space in the function itself. It a little bit simpler.
#include <stdio.h>
#include <stdlib.h>
// function declarations
int* fillArray(int* a, int n, int llim, int ulim);
float arrayAverage(int *a, int nElements);
void arrayPrint(int *a, int n);
int fairRand(int, int);
int main(int argc, char* argv[]) {
int m[10];
float av;
m = fillArray(m, 10, 10, 20); // note that 'm' is a pointer (int*) to the array m[]
av = arrayAverage(m, 10);
arrayPrint(m, 10);
printf("The average is %.2f\n", av);
return 0;
}
int* fillArray(int *a, int n, int llim, int ulim) {
// given an array a with size n
// fill each element in the array with a random element
// between llim and slim (inclusive)
int i;
for ( i = 0; i < n; i++ )
{
a[ i ] = fairRand(llim, ulim); // calling our user-defined function
}
return a;
}
float arrayAverage(int *a, int n) {
// returns the sum of values in a[n] divided by n
int j;
double s=0.0;
for (j = 0; j < n; j++ )
{
s += a[j];
}
return (float)(s / (double) n);
}
void arrayPrint(int *a, int n) {
// prints each element in the array m[n]
int j;
for (j = 0; j < n; j++ )
{
printf("Element[%d] = %d\n", j, a[j] );
}
}
int fairRand(int a, int b) {
// generates fair random number between a and b, inclusive
int diff = b - a + 1;
int r;
int largest = diff * (RAND_MAX / diff);
// this is the "magic" line: keep generating random numbers
// until the number generated fits in the interval that has equal number
// of each possible value after the modulo operation:
while( (r = rand()) >= largest) {
// keep going around...
} ;
// we now have a "fair" random number r
return a + r % diff;
}