Skip to content

Instantly share code, notes, and snippets.

@aeristhy
Created December 6, 2021 00:52
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/2be702f81cebd0f2ad83704d5bf4da39 to your computer and use it in GitHub Desktop.
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)
#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