Khái niệm con trỏ (pointer) và cách khai báo biến con trỏ trong C++
Trong C++, có một khái niệm rất hay nhưng khá khó hiểu khi mới lập trình...
Kết quả sẽ mở trong tab mới, giới hạn trong website này.
Truy cập nhanh
Tìm kiếm gần đây
1. Nhập xuất mảng 2 chiều Sử dụng 2 vòng lặp for để duyệt hàng và cột để nhập xuất mảng hai chiều Kết quả 2. Nhập xuất mảng 2 chiều với hàm Có thể khai báo mảng hai chiều trước, sau đó, dùng hàm nhập mảng thì nhập số dòng và số cột thật […]
#include <iostream>
using namespace std;
int main() {
int temp[3][4];
cout<<"input temp array:"<<endl;
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
cout<<"temp["<<i<<"]["<<j<<"]=";
cin>>temp[i][j];
}
cout<<"\n";
}
cout<<"output temp array:"<<endl;
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
cout<<temp[i][j]<<" ";
}
cout<<"\n";
}
system("pause");
}
input temp array:
temp[0][0]=1
temp[0][1]=2
temp[0][2]=5
temp[0][3]=9
temp[1][0]=1
temp[1][1]=5
temp[1][2]=7
temp[1][3]=2
temp[2][0]=0
temp[2][1]=1
temp[2][2]=5
temp[2][3]=7
output temp array:
1 2 5 9
1 5 7 2
0 1 5 7
Có thể khai báo mảng hai chiều trước, sau đó, dùng hàm nhập mảng thì nhập số dòng và số cột thật sự của mảng. Số dòng và số cột lúc khai báo phải lớn hơn hoặc bằng số dòng và số cột thật sự lúc nhập, nếu không sẽ có thể gây ra lỗi dữ liệu rác.
#include <iostream>
using namespace std;
#define MAXR 100 //max column
#define MAXC 100 //max row
void inputArray(int a[][MAXC], int &m, int &n){
cout<<"input number of row:";
cin>>m;
cout<<"input number of column:";
cin>>n;
cout<<"input two dimensional array "<<m<<"x"<<n<<":"<<endl;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cout<<"a["<<i<<"]["<<j<<"]=";
cin>>a[i][j];
}
cout<<"\n";
}
}
void outputArray(int a[][MAXC], int m, int n){
cout<<"output two dimensional array "<<m<<"x"<<n<<":"<<endl;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
}
int main() {
int temp[MAXR][MAXC];
int m, n;
inputArray(temp, m, n);
outputArray(temp, m, n);
system("pause");
}
input number of row:2
input number of column:3
input two dimensional array 2x3:
a[0][0]=1
a[0][1]=3
a[0][2]=1
a[1][0]=4
a[1][1]=5
a[1][2]=3
output two dimensional array 2x3:
1 3 1
4 5 3
Các biến lưu trữ số dòng và số cột thật sự phải truyền tham chiếu để sau khi ra khỏi hàm vẫn còn lưu được các giá trị này để xuất hoặc tính toán trên mảng.
Yêu cầu: Tìm xem phần tử x có nằm trong ma trận a kích thước mxn hay không?
Ý tưởng: Duyệt từng phần của ma trận a. Nếu phần tử đang xét bằng x thì trả về có (1), ngược lại trả về không có (0).
#include <iostream>
using namespace std;
int FindX(int a[2][3], int x)
{
int i, j;
for (i=0; i<2; i++){
for (j=0; j<3; j++){
if (a[i][j] == x){
return 1;
}
}
}
return 0;
}
int main() {
int temp[2][3]={{0, 5, 1},{8, -1, 2}};
//find -1 in temp array
int result = FindX(temp, -1);
if(result == 1){
cout<<"Find -1 in temp array";
}else{
cout<<"Not find -1 in temp array";
}
system("pause");
}
Find -1 in temp arrayYêu cầu: Cho trước ma trận a, kích thước mxn. Tính tổng các phần tử trong mảng.
Ý tưởng: Duyệt từng phần của ma trận a rồi cộng dồn các phần tử.
#include <iostream>
using namespace std;
int sumArray(int a[2][3])
{
int i, j, sum=0;
for (i=0; i<2; i++){
for (j=0; j<3; j++){
sum = sum + a[i][j];
}
}
return sum;
}
int main() {
int temp[2][3]={{0, 5, 1},{8, -1, 2}};
//sum elements in temp array
int result = sumArray(temp);
cout<<"Sum elements in temp array = "<<result;
system("pause");
}
Sum elements in temp array = 15Yêu cầu: Cho trước ma trận a, kích thước mxn. Tìm giá trị lớn nhất trong ma trận a (gọi là max).
Ý tưởng: Giả sử giá trị max hiện tại là giá trị phần tử đầu tiên a[0][0]. Lần lượt kiểm tra các phần tử còn lại để cập nhật max.
#include <iostream>
using namespace std;
int FindMax(int a[2][3])
{
int i, j, max=a[0][0];
for (i=0; i<2; i++){
for (j=0; j<3; j++){
if(a[i][j] > max){
max = a[i][j];
}
}
}
return max;
}
int main() {
int temp[2][3]={{0, 5, 1},{8, -1, 2}};
//find max in temp array
int result = FindMax(temp);
cout<<"Max in temp array = "<<result;
system("pause");
}
Max in temp array = 8