0% found this document useful (0 votes)
7 views12 pages

PhanNguyenTuanPhong E5

The document contains several C++ programs that perform various operations on arrays and matrices. These include checking if an array is sorted, sorting parts of an array, merging two arrays, and calculating specific properties of matrices such as diagonal elements and sums of border elements. Each program prompts the user for input and displays the results accordingly.

Uploaded by

tobiop999
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)
7 views12 pages

PhanNguyenTuanPhong E5

The document contains several C++ programs that perform various operations on arrays and matrices. These include checking if an array is sorted, sorting parts of an array, merging two arrays, and calculating specific properties of matrices such as diagonal elements and sums of border elements. Each program prompts the user for input and displays the results accordingly.

Uploaded by

tobiop999
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/ 12

BÀI 1

a)
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Nhap so phan tu n: ";
cin >> n;

int A[n];
cout << "Nhap " << n << " phan tu cho mang A: ";
for (int i = 0; i < n; ++i) {
cin >> A[i];
}
bool isAscending = true;
bool isDescending = true;
for (int i = 1; i < n; ++i) {
if (A[i] < A[i - 1]) {
isAscending = false;
}
if (A[i] > A[i - 1]) {
isDescending = false;
}
}

if (isAscending) {
cout << "Mang A duoc sap xep theo thu tu tang." << endl;
} else if (isDescending) {
cout << "Mang A duoc sap xep theo thu tu giam." << endl;
} else {
cout << "Mang A khong duoc sap xep." << endl;
}

return 0;
}

B)
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int n;
cout << "Nhap so phan tu n: ";
cin >> n;

int A[n];
cout << "Nhap " << n << " phan tu cho mang A: ";
for (int i = 0; i < n; ++i) {
cin >> A[i];
}

int mid = n / 2;

sort(A, A + mid);

if (n % 2 == 0) {
sort(A + mid, A + n, greater<int>());
} else {
sort(A + mid + 1, A + n, greater<int>());
}

cout << "Mang sau khi sap xep nua dau tang, nua sau giam:" << endl;
for (int i = 0; i < n; ++i) {
cout << A[i] << " ";
}
cout << endl;

return 0;
}
c)
#include <iostream>
using namespace std;

int main() {
int n;
cout << "Nhap so phan tu n: ";
cin >> n;

int A[n];
cout << "Nhap " << n << " phan tu cho mang A: ";
for (int i = 0; i < n; ++i) {
cin >> A[i];
}

int maxVal = A[0];


int minVal = A[0];
int maxIndex = 0;
int minIndex = 0;

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


if (A[i] > maxVal) {
maxVal = A[i];
maxIndex = i;
}
if (A[i] < minVal) {
minVal = A[i];
minIndex = i;
}
}

cout << "Khoang cach gia tri lon nhat: " << maxVal - minVal << endl;
cout << "Vi tri cua cap so co khoang cach gia tri lon nhat: (" << minIndex << ", " <<
maxIndex << ")" << endl;

return 0;
}

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

int main() {
int n, m;

cout << "Nhap so phan tu n cho mang A: ";


cin >> n;
int A[n];
cout << "Nhap " << n << " phan tu cho mang A: ";
for (int i = 0; i < n; ++i) {
cin >> A[i];
}

cout << "Nhap so phan tu m cho mang B: ";


cin >> m;
int B[m];
cout << "Nhap " << m << " phan tu cho mang B: ";
for (int i = 0; i < m; ++i) {
cin >> B[i];
}

int C[n + m];


int i = 0, j = 0, k = 0;

while (i < n && j < m) {


if (A[i] < B[j]) {
C[k++] = A[i++];
} else {
C[k++] = B[j++];
}
}

while (i < n) {
C[k++] = A[i++];
}

while (j < m) {
C[k++] = B[j++];
}

cout << "Mang C sau khi tron: ";


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

return 0;
}

BÀI 3
a)
#include <iostream>
using namespace std;

int main() {
int n;
cout << "Nhap cap n cua ma tran A: ";
cin >> n;
int A[n][n];
cout << "Nhap cac phan tu cho ma tran A:" << endl;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << "A[" << i << "][" << j << "] = ";
cin >> A[i][j];
}
}
cout << "Cac phan tu tren duong cheo chinh cua ma tran A:" << endl;
for (int i = 0; i < n; ++i) {
cout << A[i][i] << " ";
}
cout << endl;

return 0;
}
b)
#include <iostream>
using namespace std;

int main() {
int n;
cout << "Nhap cap n cua ma tran A: ";
cin >> n;
int A[n][n];
cout << "Nhap cac phan tu cho ma tran A:" << endl;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << "A[" << i << "][" << j << "] = ";
cin >> A[i][j];
}
}

int sum = 0;

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


sum += A[0][j];
}
for (int j = 0; j < n; ++j) {
sum += A[n-1][j];
}

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


sum += A[i][0];
}

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


sum += A[i][n-1];
}

cout << "Tong gia tri cac phan tu o 4 vien ngoai cung cua ma tran A: " << sum << endl;

return 0;
}

c)
#include <iostream>
using namespace std;

int main() {
int n;
cout << "Nhap cap n cua ma tran A: ";
cin >> n;

int A[n][n];

cout << "Nhap cac phan tu cho ma tran A:" << endl;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << "A[" << i << "][" << j << "] = ";
cin >> A[i][j];
}
}

int sum = 0;
int count = 0;

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


for (int j = 0; j < n - 1 - i; ++j) {
sum += A[i][j];
count++;
}
}
double average = (count > 0) ? static_cast<double>(sum) / count : 0;

cout << "Gia tri trung binh cua cac phan tu ben duoi duong cheo phu: " << average << endl;

return 0;
}

You might also like