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
)
Asked
Active
Viewed 151 times
0

Richard Erickson
- 2,568
- 8
- 26
- 39

user259548
- 29
- 2
-
1What language are you using? Without that detail, your question is too broad. – Richard Erickson Dec 22 '15 at 13:05
-
Oh sorry I forgot to say I'm using C . – user259548 Dec 22 '15 at 13:05
-
3http://stackoverflow.com/questions/9446707/correct-way-of-passing-2-dimensional-array-into-a-function check out this question – CoreMeltdown Dec 22 '15 at 13:10
3 Answers
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 :)

Jirka Mára
- 15
- 5
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
-
I said I know how to do it in a loop . I just wanted to know how to insert them to a function – user259548 Dec 22 '15 at 14:35
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