# How To Wiki

### Site Tools

cs:c_language:functions_and_matrices_1

# Differences

This shows you the differences between two versions of the page.

 cs:c_language:functions_and_matrices_1 [2019/02/26 14:35] cs:c_language:functions_and_matrices_1 [2020/11/26 23:18] (current) Line 1: Line 1: + ====== Functions and Matrices (Example 1) ====== + **Consepts:**\\ + Passage of a Matrix to a function (matrices in C are passed to a function by reference) + **Text:**\\ + Realize a function that: + * receives as parameters a matrix and its dimension (''nr'' and ''nc'' are the number of matrix rows and columns, respectively). The prototype of the function is the following: + + void change_sign(int matr[][C], int nr, int nc); + + * the function must change the sign of all the elements of the rows of the matrix that have an //even// row index (i.e, rows with indexes 0, 2, 4, 6,...) + + The calling function (''main'') must print the matrix before and after the transformation performed by the function ''change_sign''. + + **Solution:**\\ + + /* Realize a function that receives as parameters a matrix and its dimension, i.e., the number of rows and columns. + The function must change the sign of all the elements of the rows of the matrix that have an even row index + (i.e, rows with indexes 0, 2, 4, 6,...). */ + + #include + + #define R 3 + #define C 4 + + void change_sign(int matr[][C], int nr, int nc); + + int main(){ + int m[R][C] = {{1, 2 , 3, 4}, {-1, -2, 3, 4}, {1, -2, 3, -4}}; + int i, j; + + printf("Initial matrix\n"); + for(i=0; i