I have a 2D array with random generated values and some defines -1 values. I need to find the maximum and average values of the array. Only for the values that are >-1.
I created a for loop function for this, but i'm lost in calling it in main(), i don't know if i should set parameters for the maxavg().
Since i'm a beginner i'm really lost trying to fix this, any help?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
int GetRand(int min, int max);
int maxavg();
int main ()
{
int a[21][21], i , j, average, maximum, ret;
for (i = 0; i < 21; i++)
{
for ( j = 0; j < 21; j++)
{
a[i][j] = GetRand(0, 100);
printf("%3d" , a[i][j]);
}
a[2][15] = -1;
a[10][6] = -1;
a[13][5] = -1;
a[15][17] = -1;
a[17][17] = -1;
a[19][6] = -1;
printf("\n");
}
average = ;
maximum = ;
printf("average = %d \n maximum = %d", average, maximum);
return 0;
}
// random seed
int GetRand(int min, int max);
int get ()
{
int i, r;
for (i = 0; i < 21; i++)
{
r = GetRand(0, 100);
printf("Your number is %d \n", r);
}
return(0);
}
int GetRand(int min, int max)
{
static int Init = 0;
int rc;
if (Init == 0)
{
srand(time(NULL));
Init = 1;
}
rc = (rand() % (max - min +1) +min);
return (rc);
}
// max and average
int maxavg();
{
int max=INT_MIN, sum=0, count=0, avg, n, m, current;
current = a[i][j];
avg = sum/count;
for(n = 0; n < 21; n++){
for(m =0; m < 21; m++){
if(current > -1){
sum = sum + current;
count = count + 1;
if(current > max){
max = current;
}
}
}
}
return(0);
}