Created
December 6, 2021 00:59
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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