N
N
NeoLogic2014-05-22 00:32:06
C++ / C#
NeoLogic, 2014-05-22 00:32:06

With ++ Problem with the successor, methods do not work. Matrix challenge?

Describe the type-object MATRIX (a matrix of arbitrary dimension M*N) and its methods: input of a matrix; matrix output; determination of the minimum element among the negative elements of the matrix. Describe the type and methods of its descendant MATR (methods: determining the greatest common divisor of matrix elements; determining the maximum among non-repeating matrix elements).

#include "stdafx.h"
//#include <stdlib.h>
//#include <iomanip>
//#include <cstdlib>
#include <iostream>
using namespace std;

class Matrix {
public:
int m,n;
int a[100][100];
Matrix ( );// конструктор
~Matrix ( );// деструктор
void Enter ();
void Display ();
void Min();
};

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 :: Display ()
{
  int i;
  int j;
  int l=0;
  for (i=0; i<m; i++)
  {
  for (j=0; j<n; j++)
  {
     cout << a[i][j] << " ";
  }
  cout << "\n";
  }
  cout << "\n";
  cin >> l;
};

void Matrix :: Min () 
{
  int i;
  int j;
  int min = a[0][0];
  for (i=0;i<m;i++)
  {
    for (j=0;j<n;j++)
    {
      if (a[i][j]<0)
      {
        if (a[i][j]<min)
        {
          min = a[i][j];
        }
      }
    }
  }
  if (min > 0)
  {cout << " Net otricatelbnblx 4isel";
  }
  else
  {
  cout << "Minimum= " << min;
  }
  cout << "\n";
}
int nod (int a, int b)  //нахождение НОД по алгоритму Евклида
{
    while (a && b)
        if (a >= b)
           a %= b;
        else
           b %= a;
    return a | b;
}

class Matr : public Matrix 
{
public:
void NOD ();
void Max ();
};


void Matr :: NOD()
{
  int i;
  int j;
  int q=a[0][0];
  for(i = 0; i<m; i++)
    {
    for(j=0; j<n; j++)
    {
        q=nod(q, a[i][j]);
    }
    }
  cout << q;
  cout << "\n";
}
void Matr :: Max()
{
  int i;
  int j;
  int max = a[0][0];
  for (i=0;i<m;i++)
  {
    for (j=0;j<n;j++)
    {
      if (a[i][j]>max)
      {
        max = a[i][j]; // Нахождение максимального элемента матрицы
      }
    }
  }
  cout << max;
  cout << "\n";
}


int main ()
{
  Matrix ob;
  Matr ob2;
    ob.Enter ();
  ob.Display();
  ob.Min();
  ob2.NOD();
  ob2.Max();
  system("pause");
  return 0;
};

An error occurs when outputting the response of ob2.NOD() and ob2.Max(). I can't figure it out, please help. I remove the heir, everything works, I create it back, everything stops working.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
ManWithBear, 2014-05-22
@NeoLogic

Values ​​are set only for ob, and ob2 remains uninitialized.
Change main to

int main () {
    Matr ob2;
    ob2.Enter ();
    ob2.Display();
    ob2.Min();
    ob2.NOD();
    ob2.Max();
    system("pause");
    return 0;
};

N
NeoLogic, 2014-05-22
@NeoLogic

Thank you) Everything worked)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question