Created
December 6, 2021 00:52
-
-
Save aeristhy/2be702f81cebd0f2ad83704d5bf4da39 to your computer and use it in GitHub Desktop.
Program that will accept 12 integers from user and sort in a zigzag form. (Array Exercises #1)
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 <iostream> | |
using namespace std; | |
void zigzag(int array[], int n) { | |
bool x = true; | |
for (int i = 0; i <= n - 2; i++) { | |
if (x) { | |
if (array[i] > array[i+1]) | |
swap(array[i], array[i+1]); | |
} else { | |
if (array[i] < array[i+1]) | |
swap(array[i], array[i+1]); | |
} | |
x = !x; | |
} | |
} | |
int main() { | |
int array[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; | |
int n = sizeof(array)/sizeof(array[0]); | |
zigzag(array, n); | |
for (int i=0; i<n; i++) | |
cout << array[i] << " "; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment