Skip to content

Instantly share code, notes, and snippets.

@lavantien
Created November 17, 2018 19:41
Show Gist options
  • Save lavantien/e07c8a44560dccd5735039848f33cf90 to your computer and use it in GitHub Desktop.
Save lavantien/e07c8a44560dccd5735039848f33cf90 to your computer and use it in GitHub Desktop.
A demo program for manipulate arrays of integers.
// Compile with std=c++11 or above to avoid conflict.
// gl_ : global variables. us_ : user defined functions. :: : access global variables.
// Tested! All modules work 100%.
#include <iostream>
#include <random>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>
static const int gl_ARR_LEN_MAX = 1000;
static const int gl_RAND_MIN = -1000000;
static const int gl_RAND_MAX = 1000000;
static int gl_n = 0;
static int gl_arr[gl_ARR_LEN_MAX];
static bool gl_primeCheck[gl_RAND_MAX + 1];
static void func01();
static void func02();
static void func03();
static void func04();
static void func05();
static void func06();
static void func07();
static void func08();
static void func09();
static void func10();
static void func11();
static void func12();
static void funcDefault();
static void randomArray(int arr[], const int n, const int minValue, const int maxValue);
static void sieveOfEratosthenes(bool primeCheck[], const int n);
static void us_sort(int arr[], const int n, const std::string order); // order : {"asc", "desc", "even pos asc, skip ood pos", "prime desc, skip composite", "positive desc, negative asc"}
static void us_swap(int &a, int &b);
static void clearConsole(); // Works only on Windows OS
static void pauseConsole();
int main() {
sieveOfEratosthenes(gl_primeCheck, gl_RAND_MAX + 1);
int keyPressed = 0;
do {
clearConsole();
std::cout << "0. Exit the program.\n";
std::cout << "1. Enter values for the array.\n";
std::cout << "2. Create random values for the array.\n";
std::cout << "3. Searching using Linear Search.\n";
std::cout << "4. Ascending sort using Selection Sort.\n";
std::cout << "5. Descending sort using Interchange Sort (Bubble Sort).\n";
std::cout << "6. Searching using Binary Search.\n";
std::cout << "7. Sort for even positions ascending, the rest remains their positions.\n";
std::cout << "8. Sort for primes descending, the rest remains their positions.\n";
std::cout << "9. Show 3 biggest values in the array.\n";
std::cout << "10. Show 5 smallest values in the array.\n";
std::cout << "11. Merging 2 arrays into an ascending array.\n";
std::cout << "12. Sort for <negatives descending> <0> <positives ascending>.\n";
std::cout << "\nCurrent array (" << gl_n << "): ";
if (gl_n == 0) {
std::cout << "Empty array!";
} else {
for (int i = 0; i < gl_n; ++i) {
std::cout << gl_arr[i] << ' ';
}
}
std::cout << "\n\n>>> Enter your choice (number): ";
std::cin >> keyPressed;
switch (keyPressed) {
case 0:
break;
case 1:
func01();
break;
case 2:
func02();
break;
case 3:
func03();
break;
case 4:
func04();
break;
case 5:
func05();
break;
case 6:
func06();
break;
case 7:
func07();
break;
case 8:
func08();
break;
case 9:
func09();
break;
case 10:
func10();
break;
case 11:
func11();
break;
case 12:
func12();
break;
default:
funcDefault();
break;
}
} while (keyPressed != 0);
std::cout << "\nThe program has been terminated!\n";
pauseConsole();
return 0;
}
static void func01() {
std::cout << "\nEnter the number of elements in the array: ";
std::cin >> gl_n;
std::cout << "\nEnter the elements:\n";
for (int i = 0; i < gl_n; ++i) {
std::cin >> gl_arr[i];
}
pauseConsole();
}
static void func02() {
std::cout << "\nEnter the number of elements in the array: ";
std::cin >> gl_n;
randomArray(gl_arr, gl_n, gl_RAND_MIN, gl_RAND_MAX);
std::cout << "\nArray now contains random values!\n";
pauseConsole();
}
static void func03() {
std::cout << "\nEnter number x: ";
int x;
std::cin >> x;
int pos = -1;
for (int i = 0; i < gl_n; ++i) {
if (x == gl_arr[i]) {
pos = i;
}
}
if (pos == -1) {
std::cout << "\nThere is no x value in the array!\n";
} else {
std::cout << "\nx's position: " << pos << "\n";
}
pauseConsole();
}
static void func04() {
us_sort(gl_arr, gl_n, "asc");
std::cout << "\nThe array is sorted as required!\n";
pauseConsole();
}
static void func05() {
for (int i = 0; i < gl_n - 1; ++i) {
for (int j = 0; j < gl_n - i - 1; ++j) {
if (gl_arr[j] < gl_arr[j + 1]) {
us_swap(gl_arr[j], gl_arr[j + 1]);
}
}
}
std::cout << "\nThe array is sorted as required!\n";
pauseConsole();
}
static void func06() {
us_sort(gl_arr, gl_n, "asc");
std::cout << "\nCurrent array (" << gl_n << "): ";
if (gl_n == 0) {
std::cout << "Empty array!";
} else {
for (int i = 0; i < gl_n; ++i) {
std::cout << gl_arr[i] << ' ';
}
}
std::cout << "\n\nEnter number x: ";
int x;
std::cin >> x;
int l = 0;
int r = gl_n - 1;
int pos = -1;
while (l <= r) {
int m = l + (r - 1) / 2;
if (gl_arr[m] == x) {
pos = m;
break;
}
if (gl_arr[m] < x) {
l = m + 1;
} else {
r = m - 1;
}
}
if (pos == -1) {
std::cout << "\nThere is no x value in the array!\n";
} else {
std::cout << "\nx's position: " << pos << "\n";
}
pauseConsole();
}
static void func07() {
us_sort(gl_arr, gl_n, "even pos asc, skip ood pos");
std::cout << "\nThe array is sorted as required!\n";
pauseConsole();
}
static void func08() {
us_sort(gl_arr, gl_n, "prime desc, skip composite");
std::cout << "\nThe array is sorted as required!\n";
pauseConsole();
}
static void func09() {
int* arr = new int[gl_n];
int n = gl_n;
std::memcpy(arr, gl_arr, gl_n * sizeof *gl_arr);
us_sort(arr, n, "desc");
std::cout << "\n3 elements which have largest value: ";
n = n < 3 ? n : 3;
for (int i = 0; i < n - 1; ++i) {
std::cout << arr[i] << ", ";
}
if (n != 0) {
std::cout << arr[n - 1] << '\n';
}
delete[] arr;
pauseConsole();
}
static void func10() {
int* arr = new int[gl_n];
int n = gl_n;
std::memcpy(arr, gl_arr, gl_n * sizeof *gl_arr);
us_sort(arr, n, "asc");
std::cout << "\n5 elements which have smallest value: ";
n = n < 5 ? n : 5;
for (int i = 0; i < n - 1; ++i) {
std::cout << arr[i] << ", ";
}
if (n != 0) {
std::cout << arr[n - 1] << '\n';
}
delete[] arr;
pauseConsole();
}
static void func11() {
int* gl_arr = new int[gl_n]; // Shadowing the global variable.
std::memcpy(gl_arr, ::gl_arr, gl_n * sizeof *::gl_arr);
us_sort(gl_arr, gl_n, "asc");
std::cout << "\nArray 1 which has been entered (" << gl_n << "): ";
int maxValue = gl_RAND_MIN;
if (gl_n == 0) {
std::cout << "Empty array!";
} else {
for (int i = 0; i < gl_n; ++i) {
std::cout << gl_arr[i] << ' ';
if (std::abs(gl_arr[i]) > maxValue) {
maxValue = gl_arr[i];
}
}
}
int* arr = new int[gl_n];
int n = gl_n;
randomArray(arr, n, -maxValue, maxValue);
us_sort(arr, n, "asc");
std::cout << "\n\nArray 2 which has been random (" << n << "): ";
if (n == 0) {
std::cout << "Empty array!";
} else {
for (int i = 0; i < n; ++i) {
std::cout << arr[i] << ' ';
}
}
int nBig = n + gl_n;
int* arrBig = new int[nBig];
std::memcpy(arrBig, gl_arr, gl_n * sizeof *gl_arr);
std::memcpy(arrBig + gl_n, arr, n * sizeof *arr);
us_sort(arrBig, nBig, "asc");
std::cout << "\n\nSynthetic array of the 2 above (" << nBig << "): ";
if (nBig == 0) {
std::cout << "Empty array!";
} else {
for (int i = 0; i < nBig; ++i) {
std::cout << arrBig[i] << ' ';
}
}
std::cout << '\n';
delete[] gl_arr;
delete[] arr;
delete[] arrBig;
pauseConsole();
}
static void func12() {
us_sort(gl_arr, gl_n, "positive desc, negative asc");
std::cout << "\nThe array is sorted as required!\n";
pauseConsole();
}
static void funcDefault() {
std::cout << "\nPlease enter a valid number!\n\n";
}
static void randomArray(int arr[], const int n, const int minValue, const int maxValue) {
std::mt19937 rng;
rng.seed(std::random_device()());
std::uniform_int_distribution<std::mt19937::result_type> dist(minValue, maxValue);
for (int i = 0; i < n; ++i) {
arr[i] = dist(rng);
}
}
static void sieveOfEratosthenes(bool primeCheck[], const int n) {
std::memset(primeCheck, true, gl_RAND_MAX * sizeof *primeCheck);
int sqrtN = (int)std::sqrt(n);
for (int i = 2; i <= sqrtN; ++i) {
if (primeCheck[i]) {
for (int j = i * i; j <= n; j += i) {
primeCheck[j] = false;
}
}
}
}
static void us_sort(int arr[], const int n, const std::string order) {
if (order == "asc") {
int posMin = 0;
for (int i = 0; i < n; ++i) {
posMin = i;
for (int j = i + 1; j < n; ++j) {
if (arr[j] < arr[posMin]) {
posMin = j;
}
}
us_swap(arr[posMin], arr[i]);
}
} else if (order == "desc") {
int posMax = 0;
for (int i = 0; i < n; ++i) {
posMax = i;
for (int j = i + 1; j < n; ++j) {
if (arr[j] > arr[posMax]) {
posMax = j;
}
}
us_swap(arr[posMax], arr[i]);
}
} else if (order == "even pos asc, skip ood pos") {
int nEvenPos = 0;
for (int i = 0; i < gl_n; ++i) {
if (i % 2 == 0) {
++nEvenPos;
}
}
int* arrEvenPos = new int[nEvenPos];
int iE = 0;
for (int i = 0; i < gl_n; ++i) {
if (i % 2 == 0) {
arrEvenPos[iE++] = gl_arr[i];
}
}
us_sort(arrEvenPos, nEvenPos, "asc");
iE = 0;
for (int i = 0; i < gl_n; ++i) {
if (i % 2 == 0) {
gl_arr[i] = arrEvenPos[iE++];
}
}
delete[] arrEvenPos;
} else if (order == "prime desc, skip composite") {
int nPrime = 0;
for (int i = 0; i < gl_n; ++i) {
if (gl_arr[i] >= 2 && gl_primeCheck[gl_arr[i]]) {
++nPrime;
}
}
int* arrPrime = new int[nPrime];
int iP = 0;
for (int i = 0; i < gl_n; ++i) {
if (gl_arr[i] >= 2 && gl_primeCheck[gl_arr[i]]) {
arrPrime[iP++] = gl_arr[i];
}
}
us_sort(arrPrime, nPrime, "desc");
iP = 0;
for (int i = 0; i < gl_n; ++i) {
if (gl_arr[i] >= 2 && gl_primeCheck[gl_arr[i]]) {
gl_arr[i] = arrPrime[iP++];
}
}
delete[] arrPrime;
} else if (order == "positive desc, negative asc") {
int nPositive = 0;
int nNegative = 0;
for (int i = 0; i < gl_n; ++i) {
if (gl_arr[i] < 0) {
++nNegative;
} else {
++nPositive;
}
}
int* arrPositive = new int[nPositive];
int* arrNegative = new int[nNegative];
int iP = 0;
int iN = 0;
for (int i = 0; i < gl_n; ++i) {
if (gl_arr[i] < 0) {
arrNegative[iN++] = gl_arr[i];
} else {
arrPositive[iP++] = gl_arr[i];
}
}
us_sort(arrPositive, nPositive, "desc");
us_sort(arrNegative, nNegative, "asc");
std::memcpy(gl_arr, arrPositive, nPositive * sizeof *arrPositive);
std::memcpy(gl_arr + nPositive, arrNegative, nNegative * sizeof *arrNegative);
delete[] arrPositive;
delete[] arrNegative;
}
}
static void us_swap(int &a, int &b) {
int t = a;
a = b;
b = t;
}
static void clearConsole() {
std::system("cls");
std::system("color F0");
std::system("title Array Demo Program v.2018.11.18");
}
static void pauseConsole() {
std::cin.ignore( );
std::cin.get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment