Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. //task 1
  4. using namespace std;
  5. const int SIZE = 100;
  6. /*
  7. void alg(int matrix[][SIZE], int size)
  8. {
  9. int counter = 1;
  10. int rows = 0, colums = 1;
  11.  
  12.  
  13. while (rows<=size-1)
  14. {
  15. for (int i = rows, j = 0; i >= 0; i--, j++)
  16. {
  17. matrix[i][j] = counter;
  18. counter++;
  19. }
  20. rows++;
  21. }
  22.  
  23. rows--;
  24.  
  25. while (colums<=size-1)
  26. {
  27. for (int i = rows,j = colums; j<=size-1 ; i--,j++)
  28. {
  29. matrix[i][j] = counter;
  30. counter++;
  31. }
  32. colums++;
  33. }
  34. }
  35.  
  36. void output(int matrix[][SIZE], int &size)
  37. {
  38. alg(matrix, size);
  39. for (int i = 0; i < size; i++)
  40. {
  41. for (int j = 0; j < size; j++)
  42. {
  43. cout << matrix[i][j] << " ";
  44. }
  45. cout << std::endl;
  46. }
  47. }
  48. int main()
  49. {
  50. int matrix[SIZE][SIZE] = { 0 };
  51. int size = 0;
  52. cout << "Enter the size of the matrix: ";
  53. cin >> size;
  54. output(matrix,size);
  55. }*/
  56. ////////////
  57. //task 2
  58.  
  59. bool check(char arr[])
  60. {
  61. for (int i = 0; arr[i+1]!='\0' ; i++)
  62. {
  63. if (arr[i + 1] < arr[i])
  64. {
  65. return 0;
  66. }
  67. }
  68. return 1;
  69. }
  70.  
  71. void merge(char arr1[], char arr2[],char combinedArr[])
  72. {
  73. for (int i = 0, j = 0, k = 0; arr1[i] != '\0' || arr2[j] != '\0';)
  74. {
  75. if (arr2[j] != '\0' && arr1[i] != '\0')
  76. {
  77. if (arr1[i] > arr2[j])
  78. {
  79. combinedArr[k] = arr2[j];
  80. j++;
  81. k++;
  82. }
  83. else if (arr1[i] < arr2[j])
  84. {
  85. combinedArr[k] = arr1[i];
  86. i++;
  87. k++;
  88. }
  89. else
  90. {
  91. combinedArr[k] = arr1[i];
  92. combinedArr[k + 1] = arr2[j];
  93. i++;
  94. j++;
  95. k += 2;
  96. }
  97. }
  98. else if (arr2[j] == '\0' && arr1[i] != '\0')
  99. {
  100. combinedArr[k] = arr1[i];
  101. i++;
  102. k++;
  103. }
  104. else
  105. {
  106. combinedArr[k] = arr2[j];
  107. j++;
  108. k++;
  109. }
  110. }
  111. }
  112.  
  113. void output(char arr1[], char arr2[], char combinedArr[])
  114. {
  115. merge(arr1,arr2,combinedArr);
  116. for (int i = 0; combinedArr[i]!='\0' ; i++)
  117. {
  118. cout << combinedArr[i];
  119. }
  120. }
  121. int main()
  122. {
  123. char arr1[SIZE], arr2[SIZE], combinedArr[SIZE] = { 0 };
  124. cout << "Enter a sorted array: ";
  125. do
  126. {
  127. cin >> arr1;
  128. } while (!check(arr1));
  129.  
  130. cout << "Enter a second sorted array: ";
  131. do
  132. {
  133. cin >> arr2;
  134. } while (!check(arr2));
  135. output(arr1, arr2, combinedArr);
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement