Thread: Help! 2D arrays, maximum and average. Problem calling the function. (in C)

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    2

    Help! 2D arrays, maximum and average. Problem calling the function. (in C)

    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);
        
    }

  2. #2
    Registered User
    Join Date
    Sep 2015
    Posts
    19
    ok so the first thing you need to realize is that you can't use a variable or any data type (including your array) outside the scope it's declared in.
    the scope is those curly braces every function has:
    Code:
    function1(){
        int a = 1;
    }
    
    function2(){
        int b = 2;
        b += a ;   //this won't work
    }
    What you need to do is this:

    Code:
    int function1(){ // note the function is type int because we are returning a which is also an int
        int a = 1;
        return a;
    }
    
    void function2(){
        int b = 2;
        b += function1(); 
        
    }
    honestly I'm not a very good teacher so it's best you watch these series the guy explains things very clearly im on vid 45
    https://www.youtube.com/watch?v=sFZD...4AMPJq&index=3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with function to find the maximum of an array
    By Nhân Trần in forum C Programming
    Replies: 8
    Last Post: 07-14-2015, 11:31 PM
  2. Average and maximum character widths for logical fonts
    By megafiddle in forum Windows Programming
    Replies: 12
    Last Post: 11-10-2014, 09:47 AM
  3. how can i make average/maximum/minimum score to an output file?
    By catastrophe in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2013, 11:43 PM
  4. Calling multiple arrays to the same function
    By tomeatworld in forum C Programming
    Replies: 2
    Last Post: 11-21-2010, 01:42 PM
  5. Problem calling a function
    By JOCAAN in forum C Programming
    Replies: 4
    Last Post: 11-01-2008, 12:17 PM

Tags for this Thread