Hỏi về lỗi ... has stopped working trong chương trình C++

Code này nó cứ báo lỗi … has stopped working ạ … :frowning:

#include <conio.h>
#include <stdio.h>
#include <iostream>
using namespace std;
void InitArray(int **A, int row, int colum);
void AddArray(int **A, int **B, int row, int colum);
void DisplayArray(int **A, int row, int colum);
void Delete(int **A, int row);

void InitArray(int **A, int row, int colum){
	A = new int*[row];
	for (int i = 0; i < row; i++){
		A[i] = new int[colum];
		for (int j = 0; j < colum; j++){
			cout << "Phan tu [" << i << "," << j << "]=";
			cin >> A[i][j];
		}
	}
	return;
}
void DisplayArray(int **A, int row, int colum){
	for (int i = 0; i < row; i++){
		for (int j = 0; j < colum; j++)
			cout << A[i][j] << " ";
		cout << endl;
	}

	return;
}

void main(){
	int **A = NULL, row, colum;
	cout << "so dong= ";
	cin >> row;
	cout << "so cot= ";
	cin >> colum;
	cout << "Khoi tao mang: " << endl;
	InitArray(A, row, colum);
	cout << " show Array: " << endl;
	DisplayArray(A, row, colum);
}

Bạn nên khởi tạo con trỏ để sử dụng mảng một chiều hoặc hai chiều theo cách này:

#include <stdio.h>
#include <iostream>
using namespace std;
int** InitArray(int row, int colum);
void AddArray(int **A, int **B, int row, int colum);
void DisplayArray(int **A, int row, int colum);
void Delete(int **A, int row);

int** InitArray(int row, int colum){
	int **A = new int*[row];
	for (int i = 0; i < row; i++){
		A[i] = new int[colum];
		for (int j = 0; j < colum; j++){
			cout << "Phan tu [" << i << "," << j << "]=";
			cin >> A[i][j];
		}
	}
	return A;
}

void DisplayArray(int **A, int row, int colum){
	for (int i = 0; i < row; i++){
		for (int j = 0; j < colum; j++)
			cout << A[i][j] << " ";
		cout << endl;
	}

	return;
}

int main(){
	
	int **A = NULL, row, colum;
	cout << "so dong= ";
	cin >> row;
	cout << "so cot= ";
	cin >> colum;
	cout << "Khoi tao mang: " << endl;
	A = InitArray(row, colum);
	cout << " show Array: " << endl;
	DisplayArray(A, row, colum);
	
	return 0;
}

Nếu không thì bạn cần sử dụng một con trỏ cấp cao hơn để trỏ trực tiếp đến địa chỉ của con trỏ cấp 2:

#include <stdio.h>
#include <iostream>
using namespace std;
void InitArray(int ***A, int row, int colum);
void AddArray(int **A, int **B, int row, int colum);
void DisplayArray(int **A, int row, int colum);
void Delete(int **A, int row);

void InitArray(int ***A, int row, int colum){
	*A = new int*[row];
	for (int i = 0; i < row; i++){
		*A[i] = new int[colum];
		for (int j = 0; j < colum; j++){
			cout << "Phan tu [" << i << "," << j << "]=";
			cin >> *A[i][j];
		}
	}
	return;
}

void DisplayArray(int **A, int row, int colum){
	for (int i = 0; i < row; i++){
		for (int j = 0; j < colum; j++)
			cout << A[i][j] << " ";
		cout << endl;
	}

	return;
}

int main(){
	
	int **A = NULL, row, colum;
	cout << "so dong= ";
	cin >> row;
	cout << "so cot= ";
	cin >> colum;
	cout << "Khoi tao mang: " << endl;
	InitArray(&A, row, colum);
	cout << " show Array: " << endl;
	DisplayArray(A, row, colum);
	
	return 0;
}

Cách nữa là truyền tham chiếu :smile::

#include <stdio.h>
#include <iostream>
using namespace std;
void InitArray(int& **A, int row, int colum);
......
1 Like

This post was flagged by the community and is temporarily hidden.

1 Like

em đã làm theo anh chỉ nhưng nó chỉ chạy được khi mình nhập row vs colum là 1 vs1 thôi ạ. còn lại nó vẫn báo lỗi anh ạ :frowning::frowning:

Mình làm bình thường.

2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?