Kỹ thuật lập trình với mảng cấu trúc và truyền cấu trúc cho hàm

Đây là bài 36/43 bài của series môn học Nhập môn lập trình

1. Mảng kiểu dữ liệu cấu trúc

Viết chương trình C++ lưu trữ tọa độ của n điểm trong trục tọa độ Oxy

#include <iostream>
using namespace std;

struct POINT
{
	int x;
	int y;
};

int main() {
	POINT point_array[100];
	int n;//number of points stored
	cout<<"Input number of points to store:";
	cin>>n;
	cout<<"Input "<<n<<" points"<<endl;
	for(int i=0;i<n;i++){
		cout<<"Input "<<i+1<<"th point:";
		cout<<endl<<"x:";cin>>point_array[i].x;
		cout<<"y:";cin>>point_array[i].y;
	}
	cout<<"Output "<<n<<" points"<<endl;
	for(int i=0;i<n;i++){
		cout<<i+1<<"th point:("<<point_array[i].x<<", "<<point_array[i].y<<")"<<endl;
	}
	system("pause");
}
Kết quả
Input number of points to store:5
Input 5 points
Input 1th point:
x:0
y:1
Input 2th point:
x:-1
y:9
Input 3th point:
x:5
y:9
Input 4th point:
x:-3
y:5
Input 5th point:
x:0
y:0
Output 5 points
1th point:(0, 1)
2th point:(-1, 9)
3th point:(5, 9)
4th point:(-3, 5)
5th point:(0, 0)

Viết chương trình lưu thông tin của n sinh viên

#include <iostream>
#include <string>
using namespace std;

struct STUDENT
{
	string id;
	string name;
	string birthday;
	bool gender;//0:female, 1:male
	float math, physics, chemistry;
};


int main() {
	STUDENT student_array[100];
	int n;//number of students stored
	cout<<"Input number of students to store:";
	cin>>n;
	cout<<"Input "<<n<<" students"<<endl;
	for(int i=0;i<n;i++){
		cout<<"Input "<<i+1<<"th student:";
		cin.ignore(100,'\n');
		cout<<endl<<"id:";
		getline(cin, student_array[i].id);
		cout<<"name:";
		getline(cin, student_array[i].name);
		cout<<"birthday:";
		getline(cin, student_array[i].birthday);
		cout<<"gender:";cin>>student_array[i].gender;
		cout<<"math:";cin>>student_array[i].math;
		cout<<"physics:";cin>>student_array[i].physics;
		cout<<"chemistry:";cin>>student_array[i].chemistry;
	}
	cout<<endl<<"===================================="<<endl;
	cout<<"Output "<<n<<" students"<<endl;
	for(int i=0;i<n;i++){
		cout<<i+1<<"th student"<<endl;
		cout<<"id:"<<student_array[i].id<<endl;
		cout<<"name:"<<student_array[i].name<<endl;
		cout<<"birthday:"<<student_array[i].birthday<<endl;
		cout<<"gender:"<<student_array[i].gender<<endl;
		cout<<"math:"<<student_array[i].math<<endl;
		cout<<"physics:"<<student_array[i].physics<<endl;
		cout<<"chemistry:"<<student_array[i].chemistry<<endl<<endl;
	}
	system("pause");
}
Kết quả
Input number of students to store:3
Input 3 students
Input 1th student:
id:01
name:Nguyen Van A
birthday:01/02/2001
gender:1
math:5.6
physics:7.2
chemistry:3.3
Input 2th student:
id:02
name:Nguyen Thi B
birthday:23/02/2001
gender:0
math:9.3
physics:5.8
chemistry:4
Input 3th student:
id:03
name:Nguyen Van C
birthday:05/07/2001
gender:1
math:8.9
physics:6.5
chemistry:7
====================================
Output 3 students
1th student
id:01
name:Nguyen Van A
birthday:01/02/2001
gender:1
math:5.6
physics:7.2
chemistry:3.3

2th student
id:02
name:Nguyen Thi B
birthday:23/02/2001
gender:0
math:9.3
physics:5.8
chemistry:4

3th student
id:03
name:Nguyen Van C
birthday:05/07/2001
gender:1
math:8.9
physics:6.5
chemistry:7

2. Truyền kiểu dữ liệu cấu trúc cho hàm

Truyền kiểu dữ liệu cấu trúc cho hàm

Truyền tham trị: dữ liệu của biến cấu trúc không thay đổi khi kết thúc hàm.

#include <iostream>
using namespace std;

struct POINT
{
	int x;
	int y;
};

void inputPoint(POINT p1){//truyền tham trị
	cout<<"Input point"<<endl;
	cout<<"x:";cin>>p1.x;
	cout<<"y:";cin>>p1.y;
}

void outputPoint(POINT p1){
	cout<<"Output point:("<<p1.x<<", "<<p1.y<<")";
}

int main() {
	POINT p1 = {1, 2};
	inputPoint(p1);
	outputPoint(p1);
	system("pause");
}
Kết quả
Input point
x:99
y:101
Output point:(1, 2)

Nhận xét: POINT p1 ban đầu có tọa độ (1, 2). Gọi hàm inputPoint(p1); để nhập tọa độ cho p1 là (99, 101). Nhưng sau khi kết thúc hàm thì p1 vẫn chỉ lưu tọa độ (1, 2). Rõ ràng, dữ liệu của p1 không thay đổi khi kết thúc hàm.

Truyền tham chiếu: dữ liệu của biến cấu trúc thay đổi khi kết thúc hàm.

#include <iostream>
using namespace std;

struct POINT
{
	int x;
	int y;
};

void inputPoint(POINT &p1){//truyền tham chiếu
	cout<<"Input point"<<endl;
	cout<<"x:";cin>>p1.x;
	cout<<"y:";cin>>p1.y;
}

void outputPoint(POINT p1){
	cout<<"Output point:("<<p1.x<<", "<<p1.y<<")";
}

int main() {
	POINT p1 = {1, 2};
	inputPoint(p1);
	outputPoint(p1);
	system("pause");
}
Kết quả
Input point
x:99
y:101
Output point:(99, 101)

Hàm có kiểu trả về là kiểu dữ liệu cấu trúc

#include <iostream>
using namespace std;

struct POINT
{
	int x;
	int y;
};

POINT inputPoint(){//trả về kiểu POINT
	POINT temp;
	cout<<"Input point"<<endl;
	cout<<"x:";cin>>temp.x;
	cout<<"y:";cin>>temp.y;
	return temp;
}

void outputPoint(POINT p1){
	cout<<"Output point:("<<p1.x<<", "<<p1.y<<")";
}

int main() {
	POINT p1 = {1, 2};
	p1 = inputPoint();
	outputPoint(p1);
	system("pause");
}
Kết quả
Input point
x:99
y:101
Output point:(99, 101)

Hàm inputPoint() có kiểu trả về là kiểu cấu trúc POINT.

Mời bạn đánh giá bài viết
Bài trước và bài sau trong môn học<< Một số kỹ thuật lập trình với kiểu dữ liệu cấu trúc (struct) trong C++Khái niệm con trỏ (pointer) và cách khai báo biến con trỏ trong C++ >>
Chia sẻ trên mạng xã hội:

1 bình luận về bài viết “Kỹ thuật lập trình với mảng cấu trúc và truyền cấu trúc cho hàm

Trả lời

Lưu ý:

1) Vui lòng bình luận bằng tiếng Việt có dấu.

2) Khuyến khích sử dụng tên thật và địa chỉ email chính xác.

3) Mọi bình luận trái quy định sẽ bị xóa bỏ.