Registration starts August 22nd 😱
Try out this code practice with your physical Arduino or Arduino simulator (mentioned in our Software page)!
Sort ANY random array of numbers, such as [3, 1, 2, 5, 4], in ascending order, like [1, 2, 3, 4, 5], and print the sorted array to the Serial Monitor. Make sure to plan out your code before you write/test it!
Starting code template is shown here. Feel free to use the resources below the code to help!
// Intial, un-sorted array
const int ARRAY_LENGTH = 5;
int array[ARRAY_LENGTH] = { 3, 1, 2, 5, 4 };
// SET-UP Function (runs first and only once)
void setup() {
// Start serial communication of 9600 bps (bits per second)
Serial.begin(9600);
// note: if you use a physical Arduino, ensure that your Serial Monitor is set to the same baud rate, or you might get weird output!
// One second delay before starting your code
delay(1000);
// YOUR CODE TO SORT THE ARRAY
// note: without your code, this program will just print out the array un-sorted!
// Print array to Serial Monitor (if your sorting code is written before this line, then the printed array will be sorted)
// note: printing is this code does NOT involve any physical printing, just means displaying output to your screen via Serial Monitor
for (int index = 0; index < ARRAY_LENGTH; index++) {
// "index" will increment by 1 from 0 while it is less than "ARRAY_LENGTH", so 0->1->2->3->4
Serial.println(array[index]); // print to Serial Monitor number at this index of the array
// note: the "ln" in the command above indicates a new line, meaning each number will be printed on a new line
}
}
// LOOP Function (runs second and loops forever)
void loop() {
// we do not need anything to loop!
}
A simple sorting method to implement is "Bubble Sort." Here is a video from the KC Ang YouTube channel illustrating the process.
Watch our video here for a quick intro into the code practice
Serial Monitor in Tinkercad
Serial Monitor in Arduino IDE