Skip to content

Instantly share code, notes, and snippets.

@vonderasche
Created May 18, 2016 04:25
Show Gist options
  • Save vonderasche/2ab3ee876dd15fcc89b745e05a5ede36 to your computer and use it in GitHub Desktop.
Save vonderasche/2ab3ee876dd15fcc89b745e05a5ede36 to your computer and use it in GitHub Desktop.
int currentArray[10];
// creates spaces - function
void spaces(int x)
{
for (int beta = 0; beta < x; beta++)
{
Serial.print(" ");
}
}
// creates new lines
void lines(int z)
{
for (int alpha = 0; alpha < z; alpha++)
{
Serial.println(" ");
}
}
// sort array function
void sortArray(int sortedArray[])
{
int temp = 0;
for (int x = 0; x < 10; x++)
{
{
for (int y = 0; y < 10; y++)
{
if (sortedArray[x] < sortedArray[y])
{
temp = sortedArray[x];
sortedArray[x] = sortedArray[y];
sortedArray[y] = temp;
}
}
}
}
lines(2);
Serial.println("The Array is sorted from least to greatest.");
printArray(sortedArray);
}
// reverse sort array - function
void rsortArray(int sortedArray[])
{
int temp = 0;
for (int x = 0; x < 10; x++)
{
{
for (int y = 0; y < 10; y++)
{
if (sortedArray[x] > sortedArray[y])
{
temp = sortedArray[x];
sortedArray[x] = sortedArray[y];
sortedArray[y] = temp;
}
}
}
}
lines(2);
Serial.println("The Array is sorted from greatest to least.");
printArray(sortedArray);
}
// print array - function
void printArray(int pArray[])
{
for (int q = 0; q < 10; q++)
{
Serial.print(pArray[q]);
spaces(2);
}
}
// find the even and odd elements of a function
void even_odd(int eoArray[])
{
int evenArray[10] = { 0,0,0,0,0,0,0,0,0,0 };
int oddArray[10] = { 0,0,0,0,0,0,0,0,0,0 };
int evenCounter = 0;
lines(2);
Serial.println("The array contains the following even numbers...");
for (int f = 0; f < 10; f++)
{
if (eoArray[f] % 2 == 0)
{
evenArray[f] = eoArray[f];
}
}
for (int x = 0; x < 10; x++)
{
if (eoArray[x] != 0)
{
evenCounter++;
}
}
for (int x = 0; x < evenCounter; x++)
{
Serial.print(evenArray[x]);
spaces(2);
}
lines(2);
Serial.println("The array contains the following odd numbers...");
for (int w = 0; w < 10; w++)
{
if (eoArray[w] % 2 != 0)
{
oddArray[w] = eoArray[w];
}
}
lines(2);
Serial.println("The array contains the following zeros...");
for (int m = 0; m < 10; m++)
{
if (eoArray[m] == 0)
{
Serial.print(eoArray[m]);
spaces(2);
}
}
}
// generate random array - function
void randomArray(int rArray[])
{
randomSeed(analogRead(0));
for (int s = 0; s < 10; s++)
{
rArray[s] = random(0, 20);
}
lines(2);
Serial.println("The randomly generated array is....");
printArray(rArray);
}
// table - function
void multArray(int mArray[])
{
lines(2);
Serial.println("The Multiplication table is...");
int product;
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
Serial.print(mArray[x] * mArray[y]);
spaces(2);
}
lines(1);
}
}
void setup()
{
Serial.begin(9600);
lines(2);
Serial.println("Make a selection");
Serial.println("To generate a another random array of numbers enter: 'A'");
Serial.println("To order the array least to greatest enter: 'B'");
Serial.println("To order the array greatest to least enter: 'C'");
Serial.println("To see the even and odd numbers enter: 'D'");
Serial.println("To create multiplication table enter: 'E'");
randomArray(currentArray);
lines(2);
}
// main
void loop()
{
char input;
while (Serial.available() == 0);
{
input = Serial.read();
if (input == 'A' || input == 'a')
{
randomArray(currentArray);
}
if (input == 'B' || input == 'b')
{
sortArray(currentArray);
}
if (input == 'C' || input == 'c')
{
rsortArray(currentArray);
}
if (input == 'D' || input == 'd')
{
even_odd(currentArray);
}
if (input == 'E' || input == 'e')
{
multArray(currentArray);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment