Program haulting when trying to assign element in bool array to false

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
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
#ifndef MATRIX_H
#define MATRIX_H
#include "dlist.h"
#include "zlist.h"

const int MAXSIZE = 4;

template <class Instance_of_Type, class Mat> void RESIZE(Instance_of_Type instance, Mat & m, int newRows, int newCols)
{
    int tr = m.myRows, tc = m.myCols;
    m.myRows = newRows; m.myCols = newCols;
    for (int x = 0; x < newRows; ++x)
        for (int y = 0; y < newCols; ++y)
            if (x >= tr || y >= tc)
                m(x,y) = 0;
    return;
}

template <class Mat> void RESIZE(dlist frikken, Mat & m, int newRows, int newCols)
{
    int tr = m.myRows, tc = m.myCols;
    m.myRows = newRows; m.myCols = newCols;

    for (int i = 0; i < newRows; ++i)
        for (int j = 0; j < newCols; ++j)
            {
                cout << "m(" << i << ", " << j << ").length = " << m(i, j).length << endl;
                m(i, j).resize(newRows); cout << "after it is " << m(i, j).length << endl;
                if (i >= tr || j >= tc)
                    m(i, j) = 0;
            }
    return;
}

template <class Mat> void RESIZE(zlist frikken, Mat & m, int newRows, int newCols)
{
    int tr = m.myRows, tc = m.myCols;
    m.myRows = newRows; m.myCols = newCols;
    return;
    for (int i = 0; i < newRows; ++i)
        for (int j = 0; j < newCols; ++j)
            {
                //m(i, j).resize(newRows);
                if (i >= tr || j >= tc)
                    m(i, j) = 0;
                else m(i, j).resize(newRows);
            }
    return;
}




template <class itemType>
class matrix
{
  public:

    matrix( );                                      // default size 0 x 0
    matrix( int rows, int cols );                   // size rows x cols
    matrix( int rows, int cols,
            const itemType & fillValue );           // all entries == fillValue
    matrix( const matrix & mat );                   // copy constructor
    ~matrix( );

  // assignment
    const matrix & operator = ( const matrix & rhs );
    //bool apmatrix & operator == ( const apmatrix & rhs );
    bool operator == (const matrix & rhs) const;
    bool operator != (const matrix & rhs) const;

    int numrows( ) const;                             // number of rows
    int numcols( ) const;                             // number of columns

    const void displaymatrix();

    void resize(int newRows, int newCols);

    itemType & operator ( ) (int k, int y);
    const itemType & operator ( ) (int k, int y) const;

    int myRows;                             // # of rows (capacity)
    int myCols;                             // # of cols (capacity)

    const void clear();


 private:
       itemType myMatrix[MAXSIZE][MAXSIZE];
};


#include "matrix.cpp"
#endif


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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#ifndef MATRIX_CPP
#define MATRIX_CPP

#include "matrix.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
//#include "open.h"

template <class itemType>
matrix<itemType>::matrix()
        : myRows(0),
          myCols(0)
          //myMatrix(0)
          {
          }

template <class itemType>
matrix<itemType>::matrix(int rows,int cols)
        : myRows(rows),
          myCols(cols)
          //myMatrix(rows)
{
    myRows = rows;
    myCols = cols;
}

template <class itemType>
matrix<itemType>::matrix(int rows, int cols, const itemType & fillValue)
        : myRows(rows),
          myCols(cols)
          //myMatrix(rows)
{
    myRows = rows;
    myCols = cols;
    for (int i = 0; i < myRows; ++i)
        for (int j = 0; j < myCols; ++j)
            myMatrix[i][j] = fillValue;
}

template <class itemType>
matrix<itemType>::matrix(const matrix<itemType> & mat)
    : myRows(mat.myRows),
      myCols(mat.myCols)
      //myMatrix(mat.myRows)
{
    myRows = mat.myRows;
    myCols = mat.myCols;
    for (int i = 0; i < myRows; ++i)
        for (int j = 0; j < myRows; ++j)
            myMatrix[i][j] = mat.myMatrix[i][j];
}

template <class itemType>
matrix<itemType>::~matrix ()
{
}

template <class itemType>
const matrix<itemType> &
matrix<itemType>::operator = (const matrix<itemType> & rhs)
{
    if (this != &rhs)                    // don't assign to self!
    {
        myRows = rhs.myRows;             // set dimensions
        myCols = rhs.myCols;
        for (int i = 0; i < myRows; ++i)
            for (int j = 0; j < myCols; ++j)
                myMatrix[i][j] = rhs.myMatrix[i][j];

    }
    return *this;
}

