Advertisement
thinhckhcmus

Mảng Động ( QBT CODER )

Sep 17th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. <<<<<<<<<<<<<<<<<Mangdong.h>>>>>>>>>>>>>>>>
  2. #pragma once
  3. #include <iostream>
  4. using namespace std;
  5. class CArrayInt
  6. {
  7. public:
  8. CArrayInt(int);
  9. ~CArrayInt();
  10. void input();
  11. void output();
  12. void sort();
  13. int second();
  14. private:
  15. int n;
  16. int *arr;
  17. };
  18. <<<<<<<<<<<<<<<<<<.MangDong.cpp>>>>>>>>>>>>>>>>>>>
  19. #include "CArrayInt.h"
  20. void CArrayInt::input() {
  21. cout << "Nhap kich thuoc cua mang: ";
  22. cin >> n;
  23. arr = new int[n];
  24. for (int i = 0; i < n; i++) {
  25. cout << "Nhap vao phan tu thu " << i << " la: ";
  26. cin >> arr[i];
  27. }
  28. }
  29. void CArrayInt::output() {
  30. for (int i = 0; i < n; i++) {
  31. cout << " " << arr[i];
  32. }
  33. cout << endl;
  34. }
  35. void CArrayInt::sort() {
  36. for (int i = 0; i < n - 1; i++) {
  37. for (int j = i + 1; j < n; j++) {
  38. if (arr[i] > arr[j]) {
  39. swap(arr[i], arr[j]);
  40. }
  41. }
  42. }
  43. }
  44.  
  45. int CArrayInt::second() {
  46. for (int i = 0; i < n - 1; i++) {
  47. for (int j = i + 1; j < n; j++) {
  48. if (arr[i] > arr[j]) {
  49. swap(arr[i], arr[j]);
  50. }
  51. }
  52. }
  53. return arr[n - 2];
  54. }
  55. CArrayInt::CArrayInt(int soluong) {
  56. n = soluong;
  57. arr = new int[n];
  58. for (int i = 0; i < n; i++)
  59. {
  60. arr[i] = 0;
  61. }
  62. }
  63. CArrayInt::~CArrayInt() {
  64. delete[] arr;
  65. }
  66. <<<<<<<<<<<<<<<<<<<<<main.cpp>>>>>>>>>>>>>>>>>>>>
  67. #include"CArrayInt.h"
  68. int main() {
  69. CArrayInt arr(10);
  70. arr.input();
  71. cout << "Arry Not Sort: ";
  72. arr.output();
  73. cout << "Array Sort: ";
  74. arr.sort();
  75. arr.output();
  76. cout << "Seccond: " << arr.second();
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement