Answer to Question #346487 in C++ for ooo

Question #346487

You are given two sorted arrays (all elements are in ascending order). You need to merge these

two arrays such that the initial numbers (after complete sorting) are in the first array and the

remaining numbers are in the second array.


1
Expert's answer
2022-05-30T15:13:53-0400
#include <iostream>
void sort(int* array, int count)
{
	int i, j, temp;
	for (i = 0; i < count - 1; i++)
		for (j = 0; j < count - 1 - i; j++)
			if (*(array + j) > *(array + j + 1))
			{
				temp = *(array + j + 1);
				*(array + j + 1) = *(array + j);
				*(array + j) = temp;
			}
}
int main()
{
	const int count = 10;
	int* A;	int* B; int* C; int i;
	A = new int[count];
	B = new int[count];
	C = new int[count * 2];
	std::cout << "Array A:\n";
	for (i = 0; i < count; i++)
	{
		*(A + i) = 1 + i * 2;
		*(C + i) = *(A + i);
		std::cout << *(A + i) << " ";
	}
	std::cout << "\nArray B:\n";
	for (i = 0; i < count; i++)
	{
		*(B + i) = 2 + i * 2;
		*(C + i + count) = *(B + i);
		std::cout << *(B + i) << " ";
	}
	std::cout << "\nArray C:\n";
	for (i = 0; i < count * 2; i++)
		std::cout << *(C + i) << " ";
	std::cout << "\nSorted array C:\n";
	sort(C, count * 2);
	for (i = 0; i < count * 2; i++)
		std::cout << *(C + i) << " ";
	for (i = 0; i < count; i++)
	{
		*(B + i) = *(C + i + count);
		*(A + i) = *(C + i);
	}
	std::cout << "\nArray A:\n";
	for (i = 0; i < count; i++)
		std::cout << *(A + i) << " ";
	std::cout << "\nArray B:\n";
	for (i = 0; i < count; i++)
		std::cout << *(B + i) << " ";
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS