Created
September 25, 2026 10:00
-
-
Save thinkphp/2751d8769a37dbfea6e63a4ded6ca9b1 to your computer and use it in GitHub Desktop.
matrice liniarizata transpusa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| int **mat; | |
| //mat = (int*)malloc(m * sizeof(int*)); | |
| //mat = [ptr1, ptr2, ptr3..] | |
| for(int i = 0; i < lines; ++i) { | |
| mat[i] = (int*)malloc(n * sizeof(int)) | |
| } | |
| int mat[3][5]; | |
| int *mat = (int*)malloc(lines * cols * sizeof(int)); | |
| */ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <time.h> | |
| void citesteMatrix(int *p, int n, int m) { | |
| printf("Citeste matricea manual ,element cu element:\n"); | |
| for(int i = 0; i < n; ++i) { | |
| for(int j = 0; j < m; ++j) { | |
| scanf("%d", &p[i * m + j]); | |
| } | |
| } | |
| } | |
| void genereazaMatrix(int *p, int n, int m) { | |
| printf("Matricea este generata cu elemente random(): \n"); | |
| for(int i = 0; i < n; ++i) { | |
| for(int j = 0; j < m; ++j) { | |
| p[i * m + j] = rand() % 100; | |
| } | |
| } | |
| } | |
| void afisareMatrix(int *p, int n, int m) { | |
| printf("===Afisare Matrix===\n"); | |
| for(int i = 0; i < n; ++i) { | |
| for(int j = 0; j < m; ++j) { | |
| printf("%d ", p[i * m + j]); | |
| } | |
| printf("\n"); | |
| } | |
| } | |
| void afisareLiniarizata(int *p, int n, int m) { | |
| printf("\n===Afisare liniarizata===\n"); | |
| for(int i = 0; i < (n * m); i++, p++) { | |
| printf("%d ", *p); //*(p + 0), *(p + 1) | |
| } | |
| } | |
| void afisezaTranspusa(int *p, int n, int m) { | |
| printf("\n===Afisare transpusa matricei===\n"); | |
| for(int j = 0; j < m; ++j) { | |
| for(int i = 0; i < n; ++i) { | |
| printf("%d ", p[i * m + j]); | |
| } | |
| printf("\n"); | |
| } | |
| } | |
| int main(int argc, char const *argv[]) | |
| { | |
| int n, m, optiune; | |
| srand(time(NULL)); | |
| printf("Numarul de linii = "); | |
| scanf("%d", &n); | |
| printf("Numarul de coloane = "); | |
| scanf("%d", &m); | |
| int *p = (int*)malloc(n * m * sizeof(int)); //aloci pentru o matrice de 3x3 9 * 4 bytes = 36bytes | |
| //int *q = (int*)malloc(n * m * sizeof(int)); //aloci pentru o matrice de 3x3 9 * 4 bytes = 36bytes | |
| //p + q /// | |
| if(p == NULL) { | |
| printf("Eroare la malloc"); | |
| return 1; | |
| } | |
| printf("Alegeti modul de completare a matricei: \n"); | |
| printf("1. Citire manuala\n"); | |
| printf("2. Generare aleatoare\n"); | |
| scanf("%d", &optiune); | |
| if(optiune == 1) { | |
| citesteMatrix(p, n, m); | |
| } else { | |
| genereazaMatrix(p, n, m); | |
| } | |
| afisareMatrix(p, n, m); | |
| afisareLiniarizata(p, n, m); | |
| afisezaTranspusa(p, n, m); | |
| printf("Free Memory!"); | |
| free( p ); | |
| return 0; | |
| } | |
| /* | |
| 1 2 3 | |
| matrix = 4 5 6 | |
| 7 8 9 | |
| matrix = {1,2,3,4,5,6,7,8,9} | |
| matrix[i * m + j] | |
| matrix[0] = prima linie prima coloana: 1 | |
| matrix[1] = prima linie a doua coloana; 2 | |
| matrix[2] = prima linie a treia coloana; 3 | |
| matrix[3] = a doua linie si prima coloana | |
| matrix[4] = a doua linie si a doua coloana | |
| int**alocaMatrice(int n, int m) { | |
| int **mat = (int**)malloc(n * size(int*)); | |
| if(mat == null) {return 1;} | |
| for(int i = 0; i < n; ++i) { | |
| mat[i] = (int*)malloc(m * sizeof(int)); | |
| if(mat[i] == NULL) { | |
| return 1; | |
| } | |
| } | |
| } | |
| return mat; | |
| } | |
| void elibereaza(int **mat, int n) { | |
| for(int i = 0; i < n; ++i) { | |
| free(mat[i]); | |
| } | |
| free(mat); | |
| } | |
| void genereazaMatrix(int **mat, int n , int m) { | |
| for(int i = 0; i < n; ++i) { | |
| for(int j = 0; j < m; ++j) { | |
| mat[i][j] = rand() % 100; | |
| } | |
| } | |
| } | |
| n = 3; | |
| m = 3; | |
| int **mat1 = alocaMatrice(n, m) | |
| int **mat2 = alocaMatrice(n, m) | |
| genereazaMatrice(mat1, n, m); | |
| */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment