execution error when input matrix .using multi-level pointers

thank everyone!!
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
#include<stdio.h>
#include<conio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
class Matrix
     {
	 private:
		 int Sodong,Socot;
		 int **a;
	 public:
		 Matrix();
		 ~Matrix();
		 void Nhap();
		 void Xuat();
		 Matrix(const Matrix &);
		 void Tong(const Matrix &);
		 void Tich(const Matrix &);
    };
Matrix::Matrix()
{
	Sodong=0;Socot=0;
	a = NULL;
}
Matrix::Matrix(const Matrix  &b)
{
		for(int i= 0;i<Sodong;++i)
			for(int j=0;j<Socot;++j)
				a[i][j]=b.a[i][j];

}
Matrix::~Matrix()
{
	if(a)
	{
		for(int i=0;i<Socot;++i)
		{
			if(a[i]) 
				delete []a[i];
		}
		   delete []a;
	}
}

void Matrix::Nhap()
{
	int i,j;
	do
	{
		cout<<"\nNhap so dong cua Matrix :";
		cin>>Sodong;
	}while(Sodong<1);
	do
	{
		cout<<"\n Nhap so Cot cua Matrix :";
		cin>>Socot;
	}while(Socot<1);
	a=new int*[Sodong];
	if(a==NULL)
	{
		cout<<"\n Khong du bo nho !!";
		return;
	}
	for(i=0;i<Socot;++i)
	{
		a[i]= new int[Socot];
		if(a[i]==NULL)
		{
			cout<<"\n Khong du bo nho !!!";
			return;
		}
	}
	for( i=0;i<Sodong;++i)
		{
		     for( j=0;j<Socot;++j)
		       {
		         	cout<<"\n nhap a["<<i<<","<<j<<"]=";
			        cin>>a[i][j];
					
		       }
		}
}
void Matrix::Xuat()
{
	cout<<"\n Matrix :\n";
	for(int i=0;i<Sodong;++i)
	{
	     for(int j=0;j<Socot;++j)
		 cout<<"   "<<a[i][j];
	     cout<<"\n";
	}
}
void Matrix::Tong(const Matrix &b)
{
	int i,j;
	Matrix kq;
	if((this->Sodong!=b.Sodong)||(this->Socot!=b.Socot))
	{
		cout<<"\n khong cong duoc !!";
		return;
	}
	else
	{
	kq.Sodong=Sodong;
	kq.Socot=Socot;
	kq.a=new int*[Sodong];
	for( i=0;i<Socot;++i)
		(kq.a[i])=new int[Socot];
	for( i=0;i<Sodong;++i)
		for( j=0;j<Socot;++j)
		   kq.a[i][j]=this->a[i][j]+b.a[i][j];
	}
	cout<<"\n tong hai ma tran : ";
	kq.Xuat();
}
void Matrix::Tich(const Matrix &b)
{
	int i,j,k;
	Matrix kq;
	if(this->Socot!=b.Sodong)
	{
		cout<<"\n khong nhan duoc !!";
		return;
	}
	else
	{
	kq.Sodong=Sodong;
	kq.Socot=b.Socot;
	kq.a=new int*[Sodong];
	for(i=0;i<kq.Socot;++i)
		kq.a[i]=new int[Socot];
	for(j=0;j<kq.Sodong;++j)
		for(i=0;i<kq.Socot;++i)
		   {
			   kq.a[i][j]=0;
			   for(k=0;k<b.Socot;++k)
				   kq.a[i][j]+=(this->a[j][k])*(b.a[k][i]);
		   }
	}
		cout<<"\n tich hai ma tran : ";
		kq.Xuat();
}
void main()
{
	Matrix a,b;
	a.Nhap();
	b.Nhap();
	a.Xuat();
	b.Xuat();
	a.Tong(b);
	a.Tich(b);
	getch();
}
Main should be like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
voidint  main()
{
	Matrix a,b;
	a.Nhap();
	b.Nhap();
	a.Xuat();
	b.Xuat();
	a.Tong(b);
	a.Tich(b);
	getch();


return 0;
}


Does the code compile? If not, post the compiler output.

IF the program runs, then try using the debugger - should be easy if there is one in your IDE.

If the debugger is too hard, then you will have to resort to putting lots of cout statements, so you can check the values of variables.

I am sorry I am not offering any other advice, but using the debugger is by far the easiest way to solve this sort of problem. There a lots of others much smarter than me that might be able to see the problem directly.

Good Luck
Last edited on
Topic archived. No new replies allowed.