Pagini recente » Cod sursa (job #1550398) | Cod sursa (job #907866) | Cod sursa (job #1977525) | Cod sursa (job #1485113) | Cod sursa (job #1054613)
#include <fstream>
#include <iostream>
#include <math.h>
#include <conio.h>
using namespace std;
int matrixSum(int** matrix, int lines, int columns);
void printMatrix(int** matrix, int lines, int columns);
int main() {
int columns, lines;
int** matrix;
ifstream inFile;
inFile.open("flip.in", std::ios_base::in);
inFile >> lines >> columns;
matrix = new int*[lines];
for(int line = 0; line < lines; line++) {
matrix[line] = new int[columns];
for(int column = 0; column < columns; column++) {
inFile >> matrix[line][column];
}
}
inFile.close();
printMatrix(matrix, lines, columns);
for(int l = 0; l < lines; l++) {
int pozitives = 0, negatives = 0;
for(int c = 0; c < columns; c++) {
if(matrix[l][c] > 0) {
pozitives += matrix[l][c];
} else {
negatives -= matrix[l][c];
}
}
if(negatives > pozitives) {
for(int c = 0; c < columns; c++) {
matrix[l][c] = -matrix[l][c];
}
}
}
for(int c = 0; c < columns; c++) {
int pozitives = 0, negatives = 0;
for(int l = 0; l < lines; l++) {
if(matrix[l][c] > 0) {
pozitives += matrix[l][c];
} else {
negatives -= matrix[l][c];
}
}
if(negatives > pozitives) {
for(int l = 0; l < lines; l++) {
matrix[l][c] = -matrix[l][c];
}
}
}
printMatrix(matrix, lines, columns);
int sum = matrixSum(matrix, lines, columns);
cout << "Suma maxima este: " << sum;
ofstream outFile;
outFile.open("flip.out", std::ios_base::out);
outFile << sum;
outFile.close();
_getch();
}
void printMatrix(int** matrix, int lines, int columns) {
for(int l = 0; l < lines; l++) {
for(int c = 0; c < columns; c++) {
cout << matrix[l][c] << ' ';
}
cout << endl;
}
cout << endl;
}
int matrixSum(int** matrix, int lines, int columns) {
int rezult = 0;
for (int l = 0; l < lines; l++) {
for (int c = 0; c < columns; c++) {
rezult += matrix[l][c];
}
}
return rezult;
}