30/09/2018, 22:04

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 ạ …

#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);
}
... viết 00:12 ngày 01/10/2018

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;
}
viết 00:17 ngày 01/10/2018

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

#include <stdio.h>
#include <iostream>
using namespace std;
void InitArray(int& **A, int row, int colum);
......
Tao Không Ngu. viết 00:07 ngày 01/10/2018

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

Hoang Nhung viết 00:16 ngày 01/10/2018

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 ạ …

... viết 00:12 ngày 01/10/2018

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

Bài liên quan
0