0% found this document useful (0 votes)
18 views9 pages

Thanh Trong

Đa

Uploaded by

trongp61
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views9 pages

Thanh Trong

Đa

Uploaded by

trongp61
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Họ tên : Phạm Thành Trọng MSSV:23146392

BUỔI THỰC HÀNH 2


BÀI 1:
#include <iostream>
using namespace std;

int main() {
int giatri[3][3];
int value, found = 0;

cout << "Nhap gia tri cho mang 2 chieu 3x3:\n";


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << "giatri[" << i << "][" << j << "] = ";
cin >> giatri[i][j];
}
}

cout << "\nMang 2 chieu vua nhap:\n";


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << giatri[i][j] << " ";
}
cout << endl;
}
BÀI 2:
#include <iostream>
using namespace std;

int main() {
const int n = 10;
int songuyen [n];

cout << "Nhap 10 so nguyen: " << endl;


for (int i = 0; i < n; i++) {
cin >> songuyen [i];
}

cout << "Mang vua nhap: ";


for (int i = 0; i < n; i++) {
cout << songuyen [i] << " ";
}
cout << endl;

cout << "Mang theo thu tu nguoc lai: ";


for (int i = n - 1; i >= 0; i--) {
cout << songuyen [i] << " ";
}
cout << endl;

int minVal = songuyen [0], maxVal = songuyen [0], sum = 0;


for (int i = 1; i < n; i++) {
if (songuyen [i] < minVal) {
minVal = songuyen [i];
}
if (songuyen [i] > maxVal) {
maxVal = songuyen [i];
}
sum += songuyen [i];
}
double avg = (double)sum / n;

cout << "Gia tri nho nhat: " << minVal << endl;
cout << "Gia tri lon nhat: " << maxVal << endl;
cout << "Gia tri trung binh: " << avg << endl;

return 0;
}

do {
cout << "\nNhap gia tri can tim: ";
cin >> value;
found = 0;
BÀI 3:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
int main() {
const int size = 20;
double M[size];
double N[size];

srand(static_cast<unsigned int>(time(0)));
for (int i = 0; i < size; i++) {
M[i] = rand() % 151;
}

cout << "Mang nhiet do M:\n";


for (int i = 0; i < size; i++) {
cout << M[i] << " ";
}
cout << endl;

N[0] = (M[0] + M[1]) / 2;

for (int i = 1; i < size - 1; i++) {


N[i] = (M[i - 1] + M[i] + M[i + 1]) / 3;
}

N[size - 1] = (M[size - 2] + M[size - 1]) / 2;


cout << fixed << setprecision(2);
cout << "Gia tri sau khi loc trung binh N:\n";
for (int i = 0; i < size; i++) {
cout << N[i] << " ";
}
cout << endl;

double newValue;
cout << "Nhap gia tri moi (0 - 150): ";
cin >> newValue;

for (int i = size - 1; i > 0; i--) {


M[i] = M[i - 1];
}
M[0] = newValue;
N[0] = (M[0] + M[1]) / 2;

for (int i = 1; i < size - 1; i++) {


N[i] = (M[i - 1] + M[i] + M[i + 1]) / 3;
}

N[size - 1] = (M[size - 2] + M[size - 1]) / 2;

cout << "Mang nhiet do M sau khi cap nhat:\n";


for (int i = 0; i < size; i++) {
cout << M[i] << " ";
}
cout << endl;

cout << "Gia tri sau khi loc trung binh N sau khi cap nhat:\n";
for (int i = 0; i < size; i++) {
cout << N[i] << " ";
}
cout << endl;

return 0;
}
BÀI 4:

#include <iostream>
using namespace std;

struct Product {
string maSP;
int soLuong;
};

int main() {
int n;
cout << "Nhap so luong san pham: ";
cin >> n;

Product *products = new Product[n];

for (int i = 0; i < n; i++) {


cout << "Nhap thong tin san pham thu " << i+1 << ":" << endl;
cout << "Ma san pham: ";
cin >> products[i].maSP;
cout << "So luong: ";
cin >> products[i].soLuong;
}

cout << "\nDanh sach san pham:" << endl;


for (int i = 0; i < n; i++) {
cout << "Ma SP: " << products[i].maSP << ", So luong: " <<
products[i].soLuong << endl;
}

while (true) {
int choice;
cout << "\nChon chuc nang (1: Nhap hang, 2: Xuat hang, 0: Thoat): ";
cin >> choice;
if (choice == 0) break;

string maSP;
int soLuong;
cout << "Nhap ma san pham: ";
cin >> maSP;
cout << "Nhap so luong: ";
cin >> soLuong;

bool found = false;


for (int i = 0; i < n; i++) {
if (products[i].maSP == maSP) {
found = true;
if (choice == 1) {
products[i].soLuong += soLuong;
} else if (choice == 2) {
if (soLuong <= products[i].soLuong) {
products[i].soLuong -= soLuong;
} else {
cout << "Khong du so luong hang." << endl;
}
}
break;
}
}

if (choice == 1 && !found) {


Product newProduct = {maSP, soLuong};
Product *temp = new Product[n+1];
for (int i = 0; i < n; i++) {
temp[i] = products[i];
}
temp[n] = newProduct;
delete[] products;
products = temp;
n++;
}
}

delete[] products;

return 0;
}

You might also like