Start New Discussion within our Software Development Community

I created a 2D array and filled it with random values from 0 to 100 and now i'm trying to find the average and maximum number of all the value inside it. But i'm having serious problems understanding the problem with my functions and how to connect them in the main function (i'm a total C beginner).

Please help me understand the problem with my average and find maximum functions. (the average functions its telling me that expected a declaration before i, which is "float sum=0.0;" but i haven't been able to fix that yet.)

Any recommendations? are my functions going in the right directions? something else i should try?

thanks!

CODE:

#include<stdio.h>
#include <stdlib.h>
#include <time.h> 


int main()
{
    int a[21][21], i , j;

    for (i = 0; i < 21; i++)
    {
        for ( j = 0; j < 21; j++)
        {
            a[i][j] = GetRand(0, 100);
            a[7][15] = -1;
            a[10][6] = -1;
            a[13][5] = -1;
            a[15][17] = -1;
            a[17][17] = -1;
            a[19][6] = -1;
            printf("%3d" , a[i][j]);
        }
        printf("\n");
    }
    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);
}



// avg

int avg()

float sum=0.0;

for(i = 0; i <= 21; i = i + 1) {


        for(j = 0; j <= 21; j = j + 1){


             sum = sum + a[21][21];
         }

    printf("The the average number is %.2f\n", sum/21);
}


// maximum

 int *pv = &a[0][0];
    max = min = 0;
    for (i = 1; i < i*j; ++i){
        if (pv[i] > pv[max])
            max =i;
        if (pv[i] < pv[min])
            min = i;
    }
    printf("The max value is %d in row %d, col %d\n", pv[max], max/j, max%j);


    return 0;
}

Is line 62 to 76 meant to be a function defintion? There's no opening {

What's all that from line 81 supposed to be? Is that meant to be a function definition?

This article has been dead for over six months. Start a new discussion instead.