2d array in classes

Jul 26, 2018 at 2:15am
Can someone please look at my code? My professor gave us main.cpp and expects us to be able to code the class files. I tried coding it, but it's not working. I need help

The assignment is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
This time you create a small library with a single class in it. You should submit me a header file matrix.h and an implementation file matrix.cpp . matrix.h contains the declaration of class Matrix and  matrix.cpp contains its implementation. None of those files should contain function main() . But can you run and test program without the function main() ? Of course not. You will write a .cpp-file with function main() to test and debug your library/class. You just do not submit it to me. I will write my own program and include your library in it.

Since I will use your library with my own code it is extremely important to strictly obey my interface specifications. Make sure all user-visible names match my specification, all parameter types and parameter order too. For example, if in my specification the function is called Set_Identity() but in your class it is called set_Identity(), my code will not work with your library and I will consider the function Set_Identity() broken or missing.

 

You should declare and implement the class Matrix . An object of the Matrix class in an integer square matrix (integer 2D-array with the same number of rows and columns). 2D-array should be implemented using operators new and delete. Make sure to avoid memory leaks.

To deal with 2D array your class should have a corresponding pointer. Also, your class stores the current size of the matrix.


The following public methods should be implemented:

 

    Default constructor (with no parameters). Sets the pointer to nullptr and the size to zero. There is no practical need to implement this constructor as a separate function but I still ask you to do it.
    Constructor that takes an integer number and creates a square matrix of that size filled with 3’s. Of course, the size should be updated correspondingly.
    Destructor.
    Resize (new_size) function. Takes an integer size and changes the matrix size to that size. Then fills the new matrix with 3’s. The content of the matrix that was there before the Resize is lost. Attention: you will allocate memory for the matrix of the new size, so you must make sure that any previously allocated memory is correctly released.
    Size () function. Returns the size of matrix.
    At (row , column)  function returns the value located in a given row and column. Both row and column are given as integer numbers.
    Set_Identity () function sets the matrix to identity matrix. Identity matrix is the matrix with all zeroes except the elements of the “top left to bottom right” diagonal which are 1. The function doesn’t change the size of the matrix.


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include "matrix.h"

using namespace std;

int main()
{
  Matrix A, B(3);
  cout << "the size of matrix B is " << B.Size();
  A.Resize(3);
  A.Set_Identity();
  for(int i=0 ; i<A.Size() ; i++)
  {
    for(int j=0 ; j<A.Size() ; j++)
      cout<<A.At(A.Size()-i-1, j)<< ' ';
    cout<<'\n';
  }

  cout << '\n';

  for(int i=0 ; i<B.Size() ; i++)
  {
    for(int j=0 ; j<B.Size() ; j++)
      cout<<B.At(A.Size()-i-1, j)<<' ';
      cout<<'\n';
  }
return 0;
}        

my code for matrix.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

using namespace std;

#ifndef matrix_h
#define matrix_h

class Matrix
{
private:
int A;
int B;

public:
int **matrix; //pointer to pointer
int alloc(int A, int B);
Matrix(); //default constructor
Matrix(int B);
Matrix(int A, int B);
~Matrix();
// dealloc();

int At(int A, int B);// get column will return a pointer at [x,y]

int Size();
int Resize(int n);
int **Set_Identity();
};

#endif 

my code for matrix.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "matrix.h"

Matrix::Matrix() //default constructor
{
  *matrix = nullptr;
  alloc(0,0);
}

Matrix::Matrix(int F)
{
  alloc(F,F);
}

Matrix::Matrix(int D, int E)
{
  alloc(D,E);
}

int Matrix::alloc(int row, int col)//constructs a matrix of dimensions [rows][cols]
{ 
  A = row;
  B = col;
  int **matrix = new int *[A];
  for (int i = 0; i < A; i++)
  {
    matrix[i] = new int[B];
  }

  for (int i = 0; i < A; i++)
  {
    for (int j = 0; j < B; j++)
    {
      matrix[i][j] = 3;
    }
  }
  return **matrix;
}

int Matrix::Size()
{
  int rows{0};
  int cols{0};
  for (int i = 0; i < A; i++)
  {
    for (int j = 0; j < B; j++)
    {
      rows++;
    }
    cols++;
  }

  return (rows,cols);
}

int Matrix::Resize(int C)
{
  for (int i = 0; i < A; i++)
  {
    delete [] matrix[i];
  }
  delete [] matrix;
  return alloc(C,C);
}

int **Matrix::Set_Identity()
{
  for (int i = 0; i < Size(); i++)
  {
    for (int j = 0; j < Size(); j++)
    {
      if (i == j)
      {
        matrix[i][j] = 1;
      }
      else
      {
        matrix[i][j] = 0;
      }
    }
  }
  return 0;
  // for loop. if i == j, matrix[i][j] = 1 else matrix[i][j] = 0
}

int Matrix::At(int row, int col)
{
    if (col > B || row > A)
  {
    cerr << "The row/column at " << row << ' ' << col << "is out of bounds" << '\n';
    return 0;
  }
  else
  {
    return matrix[A][B];
  }
}


Matrix::~Matrix()
{
  for (int i = 0; i < A; i++)
  {
    delete [] matrix[i];
  }
  delete [] matrix;
} //destructor 



Last edited on Jul 26, 2018 at 2:23am
Jul 26, 2018 at 3:56am
nevermind, assignment is due. I'll ask a tutor in school.
Jul 26, 2018 at 8:08am
return (rows,cols);
This is wrong.
Topic archived. No new replies allowed.