-2

Our professor at school gave us a "simple" programm task in C.

We should create a 2D Array, fill it with random numbers and give it to another function. This function should give out the 2D-Array.

The problem is: How do i hand over the 2D Array?

Also i have a problem by calling the function. I Included the .h file, but it always give errors.

#include <stdio.h>
#include <stdlib.h>
#include "ShowMatrix.h"

#define W1 10
#define W2 10
int main(void) {
    int a,b;
    int array[W1][W2];
    for(a = 0; a < W1; ++a)
    {
        for(b=0; b<W2; ++b)
        {
            array[a][b] = rand() %10;
        }
    }
    ShowMatrix(array[][W2], W1, W2);

    return EXIT_SUCCESS;
}

The function which i want to call:

void ShowMatrix(int array[][10], int W1, int W2)
{
    int a,b;
    for(a = 0; a < W1; ++a)
    {
        for(b=0; b<W2; ++b)
        {
            printf("Inhalt von Array[%d][%d] ",a,b);
            printf("ist: %d \n", array[a][b]);
        }
    }
}

Header part of the func:

#ifndef SHOWMATRIX_H_
#define SHOWMATRIX_H_

void ShowMatrix(int array[][], int W1, int W2);

#endif /* SHOWMATRIX_H_ */
  • 1
    "Our professor", tell him/her for me that `array` in `void ShowMatrix(int array[][], int W1, int W2);` IS NOT AN ARRAY. Thanks. prototype is wrong and should be `void ShowMatrix(int (*not_an_array)[10], int W1, int W2);`, vote to close as simple typo – Stargateur Apr 25 '18 at 07:56
  • 2
    The calling code is wrong too. It's not clear if the error comes from the student or teacher. What is clear however, is that this is C90 and that the knowledge of C programming teachers around the world remains very poor. – Lundin Apr 25 '18 at 08:30
  • Also, your 2nd question is (A) one question too many and should be posted separately and (B) hopelessly vague; if you want help with an error, quote it; don't just vaguely gesture at it. – underscore_d Apr 25 '18 at 08:55

2 Answers2

1

The prototype containing int array[][] is an incomplete type. This will not compile. If you for some reason must have an incomplete type in the header (I have no idea why you would want this), then you'd have to declare the array as array[][*]. That will make the code compile, but it is nonsensical still.

The proper way to declare the function would otherwise be this:

void ShowMatrix(int W1, int W2, int array[W1][W2]);

You can then call the function as

ShowMatrix(W1, W2, array);

But this requires that you use a standard C compiler. Judging by your coding style, it seems that you are getting taught to use a 30 years old and obsolete version of C, so this might not work for you.

You should ask your teacher why they teach you programming knowledge from 1989 instead of 2018.

Lundin
  • 195,001
  • 40
  • 254
  • 396
-1

The problem is in following lines:

  • ShowMatrix(array[][W2], W1, W2); in main function. Change it to ShowMatrix(array, W1, W2); Reason Parameter is mismatched. In function definition you stated that first parameter is a 2D array but you are not passing a 2D array.
  • void ShowMatrix(int array[][10], int W1, int W2) in function defintion. Change it to void ShowMatrix(int array[][W2], int lenD1, int lenD2). Reason W1 and W2 are compile time constants. They can not hold value passed to function. These should be some variable names. In int array[][W2], W2 is used just to as good programming practice
  • You need to change the function deceleration accordingly i.e. change void ShowMatrix(int array[][], int W1, int W2); to void ShowMatrix(int array[][W2], int lenD1, int lenD2) in ShowMatrix.h file. Reason Function deceleration should match function definition and vice versa.

You can see the complete working code here.

Following is corrected code.(line #include "ShowMatrix.h" is commented becuase hosting environment don't support code spread across multiple files):

#include <stdio.h>
#include <stdlib.h>
//#include "ShowMatrix.h"

#define W1 10
#define W2 10

void ShowMatrix(int array[][W2], int lenD1, int lenD2)
{
    int a,b;
    for(a = 0; a < lenD1; ++a)
    {
        for(b=0; b<lenD2; ++b)
        {
            printf("Inhalt von Array[%d][%d] ",a,b);
            printf("ist: %d \n", array[a][b]);
        }
    }
}

int main(void) {
    int a,b;
    int array[W1][W2];
    for(a = 0; a < W1; ++a)
    {
        for(b=0; b<W2; ++b)
        {
            array[a][b] = rand() %10;
        }
    }
    ShowMatrix(array, W1, W2);

    return EXIT_SUCCESS;
}

Output:

Inhalt von Array[0][0] ist: 3 
Inhalt von Array[0][1] ist: 6 
Inhalt von Array[0][2] ist: 7
...
cse
  • 4,066
  • 2
  • 20
  • 37