template <class itemType>
bool matrix<itemType>::operator==(const matrix<itemType> &other) const
{
    matrix<itemType> temp(*this);
    bool tv = true;
    if (temp.numrows() != other.numrows())
        tv = false;
    if (temp.numcols() != other.numcols())
        tv = false;

    for (int x = 0; x <temp.numrows(); ++x)
        {
            for (int y = 0; y < temp.numcols(); ++y)
                {
                    if (temp(x, y) != other(x,y))
                        {
                            tv = false;
                        }
                }
        }
    return tv;
}

template <class itemType>
bool matrix<itemType>::operator!=(const matrix<itemType> &other) const
{
    matrix<itemType> temp(*this);
    bool tv;
    tv = (temp == other);
    tv = !tv;
    return tv;
}

template <class itemType>
int matrix<itemType>::numrows() const
{
    return myRows;
}

template <class itemType>
int matrix<itemType>::numcols() const
{
    return myCols;
}


template <class itemType>
//void matrix<itemType>::resize(itemType xyz, int newRows, int newCols)
void matrix<itemType>::resize(int newRows, int newCols)
{
    static int debugcount;
    ++debugcount;
    matrix<itemType> temp(*this);
    //myRows = newRows;
    //myCols = newCols;
    //return;
    //return RESIZE(myMatrix[0][0], *this, newRows, newCols);

    //RESIZE(myMatrix[0][0], temp, newRows, newCols);

    RESIZE(myMatrix[0][0], *this, newRows, newCols);

    //cout << "testing: " << temp(0, 0).length << endl;
    cout << "!!!!!!!!!!this is before *this = temp.!!!!!!!!! \n" << endl;
    cout << "debugcount = " << debugcount << endl; //if (debugcount > 1) exit(debugcount);
    //exit(debugcount);
    //*this = temp;
    cout << "**********this is before return********\n" << endl;

    return;
}


template <class itemType>
const void matrix<itemType>::displaymatrix()
{
    //matrix<itemType> mat(myRows, myCols);
    //mat = *this;
    matrix<itemType> mat(*this);
    for (int i = 0; i < mat.numrows(); ++i)
        {
            for (int j = 0; j < mat.numcols(); ++j)
                {
                    cout << mat.myMatrix[i][j] << " ";
                }
            cout << endl;
        }
}



template <class itemType>
const void matrix<itemType>::clear()
{
    cout << "myRows, myCols = " << myRows << " " << myCols << endl;
    for (int i = 0; i < myRows; ++i)
        for (int j = 0; j < myCols; ++j)
            myMatrix[i][j] = 0;
}

template <class itemType>
itemType &
matrix<itemType>::operator ( ) ( int k, int y)
{
    if (k < 0 || myRows <= k)
    {
        cerr << "Illegal row index: " << k << " max index = ";
        cerr << myRows-1 << endl;
        exit(y);
    }
    if (y < 0 || myCols <= y)
        {
            cerr << "Illegal column index: " << y << " max index = " ;
            cerr << myCols-1 << endl;
            exit(k);
        }
    return myMatrix[k][y];

}


template <class itemType>
const itemType &
matrix<itemType>::operator ( ) (int k, int y) const
// precondition: 0 <= k < number of rows
// postcondition: returns k-th row
{
    if (k < 0 || myRows <= k)
    {
        cerr << "Illegal row index: " << k << " max index = ";
        cerr << myRows-1 << endl;
        exit(y);
    }
    if (y < 0 || myCols <= y)
        {
            cerr << "Illegal column index: " << y << " max index = ";
            cerr << myCols-1 << endl;
            exit(k);
        }

    return myMatrix[k][y];
}
#endif


foo.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
#include "matrix.h"
#include <iostream>
#include <stdlib.h>
using namespace std;

const short int maxxx = 100;
class foo{
    public:

    int length;
    private: bool digits[maxxx];public:
    void removeN(const int & x);
    void resize(const int & n);
    bool containsN(const int & x);
    foo & operator=(const foo &rhs);
    foo & operator= (const bool & rhs);
};


void foo::resize(const int & n)
{
    if (n == -1) exit (-999);
    if (n <= maxxx)
        length = n;
    else return;
    cout << "inside resize length = " << length << endl;

    for (int i = n; i <= maxxx; ++i)
        {//cout << "i = " << i << endl;
            if (containsN(i))
                {removeN(i); }
        }
    return;
}

bool foo::containsN(const int & x)
{
    if (digits[x-1] == true)
        return true;
    else return false;

}

void foo::removeN(const int & x)
{

    cout << "x = " << x << "!!!"<<endl;

    if (x == 68) { cout << "before questionable assignment.\n"; cout << "digits[x] = " << digits[x-1] << endl;  }//exit(701);
    digits[x-1] = false; cout << "ok\n";
    //if (x == 68) {cout << "after questionable assignment.\n"; exit(702);}
    cout << "after digits[x-1] = false\n";
    return;

}


int main()
{
    foo me;
    me.removeN(5);
    me.resize(4);

    //matrix<foo> please;
    foo dangit[81];
    //please.resize(9, 9);
    for (int i = 0; i < 81; ++i)
        dangit[i].resize(9);

    matrix<foo> fm;
    fm.resize(9,9);


    cout << "\nThis is the 'You have reached the end of a program.' statement.\n" << endl;
    return 0;
}


foo& foo::operator=(const foo &rhs)
{
    length = rhs.length;
    for (int i = 0; i < length; ++i)
        {
            digits[i] = rhs.digits[i];
        }

}

foo& foo::operator =(const bool &rhs)
{
    static int counter;
    ++counter;
    //if (length != 9) exit(length);
    for (int i = 0; i < length; ++i)
        {
            digits[i] = rhs;
        }

}



What is going wrong?
I think a lack of a proper default constructor is part of the problem here;
Let's look at this bit:
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
int main()
{
    foo me;
    me.removeN(5);
    me.resize(4);

    //matrix<foo> please;
    foo dangit[81];
    //please.resize(9, 9);
    for (int i = 0; i < 81; ++i)
        dangit[i].resize(9);

    matrix<foo> fm;
    fm.resize(9,9);


    cout << "\nThis is the 'You have reached the end of a program.' statement.\n" << endl;
    return 0;
}


foo& foo::operator=(const foo &rhs)
{
    length = rhs.length;
    for (int i = 0; i < length; ++i)
        {
            digits[i] = rhs.digits[i];
        }

}

foo& foo::operator =(const bool &rhs)
{
    static int counter;
    ++counter;
    //if (length != 9) exit(length);
    for (int i = 0; i < length; ++i)
        {
            digits[i] = rhs;
        }

}


if you try this part of the program - you will find that this bit seems to behave
strangely.
1
2
3
4
5
6
7
8
bool foo::containsN(const int & x)
{
    if (digits[x-1] == true)
        return true;
    else return false;

}

Do you find that the if (digits[x-1] == true) fails - but if you use code] (digits[x-1]) it will work??

This is because if you have no proper constructor - so when you create the variable foo me; the default constructor for a bool is like that for integers - that is to say it does nothing - it will have some rubbish value - which is almost certainly be non-zero which
will make it true in the normal C/C++ sense, but not in the C++ bool sense.

so if (digits[x-1] == true) will fail but if (digits[x-1]) works

Do you see the difference?


Add a proper default constructor to foo class - For example
1
2
3
4
5
6
7
8
9
10
  foo()
    {
        length = 0; //set your length
        for (int n =0; n< maxxx; n++)
        {
            //set digits to proper C++ bool value.
            digits[n] = false;//or true depending on what you want.
        }

    }



PS - I think the indexing in the resizeN llop end up being function is is one-off

can you post dlist and zlist header files ??
Last edited on
I did what you suggested and changed all of the if (digits[x-1] == true) to if ((digits[x-1]) and that helped significantly. It helped me get through all of the code that I had been having trouble with yesterday. But my dlist constructors already had initialized the length values so that is not the culprit.

I was still getting other errors in other sections of code. Fortunately I was reminded just now that last night it had dawned on me what the problem is.

In my matrix.h header file I defined:
 
const int MAXSIZE = 4;


and in my class definition:
1
2
private:
       itemType myMatrix[MAXSIZE][MAXSIZE];


but had been trying to:D.resize(9, 9);

so I changed const int MAXSIZE = 4 to const int MAXSIZE = 16 and everything seems to be working now.

Although I wish there were a way to declare a variablly sized double array.

I have the data members
int myRows; int myCols; to keep track of the actual size the matrix I'm using is...
but it turns out this method isn't nearly as fast as I had hoped because to use a 9x9 matrix you have to initialize a 16x16 matrix just in case you wanted to resize it to that size later. . Instead of being 7 times as fast to declare a matrix, its less then twice as fast...

but wouldn't it be possible also to use pointers somehow? The only way i can think of so far is to have a data member something like:
1
2
    typedef itemType mat2[2][2], mat3[3][3], mat4[4][4];
    mat2 * m2; mat3 * m3; mat4 * m4;


And have mymatrix somehow point to either m2 m3 or m4, somehow.
But I don't know how.

But if I try to redefine mymatrix as
itemType * mymatrix[][]; I get the same error as I had originally gotten when trying to declare a variably sized double array. (Which is weird, because I have always been able to get by in the past with declaring variably sized single arrays.)


Any suggestions of what to do next?
(Thanks again guestgulkan for your help.)
Topic archived. No new replies allowed.