NEEDS MOAR DYNAMIC
Created
January 17, 2012 11:35
-
-
Save rxbynerd/1626342 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
fantastic |
This file contains hidden or 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
// Fantastic | |
// Copyright Luke Carpenter | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#define BUBBLES 5 | |
void print_array(int index, int list[]) { | |
printf("debug called: "); | |
for (int position = 0; position <= index; position++) { | |
printf("%d => %d; ", position, list[position]); | |
} | |
printf("\n"); | |
} | |
int main(int argc, const char *argv[]) { | |
int list[BUBBLES]; | |
int i = 0; | |
// n is used for n pass | |
int n = 1; | |
// finished is used to delcare if the list is sorted or not | |
bool finished = false; | |
int position, temp; | |
printf("Fantastic magical bubbly happy funtimes!!\n"); | |
if (argc != 6) { | |
fprintf(stderr, "Invalid argument length!\n"); | |
exit(EXIT_FAILURE); | |
} | |
for(i++; i <= BUBBLES; i++) { | |
list[i - 1] = atoi(argv[i]); | |
} | |
// printf("INPUT: "); | |
// for (i = 0; i <= BUBBLES; i++) | |
// printf("%d ", list[i]); | |
// printf("\n"); | |
print_array(BUBBLES, list); | |
position = BUBBLES; | |
printf("----\n"); | |
do { | |
finished = false; | |
for(i = 0; i <= (position - 1); i++) { | |
if (list[i] > list[i+1]) { | |
// le swap | |
printf("SWAPPING: list[i]: %d, list[i-1]: %d\n", list[i], list[i-1]); | |
temp = list[i]; | |
list[i] = list[i+1]; | |
list[i+1] = temp; | |
finished = true; | |
} | |
printf("Inside for %d @ position %d: ", i, position); | |
for (temp = 0; temp <= BUBBLES; temp++) | |
printf("%d ", list[temp]); | |
printf("\n"); | |
} | |
position--; | |
printf("----\n"); | |
} while((position != 0) || (finished == false)); | |
printf("And we're done\n"); | |
printf("inspect: "); | |
for(i = 0; i <= BUBBLES; i++) | |
printf("%d ", list[i]); | |
printf("\n"); | |
return 0; | |
} |
This file contains hidden or 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
// Fantastic | |
// Header file for when I work out pointers | |
void print_array(int, int[]); |
This file contains hidden or 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
You have an array with five items in it | |
These five items are 9, 7, 4, 6, 2 | |
You also have a boolean called "swapped", which is used to exit your primary loop if you have not swapped any items in the array | |
You have an integer called position | |
It is set to the length of your array | |
You are now in a loop | |
This loop represents your passes | |
You must only exit the loop when position is zero, or you have completed a pass of the array without swapping two adjacent values | |
Now you are in the loop, you first set "swapped" to false, because this is a new pass | |
You now enter another loop | |
This is a "for" loop, which initializes a variable called "i" and sets it to zero, stops the loop when i becomes equal or greater than position, and increments i on every new loop |
This file contains hidden or 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
main: | |
gcc -std=c99 -o fantastic fantastic.c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment