Reputation: 327
I implemented c++ program which can find elements of matrix: Which is max element of the row and at the same time min element of its column or - min element of the row and at the same time max element of its column. For example we have data.txt file with:
Where 4 is n - matrix size (4x4), 7 and 10 are those numbers.
Here is the code:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream is;
ofstream os;
int n;
is.open("data.txt");
if (is.fail()){
cout << "Failed to open data file" << endl;
exit(-1);
}
is >> n;
double **matrix;
matrix = new double*[n];
for (int i = 0; i < n; i++){
matrix[i] = new double[n];
}
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
is >> matrix[i][j];
}
}
double* maxROW = new double[n];
double* minROW = new double[n];
for (int i = 0; i < n; i++){
maxROW[i] = matrix[i][0];
minROW[i] = matrix[i][0];
for (int j = 0; j < n; j++){
if (matrix[i][j] > maxROW[i]){
maxROW[i] = matrix[i][j];
}
if (matrix[i][j] < minROW[i]){
minROW[i] = matrix[i][j];
}
}
}
double* maxCOLUMN = new double[n];
double* minCOLUMN = new double[n];
int x = 0;
for (int i = 0; i < n; i++){
maxCOLUMN[i] = matrix[0][x];
minCOLUMN[i] = matrix[0][x];
for (int j = 0; j < n; j++){
if (matrix[j][i] > maxCOLUMN[i]){
maxCOLUMN[i] = matrix[j][i];
}
if (matrix[j][i] < minCOLUMN[i]){
minCOLUMN[i] = matrix[j][i];
}
if (j == n - 1) x++;
}
}
int k = 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (maxROW[i] == minCOLUMN[j]){
k++;
}
if (minROW[i] == maxCOLUMN[j]){
k++;
}
}
}
double* matrixNUM = new double[k];
int l = 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (maxROW[i] == minCOLUMN[j]){
matrixNUM[l] = matrix[i][j];
l++;
}
if (minROW[i] == maxCOLUMN[j]){
matrixNUM[l] = matrix[i][j];
l++;
}
}
}
cout << "Matrix numbers: " << endl;
for (int i = 0; i < k; i++){
cout << matrixNUM[i] << ", ";
}
cout << endl;
for (int i = 0; i < n; i++){
delete[] matrix[i];
}
delete[] matrix;
delete[] maxCOLUMN;
delete[] minCOLUMN;
delete[] maxROW;
delete[] minROW;
delete[] matrixNUM;
return 0;
}
Question: I want to know if my code is - "dirty" code? Because I'm always keen to make everything so difficult, when it is possible to make it easy. Whether there is a different way, maybe easier, maybe more understandable way to implement such a task? Thanks in advance who will find mistakes in this code.
Upvotes: 0
Views: 2278
Reputation: 60443
Random note, C++11 still has cumbersome aspects to its variable-length array handling, but:
int n; is >> n;
double (&matrix)[n][n] = *(double(*)[n][n])(new double[n*n]);
for (auto &r:matrix)
for (auto &c:r)
is >> c;
// ...
delete[] &matrix
stacks up pretty well.
Upvotes: 0
Reputation: 275780
int main(){
std::ifstream is;
std::ofstream os;
is.open("data.txt");
if (is.fail()){
std::cerr << "Failed to open data file\n";
exit(-1);
}
int n;
is >> n;
if (n <= 0)
{
std::cerr << "Negative matrix size, aborting\n";
exit(-1);
}
std::vector< std::vector<double> > matrix( n, std::vector<double>(n) );
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
is >> matrix[i][j];
}
}
std::vector<double> maxROW(n);
std::vector<double> minROW(n);
for (int i = 0; i < n; i++){
auto minmax = std::minmax_element( matrix[i].begin(), matrix[i].end() );
maxROW[i] = *minmax.second;
minROW[i] = *minmax.first;
}
std::vector<double> maxCOLUMN(n);
std::vector<double> minCOLUMN(n);
for (int i = 0; i < n; i++){
maxCOLUMN[i] = matrix[0][i];
minCOLUMN[i] = matrix[0][i];
for (int j = 1; j < n; j++){
maxCOLUMN[i] = std::max( maxCOLUMN[i], matrix[j][i] );
minCOLUMN[i] = std::min( minCOLUMN[i], matrix[j][i] );
}
}
std::vector<double> matrixNUM;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (maxROW[i] == minCOLUMN[j]){
matrixNUM[l].push_back(matrix[i][j]);
}
if (minROW[i] == maxCOLUMN[j]){
matrixNUM[l].push_back(matrix[i][j]);
}
}
}
std::cout << "Matrix numbers: " << endl;
for (double num:matrixNUM)
std::cout << num << ", ";
}
std::cout << "\n";
}
There may be typos, but the above should be as fast, and is less code.
It does use some C++11 features.
Upvotes: 2