0

Lets say I have a matrix with x rows and y columns and an integer a and I want to make a function that takes the matrix and multiply ever row with the integer a ( I know I can do this simply with a for loop ). How do I write the beginning of the function ? function( int a, matrix[x],[y])? (I'm using C)

Richard Erickson
  • 2,568
  • 8
  • 26
  • 39
user259548
  • 29
  • 2

3 Answers3

1

Creating a matrix (2D array) in C isn't as simple as in C#, Java etc. You have to create an array of arrays - by using pointers. Let me show you a function:

Function fill2D() is taking 3 arguments - pointer to matrix, number of rows, number of columns.

The declaration of a fill2D() function in functions.h file:

extern void fill2D(float **pole, int r, int c);`

The definition of a fill2D() function in functions.c file:

void fill2D(float **pole, int r, int c) {
int i, j;

for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++) {
        pole[i][j] = 1;
    }
}

And here's a code in main.c:

fill2D(p_float_array, rows, columns);

Definition of arguments for fill2D() function:

float **p_float_array = NULL;
int rows = 10;
int columns = 3;

Hope, it's a helpful answer :)

0

Pretty much the simplest way. You can declare the 2d array in multiple ways.

void ApplyConstant(int c, int matrix[][col], int row)
{
    for (j = 0; j < col; j++)
    {
        // access by matrix[row][j]
    }
}
Bran
  • 617
  • 8
  • 21
0

If i understood you right, you need the declaration of this function. In this case i use:

void func(int a, int* matrix, int len);
int main(void) {
    int matrix[4][4] = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
    func(3,matrix[4],4);
    return 0;
}

void func(int a, int* matrix, int len) {
    //your loop code here...
}

Hope it's helped you .

nisanarz
  • 895
  • 1
  • 12
  • 22