Problem with class that stores 2d matrix

Hello guys,

I have this code which stores 2d matrix in a class matrix (keeping its elements in a dynamically allocated array of integers (int **A

However when I execute the program it reads the matrix and outputs it correctly but when it's being multiplied by a scalar the program crashes.

Why is this ?

Here's my code:

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
#include<iostream>
#include <vector>
#include <ctime>
#include <fstream>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <numeric>
#include <iterator>
#include <iomanip>

class matrix
{
	int **A;                          // pointer to store 2 dimensional array
	int x, y;

public:
	matrix();

	matrix(int, int);
	
	void read();
	void input();
   	void display();
	matrix operator* (int);
};

//
matrix :: matrix()
{
	x = 0;
	y = 0;
}

matrix :: matrix(int a, int b)
{
	x = a;
	y = b;
}



//read matrix
void matrix :: read()
{
	std::cout << " enter number of rows " << std::endl;
	std::cin >> x;
	std::cout << " enter number of columns " << std::endl;
	std::cin >> y;
}

//matrix input
void matrix :: input()
{ 
    	int num1, num2, k;
     	int i, j;
	A = new int *[x];            
	for (k = 0; k < x; k++)     	
		A[k] = new int [y];
	for (num1=0; num1<x; num1++)
	{

		for (num2=0; num2<y; num2++)
		{
			A[num1][num2] = 0;
		}
	} 
	for ( i = 0; i < x; i++)
	{
		for ( j = 0; j < y; j++)
		{
			std::cin >> A[i][j];
     	}

	}

}


//matrix display
void matrix :: display()

{
    	int i, j;
	for (i=0; i<x; i++)
	{
		for (j=0; j<y; j++)
		{
			std::cout << "  " << A[i][j];
		}
		std::cout << "\n";
	}
} 


//matrix mult
matrix  matrix :: operator* (int a)
{
	matrix temp(x, y);
	int num,num1, num2;
        for (num1=0; num1<temp.x; num1++)
	{
		for (num2=0; num2<temp.y; num2++)
		{
			temp.A[num1][num2] = 0;
		}
	}
	int i, j;
	for ( i = 0; i < x; i++)
	{
		for ( j = 0; j < y; j++)
		{
			temp.A[i][j] = a * A[i][j];
		}
	}
	return (temp);
}


// MAIN
int main ()

{

matrix s;
        matrix t = s;

	matrix M1, M2;
	M1.read ();
    	M1.input ();
 	M1.display ();
    	M2 = M1*2;
	M2.display();

	return 0;

}
Because neither of your constructors initialize A.

Then on 101-118 you just assume that temp.A is initialized and actually points to allocated memory.
Your constructor is not well-written. Instead of taking care of allocation, you're leaving that up to input.
Can you point what should I change in my design, cause I'm new in this. thanks in advance. Please post with examples.

Alex from Germany.
The default constructor should initialize A to NULL.

The other constructor I assume is specifying the dimensions, in which case it should allocate a 2d buffer of the given dimensions and point A to it. (Best to initialize the values to zero in the matrix).

input() should not be a member of matrix, but should be a function elsewhere.

you need to add methods that allow you to set the size of the matrix and/or grow/shrink the matrix.

you need to write a copy constructor and an assignment operator that does the right thing.

you need to write a destructor that frees the memory referenced by A.
Topic archived. No new replies allowed.