it:informatica:linguaggio_c:funzioni_e_matrici_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/it/informatica/linguaggio_c/funzioni_e_matrici_1
Funzioni e Matrici (Esempio 1)
Concetti:
Passaggio di matrici a funzioni (le matrici in C vengono passate alle funzioni sempre per riferimento)
Testo:
Si realizzi una funzione che:
- riceve in ingresso una matrice e la sua dimensione (
nr
enc
, rispettivamente il numero di linee e di colonne). Il prototipo della funzione da realizzare è il seguente:
void cambiaSegno(int matr[][C], int nr, int nc);
- la funzione dovrà cambiare di segno tutti gli elementi delle righe della matrice con indice di riga pari (righe 0, 2, 4, 6,…)
La funzione chiamante (main
) dovrà stampare la matrice prima e dopo la trasformazione effettuata dalla funzione cambiaSegno
.
Soluzione:
- matrici_funzioni_1.c
/* Realizzare una funzione che riceve in ingresso una matrice e le sue dimensioni, cioè il suo numero di righe e di colonne. La funzione deve cambiare di segno tutte le righe con indice di riga pari (righe 0, 2, 4, 6,...). */ #include <stdio.h> #define R 3 #define C 4 void cambiaSegno(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("Matrice iniziale\n"); for(i=0; i<R; i++){ for(j=0; j<C; j++){ printf("%3d", m[i][j]); } printf("\n"); } cambiaSegno(m, R, C); printf("Matrice con elementi cambiati di segno\n"); for(i=0; i<R; i++){ for(j=0; j<C; j++){ printf("%3d", m[i][j]); } printf("\n"); } return 0; } void cambiaSegno(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/it/informatica/linguaggio_c/funzioni_e_matrici_1
/web/htdocs/www.skenz.it/home/data/pages/it/informatica/linguaggio_c/funzioni_e_matrici_1.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1