0% found this document useful (0 votes)
7 views

Matrix Code

The document contains C++ code that defines and manipulates a two dimensional integer array (matrix). It generates random values for the matrix and performs several operations on the matrix like calculating row and column sums, finding maximum and minimum values, and counting even and odd values.

Uploaded by

Mihaela Guja
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Matrix Code

The document contains C++ code that defines and manipulates a two dimensional integer array (matrix). It generates random values for the matrix and performs several operations on the matrix like calculating row and column sums, finding maximum and minimum values, and counting even and odd values.

Uploaded by

Mihaela Guja
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

#include <cstdlib> // <stdlib.h>


#include <ctime> // <time.h>
#include <cmath> // <math.h>
#include <iomanip>

using namespace std;

int main() {
srand(time(0));

/*
00 01 02 ij ij ij
10 11 12 ij ij ij
20 21 22 ij ij ij
*/
/*
int matrix[3][3];

for(int i = 0; i < 3; i++)


{
for(int j = 0; j < 3; j++)
{
cout<<"i="<<i<<" j="<<j<<" ";
}
cout<<endl;
}
cout<<endl;

// alg de inserare
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
matrix[i][j] = rand()%10;
//cout << "M["<<i<<"]["<<j<<"]=";
//cin >> matrix[i][j];
}
cout<<endl;
}

// alg de afisare
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
cout<<setw(3)<<matrix[i][j];
}
cout<<endl;
}
*/

int matrix[100][100];
int n,m;
//1. [][] NxM
do
{
cout<<"Enter n = ";
cin >>n;
}while(n> 100 || n < 1);

do {
cout <<"Enter m = ";
cin >> m;
}while (m > 100 || m < 1);

for(int i = 0; i < n; i++)


{
for(int j = 0; j < m; j++)
{
matrix[i][j] = rand()%10;
}
cout<<endl;
}

for(int i = 0; i < n; i++)


{
for(int j = 0; j < m; j++)
{
cout<<setw(3)<<matrix[i][j];
}
cout<<endl;
}

// 2. suma [][]
int sum = 0;

for(int i = 0; i < n; i++)


{
for(int j = 0; j < m; j++)
{
sum += matrix[i][j];
}
cout<<endl;
}

cout<<"suma = "<<sum<<endl;

// 3. suma per linie

for(int i = 0, s; i < n; i++)


{
s = 0; // resetam suma la fiecare linie noua
for(int j = 0; j < m; j++) // j parcurge linia (per coloana)
{
s += matrix[i][j];
}
cout <<"suma per linie"<<s<<endl;
}
cout<<endl;

//4. suma per linie para si suma per linie impara


for(int i = 0, si,sp; i < n; i++)
{
si = 0; sp = 0;
for(int j = 0; j < m; j++)
{
if(i % 2 == 0) sp += matrix[i][j];
else si += matrix[i][j];
}
cout <<(i %2 == 0 ? "par " : "impar ")<<"s = "<<( i %2 == 0 ? sp :
si)<<endl;
}
cout<<endl;
/*for(int i = 0, s; i < n; i++)
{
s = 0;
for(int j = 0; j < m; j++)
{
s += matrix[i][j];
}
cout <<(i %2 == 0 ? "par " : "impar ")<<"s = "<<s<<endl;
}
cout<<endl;*/

//5. contor valorile pare si impare per linie


for(int i = 0, ci,cp; i < n; i++)
{
cp = 0; ci = 0;
for(int j = 0; j < m; j++)
{
if(matrix[i][j] % 2 == 0) cp++;
else ci++;
}
cout <<i<<" cp"<<cp<<" ci"<<ci<<endl;
}
cout<<endl;

//6. max si min per linie (per vector)


for(int i = 0, max,min; i < n; i++)
{
max = matrix[i][0]; // i = a cata linia, [0] atribui val primului element
min = matrix[i][0];
for(int j = 0; j < m; j++)
{
if(max < matrix[i][j]) max = matrix[i][j]; // rest max
if(min > matrix[i][j]) min= matrix[i][j];
}
cout<<i<<"."<<" max = "<<max<<" min = "<< min<<endl;
}
cout<<endl;

// [][] NxM
//1. suma coloanelor, suma col. pare si impare
//2. contor de cate ori primul element din linile se repeta
//3. contor pentru max si

return 0;
}

You might also like