Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #include<iostream>
  2. using std::cout; using std::endl; using std::cin;
  3. #include<fstream>
  4. using std::ifstream;
  5. #include<string>
  6. using std::string;
  7. #include<algorithm>
  8. using std::copy; using std::copy_if; using std::swap;
  9. #include<fstream>
  10. using std::ifstream;
  11. #include<utility>
  12. using std::pair; using std::make_pair;
  13.  
  14. #include "lab11_arrays.h"
  15.  
  16. /*
  17. Alex Ralya Lab 11
  18. */
  19.  
  20. template<typename T>
  21. void insert(T* (&ary), size_t &sz, size_t indx, T val){
  22. if (indx >= sz){
  23. sz = indx+1;
  24. T* new_ary = new T[sz]{};
  25. copy(ary, ary+sz, new_ary);
  26. swap(ary,new_ary);
  27. delete [] new_ary;
  28. }
  29. ary[indx] = val;
  30. }
  31.  
  32. size_t fill_from_file(long*& array, string fileName) {
  33.  
  34. ifstream in_file(fileName);
  35.  
  36. if (in_file){
  37.  
  38. string line;
  39. int counter = 0;
  40. size_t sz = 0;
  41.  
  42. while(std::getline(in_file,line)) {
  43. if(counter == 0) {
  44. array = new long[std::stol(line)]
  45. counter++
  46. } else {
  47. sz++;
  48. insert(array, sz, (counter-1), std::stol(line));
  49. }
  50. }
  51.  
  52. return sz;
  53.  
  54. } else {
  55. throw std::runtime_error("File could not be opened!");
  56. }
  57.  
  58. }
  59.  
  60. void print_array(long* array[], size_t sizeOfArray, ostream& oss) {
  61.  
  62. for(int i=0; i < sizeOfArray; i++) {
  63. oss << array[i] << " ";
  64. }
  65.  
  66. cout << oss.str();
  67.  
  68. }
  69.  
  70. size_t concatenate(long*& array1, size_t &sizeOfArray1, long* array2, size_t sizeOfArray2) {
  71.  
  72. int *array1Front = array1;
  73. int *array1Back = array1 + sizeOfArray1;
  74.  
  75. sizeOfArray1 = sizeOfArray1 + sizeOfArray2;
  76. int *newArray = new long[sizeOfArray1 + sizeOfArray2]{};
  77. copy(array1, array1+sizeOfArray1, newArray);
  78. copy(array2, array2 + sizeOfArray2, newArray + sizeOfArray1);
  79. swap(array1, newArray);
  80. delete [] newArray;
  81.  
  82. return sizeOfArray1;
  83.  
  84. }
  85.  
  86. pair<long*, size_t> copy_evens(long array[], size_t sz) {
  87.  
  88. size_t szForNewArray = 0;
  89.  
  90. for(auto element: array) {
  91.  
  92. if(element%2==0){
  93. szForNewArray++;
  94. }
  95.  
  96. }
  97.  
  98. int *newArray = new long[szForNewArray]{};
  99.  
  100. copy_if(array, array + sz, newArray, [](long i) {
  101. return !(i%2);
  102. } );
  103.  
  104. pair<long*, size_t> thePair[newArray, szForNewArray];
  105. return thePair;
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement