Created
August 22, 2018 04:03
-
-
Save hoc081098/939db8dfb45d609be0f5f8592b6ff716 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iterator> | |
#include <iomanip> | |
#include <algorithm> | |
#include <vector> | |
using namespace std; | |
int **inputMatrix(int &m); | |
vector<int> findMaxEachRow(int **a, int m); | |
void outputArray(int *x, int m); | |
void outputMatrix(int **a, int m); | |
void freeMatrix(int **a, int m); | |
void findAndInsertK(vector<int> &x); | |
ostream &operator<<(ostream &os, vector<int> &v) { | |
os << '['; | |
for (int i = 0, size = v.size(); i < size; ++i) { | |
os << v[i]; | |
if (i < size - 1) os << ", "; | |
} | |
return os << ']'; | |
} | |
int main() { | |
int m; | |
// Cau 1: | |
int **a = inputMatrix(m); | |
if (!a) { | |
cout << "Loi cap phat bo nho..."; | |
return 1; | |
} | |
// Cau 2: | |
cout << "Matrix: " << '\n'; | |
outputMatrix(a, m); | |
// Cau 3: | |
vector<int> x = findMaxEachRow(a, m); | |
cout << "Vector x: " << '\n'; | |
std::cout << x << '\n'; | |
sort(x.begin(), x.end()); | |
cout << "Vector x sau khi sap xep: " << '\n'; | |
std::cout << x << '\n'; | |
// Cau 4: | |
findAndInsertK(x); | |
freeMatrix(a, m); | |
return 0; | |
} | |
void findAndInsertK(vector<int> &x) { | |
int k; | |
cout << "Nhap gia tri k = " << '\n'; | |
cin >> k; | |
vector<int>::iterator pK = find(x.begin(), x.end(), k); | |
if (pK != x.end()) { | |
cout << "Vi tri cua " << k << ": " << distance(x.begin(), pK) << '\n'; | |
return; | |
} | |
x.insert(upper_bound(x.begin(), x.end(), k), k); | |
cout << "Vector x sau khi chen " << k << ": " << '\n'; | |
cout << x << '\n'; | |
} | |
void outputArray(int *x, int m) { | |
for (int i = 0; i < m; ++i) { | |
cout << setw(5) << x[i] << " "; | |
} | |
cout << '\n'; | |
} | |
vector<int> findMaxEachRow(int **a, int m) { | |
vector<int> x(m, 0); | |
for (int i = 0; i < m; ++i) { | |
x[i] = *max_element(a[i], a[i] + m); | |
} | |
return x; | |
} | |
void outputMatrix(int **a, int m) { | |
for (int i = 0; i < m; ++i) { | |
outputArray(a[i], m); | |
} | |
cout << '\n'; | |
} | |
void freeMatrix(int **a, int m) { | |
for (int i = 0; i < m; i++) { | |
delete[] a[i]; | |
} | |
delete[] a; | |
} | |
int **inputMatrix(int &m) { | |
do { | |
cout << "Nhap m = "; | |
cin >> m; | |
if (m < 1) cout << "M must be greater than 0" << '\n'; | |
} while (m < 1); | |
int **a = new(nothrow) int *[m]; | |
if (!a) return 0; | |
for (int i = 0; i < m; ++i) { | |
a[i] = new(nothrow) int[m]; | |
if (!a[i]) { | |
for (int j = 0; j < i; j++) { | |
delete[] a[j]; | |
} | |
delete[] a; | |
return 0; | |
} | |
} | |
for (int i = 0; i < m; ++i) { | |
for (int j = 0; j < m; ++j) { | |
cout << " Nhap a[" << i << "][" << j << "] = "; | |
cin >> a[i][j]; | |
} | |
} | |
return a; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment