Answer the question
In order to leave comments, you need to log in
Determining the maximum among non-repeating elements of a matrix (C++)
Can't figure out how to find unique elements in a matrix.
#include "stdafx.h"
#include <iostream>
using namespace std;
class Matrix {
public:
int m,n;
int a[100][100];
Matrix ( );// конструктор
~Matrix ( );// деструктор
void Max();
};
Matrix:: Matrix ( ) // Вводим размерность матрицы
{
cout << "Vvod v matrix \n";
cout << "Vvedite kolvo strok: ";
cin >> m;
cout << "Vvedite kolvo stolbcov: ";
cin >> n;
}
Matrix:: ~Matrix()
{
cout << "Deleted...\n";
}
void Matrix :: Enter ( ) //ввод чисел в матрицу
{
int i;
int j;
for (i=0; i<m; i++)
{
for (j=0; j<n;j++)
{
cout << "Vvod chisel v matricu: " << "a["<<i<<"]["<<j<<"]:" ;
cin >> a[i][j];
}
}
cout <<"\n";
};
void Matrix :: Max() // нахождение максимального числа
{
int i,j,p,q;
int x=0,k=0;
int b[100];
int l,c = 0;
int max=0;
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
x=a[i][j];
k=0;
for (p=0;p<m;p++)
{
for (q=0;q<n;q++)
{
if (i!=p && j!=q && a[p][q]==x)
{
k=1;
}
}
}
if (k==0)
{
b[c]=a[i][j];
c+=1;
}
}
}
max = b[0];
for (l=0;l<c;l++)
{
{
cout<<b[l];
if (b[l]>max)
{
max=b[l];
}
}
cout << "\n";
}
cout << "maximum=" << max;
cout << "\n";
}
main()
{
Matrix ob;
ob.Enter;
ob.Max();
system("pause");
return 0;
}
Answer the question
In order to leave comments, you need to log in
Let's first give the normal names that reflect their purpose to the variables. Please update the question. And then we'll see where you're wrong.
And there is no desire to understand what l, c, m is.
ZY and variables can be declared separated by commas, like this:
and even like this:
UPD replace the condition:
with the following:
Due to incorrect logic, when checking, the same elements standing on the same line or in the same column were considered different.
#include <iostream>
#include <vector>
#include <cstdlib>
#include <climits>
void fill_vector(size_t x,
size_t y,
int range,
std::vector< std::vector <int> > &aVector)
{
for (size_t i = 0; i < x; ++i) {
std::vector <int> temp_vector;
for (size_t j = 0; j < y; ++j) {
temp_vector.push_back(rand() % range);
}
aVector.push_back(temp_vector);
}
}
void print_vector(const std::vector< std::vector <int> > &aVector)
{
for (size_t i = 0; i < aVector.size(); ++i) {
for (size_t j = 0; j < aVector.at(0).size(); j++) {
std::cout << aVector.at(i).at(j) << "\t";
}
std::cout << std::endl;
}
}
bool unique(int aElem,
const std::vector <int> &aVector)
{
int j = 0;
for (size_t i = 0; i < aVector.size(); ++i) {
if (aElem == aVector.at(i)) {
++j;
}
}
return (j > 1) ? false : true;
}
int determine_max_elem(const std::vector< std::vector <int> > &aVector)
{
int max_elem = INT_MIN;
std::vector <int> temp_vector;
for (size_t i = 0; i < aVector.size(); ++i) {
for (size_t j = 0; j < aVector.at(0).size(); j++) {
temp_vector.push_back(aVector.at(i).at(j));
}
}
for (size_t i = 0; i < temp_vector.size(); i++) {
if (max_elem < temp_vector.at(i) &&
unique(temp_vector.at(i), temp_vector)) {
max_elem = temp_vector.at(i);
}
}
return max_elem;
}
int main()
{
std::vector< std::vector <int> > vector;
fill_vector(5, 5, 10, vector); // Fill 2D 5x5 Matrix
print_vector(vector);
int max = determine_max_elem(vector);
if (max != INT_MIN) {
std::cout << "\nMax element is: " << max << std::endl;
} else {
std::cout << "\nMax element in matrix isn't found!" << std::endl;
}
return 0;
}
Based on the @EXL
listing
but I rewrote it with std::map
#include <iostream>
#include <vector>
#include <climits>
#include <map>
#include <ctime>
class MyMatrixInt {
public:
MyMatrixInt(size_t rows = 1, size_t cols = 1, int range = 1)
{
reFill(rows, cols, range);
}
void reFill(size_t rows = 1, size_t cols = 1, int range = 1)
{
data_.clear();
for (size_t i = 0; i < rows; ++i) {
std::vector<int> row;
for (size_t j = 0; j < cols; ++j) {
row.push_back(rand() % range);
}
data_.push_back(row);
}
}
int maxUniqueElem() const
{
int max_elem = INT_MIN;
std::map<int, int> elem_map;
for (size_t i = 0; i < data_.size(); i++) {
for (size_t j = 0; j < data_[i].size(); j++) {
elem_map[data_[i][j]]++;
}
}
std::map<int, int>::reverse_iterator rit;
for (rit = elem_map.rbegin(); rit != elem_map.rend(); ++rit) {
if (rit->second == 1) {
max_elem = rit->first;
break;
}
}
return max_elem;
}
void print() const
{
for (size_t i = 0; i < data_.size(); ++i) {
for (size_t j = 0; j < data_[0].size(); j++) {
std::cout << data_[i][j] << "\t";
}
std::cout << std::endl;
}
}
private:
std::vector<std::vector<int> > data_;
};
int main()
{
MyMatrixInt my_mat;
//srand(time(NULL));
my_mat.reFill(5, 5, 10); // Fill 2D 5x5 Matrix
my_mat.print();
int max = my_mat.maxUniqueElem();
if (max != INT_MIN) {
std::cout << "\nMax element is: " << max << std::endl;
}
else {
std::cout << "\nMax element in matrix isn't found!" << std::endl;
}
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question