const int arraySize = 50; // Define the size of the array
int array[arraySize]; // Declare the array
const int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; // Define LED pin numbers
void setup() {
Serial.begin(9600); // Initialize the serial communication
randomSeed(analogRead(0)); // Seed the random number generator
for (int i = 0; i < 8; i++) {
pinMode(ledPins[i], OUTPUT); // Set LED pins as outputs
}
}
void loop() {
Serial.print("\nPress '1' to sort in Ascending Order\nPress '2' for Descending Order");
if (Serial.available() > 0) {
String str = Serial.readString(); // Read the incoming string
int ch = str.toInt();
Serial.println("Generating Random Numbers ....");
for (int i = 0; i < arraySize; i++) {
array[i] = random(0, 256); // Generate numbers between 0 and 100
}
// Print the random numbers
for (int i = 0; i < arraySize; i++) {
Serial.print(array[i]);
Serial.print(" ");
}
Serial.println();
if( ch == 1)
{
sort(array,arraySize,1);
Serial.println("Sorting ......");
delay(2000);
for (int i = 0; i < arraySize; i++) {
Serial.print(array[i]);
Serial.print(" ");
}
Serial.println();
delay(2000);
Serial.print("Minimum Number is : ");
Serial.print(array[0]);
Serial.print(" ");
displayNumber(array[0]);
delay(5000); // Wait for 5 second
Serial.print("Miximum Number is : ");
Serial.print(array[arraySize-1]);
Serial.print(" ");
displayNumber(array[arraySize-1]);
delay(5000); // Wait for 5 second
}
else if( ch == 2)
{
sort(array,arraySize,0);
Serial.println("Sorting ......");
delay(2000);
for (int i = 0; i < arraySize; i++) {
Serial.print(array[i]);
Serial.print(" ");
}
Serial.println();
delay(2000);
Serial.print("Minimum Number is : ");
Serial.print(array[arraySize-1]);
Serial.print(" ");
displayNumber(array[arraySize-1]);
delay(5000); // Wait for 5 second
Serial.print("Miximum Number is : ");
Serial.print(array[0]);
Serial.print(" ");
displayNumber(array[0]);
delay(5000); // Wait for 5 second
}
else
{
Serial.print("Invalid Option \n");
}
}
delay(5000); // Wait for 5 second before generating new numbers
// Generate random numbers and store them in the array
}
void sort(int arr[], int size,int f){
int i,j,temp;
if( f )
{
for (i = 0; i < size; i++) {
for (j = 0; j < size - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
else
{
for (i = 0; i < size; i++) {
for (j = 0; j < size - i; j++) {
if (arr[j] < arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
void displayNumber(int number) {
for (int i = 0; i < 8; i++) {
int bit = (number >> i) & 1; // Extract the i-th bit
digitalWrite(ledPins[7 - i], bit); // Write the bit to the corresponding LED
}
}