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 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#include <iostream>
#include <limits>
#include <algorithm>
using namespace std;
using uInt = unsigned int;
int main()
{
constexpr uInt MAXSIZE{ 32 };
//int even[MAXSIZE]{}; // <--- These 3 lines for normal run.
//int odd[MAXSIZE]{};
//int evenIdx{}, oddIdx{}, mergeIdx{};
int even[MAXSIZE]{ 2, 22, 44, 4, 6, 66 }; // <--- These 3 lines used for testing. Switch comments when finished.
int odd[MAXSIZE]{ 11, 1, 33, 3, 55, 5 };
int evenIdx{ 6 }, oddIdx{ 6 }, mergeIdx{};
int merge[MAXSIZE * 2]{};
int inVAlue{};
// <--- Uncomment for normal run.[/b]
//while (std::cout << "Input an integer, enter 0 to terminate: " && (std::cin >> inVAlue) && inVAlue != 0)
//{
// //add to even list
// if (!(inVAlue % 2))
// {
// even[evenIdx++] = inVAlue;
// }
// //add to odd list
// else
// {
// odd[oddIdx++] = inVAlue;//odd[] = invalue;
// }
//}
std::sort(even, even + evenIdx);
std::sort(odd, odd + oddIdx);
int b = 0;
std::cout << "\n Unmerged arrays:\n ";
while (even[b] != 0)
{
cout << even[b] << ' ';
merge[mergeIdx++] = even[b++];
}
std::cout << "\n ";
b = 0;
while (odd[b] != 0)
{
cout << odd[b] << ' ';
merge[mergeIdx++] = odd[b++];
}
std::cout << "\n\n Merged arrays:\n ";
for (uInt idx = 0; idx < mergeIdx; idx++)
{
std::cout << merge[idx] << ' ';
}
std::cout << '\n';
// A fair C++ replacement for "system("pause")". Or a way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
}
|