Question 2

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.
Write all numbers into array C, use a standard sorting algorithm. There are many. A few moments' search will find them.
1
2
3
4
5
6
7
8
9
void merge( int *a,  int *b, int *c){
   
    for(int i = 0; i < 0; i++){
       for(int j = num_of_elements; j >= 0; j--){
            c[i] = b[num_of_elements] + a[i]; 
       }
    } 

}


something along those lines, check my parameters as i think i did them wrong
This is the "user defined function". :)

1
2
3
4
5
6
7
void merge( const int ( &a )[M], const int ( &b )[N], int ( &c )[M+N] )
{
   std::merge( std::begin( a ), std::end( a ),  
               std::reverse_iterator<const int *>( std::end( b ) ),
               std::reverse_iterator<const int *>( std::begin( b ) ),
               std::begin( c ) );
}  
Last edited on
I am not getting it sorry...
You should probably make your own homework. At least show us what you have and where your troubles lie.
I am not completing my homework but I am preparing for my exam... I will submit what I did in a while.
Here is a compiled example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <algorithm>
#include <numeric>
#include <iterator>

template <size_t M, siize_t N>
void merge( const int ( &a )[M], const int ( &b )[N], int ( &c )[M+N] )
{
   std::merge( std::begin( a ), std::end( a ),  
               std::reverse_iterator<const int *>( std::end( b ) ),
               std::reverse_iterator<const int *>( std::begin( b ) ),
               std::begin( c ) );
}  

void ( *out )( int ) = [] ( int x ) { std::cout << x << ' '; };

int main()
{
   const int M = 10;
   const int 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 );
}   
Last edited on
#include <algorithm>
#include <numeric>
#include <iterator>

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( const int *a, int m, const int *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
const int M = 10;
const int 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.
Thank you to all of you..
Topic archived. No new replies allowed.