-1
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <stdbool.h>
    #include <conio.h>
    
    
    
    
    void fillPin2D(int n ,int my2DArray[][n]);
    void showPin2D(int n ,int my2DArray[][n]);
    float findMeanLine(int n ,int my2DArray[][n]);
    float findMeanCol(int n ,int my2DArray[][n]);
    void showPinDouble1D(int n,float avgLine[n]);
    int findDSum(int n ,int my2DArray[][n]);
    
    
    
    
    int main(int argc,char *argv[])
    {
        srand( time(NULL) );
        int n;
        printf ("give a number higher than five (5)\n\n");
        scanf ("%d",&n);
        while (n <= 5)
        {
            printf ("give a number higher than five (5)\n\n");
            scanf ("%d",&n);
        }
        printf("\n");
        int my2DArray[n][n];
        float avgLine[n],avgCol[n];
        

// filling array /////

        fillPin2D (n, my2DArray);
        

//showing array /////

        showPin2D(n, my2DArray);
        printf("\n\n");
        

// average per line /////

        avgLine[n] = findMeanLine(n, my2DArray);
        

//show average per line ///// showPinDouble1D(n,avgLine);

//average per Col /////

        avgCol[n] = findMeanCol(n ,my2DArray);
        

//Show average per Col /////

        showPinDouble1D(n,avgCol);
        

//add 2 diagonals /////

        int sumD;
        sumD = findDSum(n ,my2DArray);
        printf ("sumD = %d",sumD);
        
        return 0;   
    }
    
    void fillPin2D(int n ,int my2DArray[][n])
    {
        int row,col;
        for (row=0 ; row <n ; row++)
            for (col=0 ; col<n ; col++)
                my2DArray[row][col] = rand()%10 + 1;
    }
    void showPin2D(int n ,int my2DArray[][n])
    {
        int row,col;
        for (row=0 ; row <n ; row++)
        {
            for (col=0 ; col<n ; col++)
            {
                printf ("%d  ",my2DArray[row][col]);
            }
            printf ("\n");
        }
    }
    float findMeanLine(int n ,int my2DArray[][n])
    {
        int sumRow=0;
        int row,col;
        float avgLine[n];
        for (row=0; row <n ; row++)
        {
            sumRow=0;
            for (col=0 ; col < n ; col++)
            {
                printf("%lu\n", sizeof(my2DArray));
                printf("%d\n",my2DArray[row,col]);    
//checking value of my2DArray[row,col]

                sumRow+=my2DArray[row,col]; 
//[Warning] assignment makes integer from pointer without a cast      
            }
            avgLine[row]=(float)sumRow/n;
        }
        return avgLine[n];
    }
    void showPinDouble1D(int n,float avgLine[])
    {
        printf("avg = ");
        int a;
        for (a=0 ; a < n ; a++)
        {
            printf(" %f ",avgLine[a]);
        }
        printf("\n");
    }
    float findMeanCol(int n ,int my2DArray[][n])
    {
        int row,col;
        float avgCol[n];
        for (col=0 ; col < n ; col++)
        {
            int sumCol=0;
            for (row=0; row < n ; row++)
            {
                printf("%d\n",my2DArray[row,col]);

//checking value of my2DArray[row,col] sumCol+=my2DArray[row,col];

//[Warning] assignment makes integer from pointer without a cast
}

            avgCol[col]=(float)sumCol/n;
        }
        return avgCol[n];
    }
    int findDSum(int n ,int my2DArray[][n])
    {
        int row,col,sumD=0;
        double avgCol[n];
        for (col=0 ; col < n ; col++)
        {
            int sumCol=0;
            for (row=0; row < n ; row++)
            {
                if (row==col)
                {
                    sumD+=my2DArray[row][col];
                }
                else if (n-row-1 == col)
                {
                    sumD+=my2DArray[row][col];
                }
            }
        }
        return sumD;
    }
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    Please read about how to create an MCVE ([Minimal, Complete, Verifiable Example](https://stackoverflow.com/help/mcve) — or MRE or whatever name SO now uses) or an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)) — the same idea by a different name. Note the "minimal" and "short" parts of the descriptions — you have provided far more code than is necessary to reproduce the problem. And your interpolated comments make it hard to see where the problem really is. – Jonathan Leffler Oct 22 '21 at 18:45
  • 1
    First: Read, understand and act on the comment made by @JonathanLeffler. But, looking through your code quickly, I see you sometimes use `my2DArray[row][col]` and other places have `my2DArray[row,col]`. Only the former is correct syntax. – Adrian Mole Oct 22 '21 at 18:52

1 Answers1

3

The both functions findMeanLine and findMeanCol are invalid.

It seems you are trying to return an array from the functions but the return type of the functions is float as for example

float findMeanLine(int n ,int my2DArray[][n]);

Also, you may not return a local array with automatic storage duration from a function because it will not be alive after exiting the function.

This statement

printf("%lu\n", sizeof(my2DArray));

does not make great sense because it always outputs the size of a pointer.

In this statement

printf("%d\n", my2DArray[row,col]);

In the expression my2DArray[row,col] there is used the comma operator in brackets. It seems you mean my2DArray[row][col].

In this return statement

return avgLine[n];

you are trying to return a non-existent element of the array with the index n. The valid range of indices is [0, n ).

You should redeclare the functions like for example

void findMeanLine(int n, int my2DArray[][n], float avgLine[]);

where the third parameter will specify an array with n elements declared in the caller of the functions.

And the functions will be called like

findMeanLine(n, my2DArray, avgLine);

or

findMeanCol(n, my2DArray, avgCol);

P.S.

Looking at your code, I can presume that you think that you may assign one array to another like

avgCol[n] = findMeanCol(n, my2DArray);

However, arrays do not have the assignment operator. And moreover the expression avgCol[n] is a non-existent element of the array avgCol.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335