Suppose A, B, C are arrays of integers of size M, N, and M + N respectively. The
numbers in array A appear in ascending order while the numbers in array B appear in descending order. Write a user defined function in C++ to produce third array C by merging arrays A and B in ascending order. Use A, B and C as arguments in the function.
#include <iostream>
#include <algorithm>
#include <numeric>
#include <iterator>
template <size_t M, siize_t N>
void merge( constint ( &a )[M], constint ( &b )[N], int ( &c )[M+N] )
{
std::merge( std::begin( a ), std::end( a ),
std::reverse_iterator<constint *>( std::end( b ) ),
std::reverse_iterator<constint *>( std::begin( b ) ),
std::begin( c ) );
}
void ( *out )( int ) = [] ( int x ) { std::cout << x << ' '; };
int main()
{
constint M = 10;
constint N = 20;
int a[M];
int b[N];
int c[M+N];
std::iota( std::begin( a ), std::end( a ), M / 2 );
std::iota( std::begin( b ), std::end( b ), 0 );
std::reverse( std::begin( b ), std::end( b ) );
for ( auto i : a ) out( i );
std::cout << std::endl;
for ( auto i : b ) out( i );
std::cout << std::endl;
merge( a, b, c );
for ( auto i : c ) out( i );
std::cout << std::endl;
return ( 0 );
}
I am just a beginner in TurboC++ ..I dont know the above mentioned Headers....
I know the following sorting algorithms..
1. Bubblesort
2. Selection sort
Well I only demostrated that now there are two C++ languages after adopting the C++ 2011 Standard. And the C++ which you will use to write your task differs much from the C++ I used in my example.
You should declare a function the following way
void merge( constint *a, int m, constint *b, int n, int *c, int k );
and in main you will call it the following way
1 2 3 4 5 6 7 8 9 10
constint M = 10;
constint N = 20;
int a[M];
int b[N];
int c[M+N];
// here arrays a and b are filled with values or it can be done in declarations of the arrays
merge( a, M, b, N, c, M+N );
Your task is to write the algorithm for merging arrays.