0% found this document useful (0 votes)
8 views3 pages

Matrix Operations in C++

The document discusses reading and processing a 2D matrix in C++. It provides examples of how to read in a matrix from input, then iterate through the matrix to perform various operations like printing the matrix, calculating the sum of elements on the main diagonal, or finding the maximum value in the matrix. It also gives examples of how to find the maximum value within each row or column.

Uploaded by

Mihai Hăţişi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Matrix Operations in C++

The document discusses reading and processing a 2D matrix in C++. It provides examples of how to read in a matrix from input, then iterate through the matrix to perform various operations like printing the matrix, calculating the sum of elements on the main diagonal, or finding the maximum value in the matrix. It also gives examples of how to find the maximum value within each row or column.

Uploaded by

Mihai Hăţişi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Matrice Citire Iau un contor i pt linii si un contor j pt coloane M=nr col N=nr linii Cin>>m>>n; For(i=1; i<=n; i++)

For(j=1; j<=m; j++) Cin>>a[i][j]; Parcurgere For(i=1; i<=n; i++) For(j=1; j<=m; j++) { ce vreau sa faca programul; } Exemplu: sa se citeasca si sa se afiseze o matrice Void main() {int a[i][j], m, n; Cin>>m>>n; For(i=1; i<=n; i++) For(j=1; j<=m; j++) Cin>>a[i][j]; For(i=1; i<=n; i++) {For(j=1; j<=m; j++) Cout<<a[i][j]; Cout<<endl;} //dupa ce am terminat de afisat prima linie trec pe urm rand } Elementele de pe diagonale Diagonala principala J==I Ex: sa se faca suma elementelor de pe diagonal principal //citire S=0; For(i=1; i<=n; i++) For(j=1; j<=m ; j++) If(j==i) S=s+a[i][j]; Cout<<s; Diagonala secundara

I=n-j+1 Ex: sa se faca produsul elem de pe diagonal secundara //citire p=1; For(i=1; i<=n; i++) For(j=1; j<=m ; j++) If(i==n-j+1) p=p*a[i][j]; Cout<<p; Probleme 1. Sa se afiseze maximul din matrice 2. Sa se afiseze max de pe fiecare linie. 3. Sa se afiseze max de pe fiecare coloana 1. Void main() { int a[10][10], n, m, i, j,max=0; Cin>>n>>m; For(i=1; i<=n; i++) For(j=1; j<=m; j++) If(a[i][j]>max) Max=a[i][j]; Cout<<max; 2 . Void main() { int a[10][10], n, m, i, j,max=0; Cin>>n>>m; For(i=1; i<=n; i++) { max=0; //max incepe de la 0 la fiecare linie parcursa For(j=1; j<=m; j++) //parcurg linia i If(max<a[i][j]) Max=a[i][j]; Cout<<max<< ; //afisez max de pe linia curenta adica i } 3 . Void main() { int a[10][10], n, m, i, j,max=0; Cin>>n>>m; //citire For(j=1;ji<=m;j++) // de data asta parcurg coloanele {max=0; For(i=1; i<=n;i ++) //parcurg coloana j

If(max<a[i][j]) Max=a[i][j]; Cout<<max<< ;}

You might also like