
mDot program that generates random numbers and sorts them, outputting to USB debug.
Fork of mDot_helloworld by
Revision 2:3c65515d63e3, committed 2017-08-05
- Comitter:
- kellybs1
- Date:
- Sat Aug 05 13:06:18 2017 +0000
- Parent:
- 1:34c1fcb8ea5a
- Commit message:
- Playing around with mDot
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Jul 07 14:09:33 2015 +0000 +++ b/main.cpp Sat Aug 05 13:06:18 2017 +0000 @@ -1,34 +1,50 @@ /************************************* - * This simple example program blinks - * a LED and prints a message out the - * USB debug port. + * This simple example program + * randomly generates a small array + * of integers and then sorts them, + * displaying at the end of each + * sort step ************************************/ #include "mbed.h" -// Uncomment this line if using a full sized UDK2.0 instead of a Micro UDK -// #define UDK2 1 - -#ifdef UDK2 -DigitalOut led(LED1); -#else -DigitalOut led(XBEE_RSSI); -#endif - -Ticker tick; - -// callback function to change LED state -void blink() { - led = !led; +void swap(int nums [], int indexA, int indexB) +{ + //throw value in a temp value + int temp = nums[indexA]; + //move second into first + nums[indexA] = nums[indexB]; + //move temp into second + nums[indexB] = temp; } -int main() { - // configure the Ticker to blink the LED on 500ms interval - tick.attach(&blink, 0.5); +int main() +{ + int loopEnd = 8; + int nums [loopEnd]; - // print the message on 2s interval while (true) { - printf("Hello world!\r\n"); - wait(2); + printf("\nHello world! I am an mDot sorting random numbers\n"); + //fill array with pseudorandom numbers + for (int i = 0; i < loopEnd; i++) + nums[i] = rand() % 100; + + //Bubble sort! + for (int i = 0; i < loopEnd; i++) //check every value + { + for (int j = i; j < loopEnd; j++) //against every value after it + { + if (nums[i] > nums[j] && i != j) //swap if unsorted and not the same element + { + swap(nums, i, j); + } + } + //output this step + for(int i = 0; i < loopEnd; i++) + printf( "%d ", nums[i]); + printf("\n"); + wait(1); + } + wait(3); } } \ No newline at end of file