Skip to content

Instantly share code, notes, and snippets.

@aeristhy
Created December 6, 2021 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aeristhy/e260e13d6bb736f5302afdea607fd421 to your computer and use it in GitHub Desktop.
Save aeristhy/e260e13d6bb736f5302afdea607fd421 to your computer and use it in GitHub Desktop.
Program that will accept 10 integers from user and sort all the numbers and group them accordingly (odd/even). Put all even numbers first, and then odd numbers (Array Exercises #2)
#include <bits/stdc++.h>
using namespace std;
void printArray(int array[], int n) {
for (int i = 0; i < n; i++)
cout << array[i] << " ";
}
bool numSort(int a, int b) {
if (a % 2 == 0 && b % 2 == 0)
return a < b;
if (a % 2 != 0 && b % 2 != 0)
return a < b;
if (b % 2 != 0)
return true;
return false;
}
int main() {
int array[] = { 8, 5, 2, 9, 6, 3, 7, 4};
int n = sizeof(array) / sizeof(int);
sort(array, array + n, numSort);
printArray(array, n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment