mDot program that generates random numbers and sorts them, outputting to USB debug.

Dependencies:   mbed

Fork of mDot_helloworld by MultiTech

main.cpp

Committer:
kellybs1
Date:
2017-08-05
Revision:
2:3c65515d63e3
Parent:
1:34c1fcb8ea5a

File content as of revision 2:3c65515d63e3:

/*************************************
 * 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"

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()
{   
    int loopEnd = 8;
    int nums [loopEnd];
    
    while (true) {
        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);
    }
}