Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- //task 1
- using namespace std;
- const int SIZE = 100;
- /*
- void alg(int matrix[][SIZE], int size)
- {
- int counter = 1;
- int rows = 0, colums = 1;
- while (rows<=size-1)
- {
- for (int i = rows, j = 0; i >= 0; i--, j++)
- {
- matrix[i][j] = counter;
- counter++;
- }
- rows++;
- }
- rows--;
- while (colums<=size-1)
- {
- for (int i = rows,j = colums; j<=size-1 ; i--,j++)
- {
- matrix[i][j] = counter;
- counter++;
- }
- colums++;
- }
- }
- void output(int matrix[][SIZE], int &size)
- {
- alg(matrix, size);
- for (int i = 0; i < size; i++)
- {
- for (int j = 0; j < size; j++)
- {
- cout << matrix[i][j] << " ";
- }
- cout << std::endl;
- }
- }
- int main()
- {
- int matrix[SIZE][SIZE] = { 0 };
- int size = 0;
- cout << "Enter the size of the matrix: ";
- cin >> size;
- output(matrix,size);
- }*/
- ////////////
- //task 2
- bool check(char arr[])
- {
- for (int i = 0; arr[i+1]!='\0' ; i++)
- {
- if (arr[i + 1] < arr[i])
- {
- return 0;
- }
- }
- return 1;
- }
- void merge(char arr1[], char arr2[],char combinedArr[])
- {
- for (int i = 0, j = 0, k = 0; arr1[i] != '\0' || arr2[j] != '\0';)
- {
- if (arr2[j] != '\0' && arr1[i] != '\0')
- {
- if (arr1[i] > arr2[j])
- {
- combinedArr[k] = arr2[j];
- j++;
- k++;
- }
- else if (arr1[i] < arr2[j])
- {
- combinedArr[k] = arr1[i];
- i++;
- k++;
- }
- else
- {
- combinedArr[k] = arr1[i];
- combinedArr[k + 1] = arr2[j];
- i++;
- j++;
- k += 2;
- }
- }
- else if (arr2[j] == '\0' && arr1[i] != '\0')
- {
- combinedArr[k] = arr1[i];
- i++;
- k++;
- }
- else
- {
- combinedArr[k] = arr2[j];
- j++;
- k++;
- }
- }
- }
- void output(char arr1[], char arr2[], char combinedArr[])
- {
- merge(arr1,arr2,combinedArr);
- for (int i = 0; combinedArr[i]!='\0' ; i++)
- {
- cout << combinedArr[i];
- }
- }
- int main()
- {
- char arr1[SIZE], arr2[SIZE], combinedArr[SIZE] = { 0 };
- cout << "Enter a sorted array: ";
- do
- {
- cin >> arr1;
- } while (!check(arr1));
- cout << "Enter a second sorted array: ";
- do
- {
- cin >> arr2;
- } while (!check(arr2));
- output(arr1, arr2, combinedArr);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement