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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <iostream>
#include <cstdlib>
using namespace std;
bool die( const string & msg ){
cerr <<endl <<"Fatal error: " <<msg <<endl;
exit( EXIT_FAILURE );
}
bool sorted( const unsigned a[], unsigned elements ){
for (unsigned i = 0; i < elements; i++){
if (a[i] > a[i+1]){
return false;
break;
}}
return true;
}
void merge(unsigned combo[], const unsigned a[], unsigned aElements, const unsigned b[], unsigned bElements ){
if (sorted(a, aElements) && sorted(b, bElements)){
for (unsigned i = 0; i < aElements; i++){
combo[i] = a[i];
}
for (unsigned k = 0; k < bElements; k++){
if (b[k] > b[k+1]){
combo[k] += b[k];
}
}
}
else{
die("One or more arrays not sorted");
}
}
int main(){
unsigned aElements = 4;
unsigned bElements = 4;
unsigned a[] = {0, 1, 2, 3};
unsigned b[] = {3, 4, 5, 6};
unsigned combo[8] = {}
merge(combo, a, aElements, b, bElements);
return 0;
}
|