Created
April 8, 2014 00:25
-
-
Save mutahirqureshi/10076886 to your computer and use it in GitHub Desktop.
Sorting puzzle
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 <stdio.h> | |
/** | |
* Compile: gcc -o sort sort.c | |
* | |
* Usage: ./sort integer1, integer2 ... | |
*/ | |
int main (int argc, char *argv[]) { | |
// array that will store sorted input | |
int copy[argc - 1]; | |
int i; | |
int negIndex = 0; // index to put current negative int | |
int posIndex = argc - 2; // last index of copy array | |
int curNum; | |
for (i = 1; i < argc; i++) { | |
curNum = atoi(argv[i]); | |
if (curNum < 0) { | |
copy[negIndex] = curNum; | |
negIndex++; | |
} else if (curNum > 0) { | |
copy[posIndex] = curNum; | |
posIndex--; | |
} | |
} | |
for (i = negIndex; i <= posIndex; i++) { | |
copy[i] = 0; | |
} | |
for (i = 0; i < argc - 1; i++) { | |
curNum = copy[i]; | |
printf("%d\n", curNum); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this solution is for integer input but we could easily modify this to accept double values. Same concept applies.