cs:c_language:functions_and_matrices_1
Return to Home page
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/c_language/functions_and_matrices_1?do=diff&rev2%5B0%5D=1551188115&rev2%5B1%5D=1551188115&difftype=sidebyside
Differences
This shows you the differences between two versions of the page.
— | cs:c_language:functions_and_matrices_1 [2024/04/08 22:35] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
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 ('' | ||
+ | <code C> | ||
+ | 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 ('' | ||
+ | |||
+ | **Solution: | ||
+ | <file C matrici_funzioni_1.c> | ||
+ | /* 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(" | ||
+ | for(i=0; i<R; i++){ | ||
+ | for(j=0; j<C; j++){ | ||
+ | printf(" | ||
+ | } | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | change_sign(m, | ||
+ | |||
+ | printf(" | ||
+ | for(i=0; i<R; i++){ | ||
+ | for(j=0; j<C; j++){ | ||
+ | printf(" | ||
+ | } | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | |||
+ | void change_sign(int matr[][C], int nr, int nc){ | ||
+ | int r, c; | ||
+ | | ||
+ | for(r=0; r<nr; r++) { | ||
+ | if(r%2==0) { | ||
+ | for(c=0; c<nc; c++) { | ||
+ | matr[r][c] = -1*matr[r][c]; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ |
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/c_language/functions_and_matrices_1?do=diff&rev2%5B0%5D=1551188115&rev2%5B1%5D=1551188115&difftype=sidebyside
/web/htdocs/www.skenz.it/home/data/pages/cs/c_language/functions_and_matrices_1.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1