I am having some trouble with this. We basically have been asked to create two versions of a program that will sort an array of 50 items then display the largest numbers in LED's. I have got it working using a basic bubble sort and it displaying. My issue is when I have to do this using recursion. Unfortunately my knowledge on recursion is very limited as I missed the lecture on it -.- He has also not put the notes online. I have had a long google however still can't get my head around it. So I ask a few things. Firstly, could someone explain recursion in relation to a bubble sort using sedo code. Secondly, am I completely wrong with my attempt.
int numbers[49];
void setup()
{
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
Serial.begin(9600);
}
void loop()
{
resetLEDLow();
genNumbers();
sortNumbers();
displayNumber();
delay(5000);
}
void resetLEDLow()
{
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
}
void genNumbers()
{
for( int i = 0 ; i < 50 ; i++ )
{
numbers[i] = random(256);
}
}
void sortNumbers()
{
int sizeOfArray = sizeof(numbers)/sizeof(int);
int check = 1;
if( check != 0 )
{
check = 0;
for( int i = 0 ; i < sizeOfArray ; i++ )
{
for ( int j = 1 ; j < sizeOfArray ; j++ )
{
if( numbers[j-1] > numbers[j] )
{
int temp = numbers[j-1];
numbers[j-1] = numbers[j];
numbers[j] = temp;
check++;
}
}
}
sortNumbers();
}
}
void displayNumber()
{
int i = 12;
int a = numbers[49];
Serial.println(numbers[49]);
while ( a > 0 )
{
int num = a % 2;
a = a / 2;
if( num == 1 )
{
digitalWrite(i, HIGH);
}else{
digitalWrite(i, LOW);
}
i--;
}
}
I know that code does not work as when it loops round count is reset to 1 so the condition will never be true thus meaning it will loop round forever and ever. So, what do I need to change or is what I am doing not really recursion at all?
Really need some help with this, got my brain going round and around.
Edit: Here is my attempt with the new recursion. I have removed code not relevant to the problem.
void loop()
{
int topLevel = 0;
int currentSort = 0;
int sizeOfArray = sizeof(numbers)/sizeof(int);
int numbers[49];
sortNumbers(topLevel,currentSort,sizeOfArray,numbers);
}
int sortNumbers(p,c,l,numbers)
{
// Swap if they are in the wrong order
if( numbers[c-1] > numbers[c] )
{
int temp = numbers[c-1];
numbers[c-1] = numbers[c];
numbers[c] = temp;
}
// If we have finished this level and need to go to next level
if( c == l )
{
c = 0;
p++;
}
// Finished
if( p == l+1 )
{
return numbers;
}
// Continue to the next place
return sortNumbers(p,c+1,l, numbers);
}
Sort50Rec:-1: error: 'p' was not declared in this scope Sort50Rec:-1: error: 'c' was not declared in this scope Sort50Rec:-1: error: 'l' was not declared in this scope Sort50Rec:-1: error: 'numbers' was not declared in this scope Sort50Rec:-1: error: initializer expression list treated as compound expression Sort50Rec.cpp: In function 'void loop()': Sort50Rec:19: error: 'numbers' was not declared in this scope Sort50Rec:21: error: 'sortNumbers' cannot be used as a function Sort50Rec.cpp: In function 'void genNumbers()': Sort50Rec:42: error: 'numbers' was not declared in this scope Sort50Rec.cpp: At global scope: Sort50Rec:46: error: redefinition of 'int sortNumbers' Sort50Rec:-1: error: 'int sortNumbers' previously defined here Sort50Rec:46: error: 'p' was not declared in this scope Sort50Rec:46: error: 'c' was not declared in this scope Sort50Rec:46: error: 'l' was not declared in this scope Sort50Rec:46: error: 'numbers' was not declared in this scope
I am sure part of it is the way I am naming my function and am I right in thinking you can only return one value? Or not?
Edit: Fixed error pointed out new error.
void loop()
{
int topLevel = 0;
int currentSort = 0;
int numbers [49];
int sizeOfArray = sizeof(numbers)/sizeof(int);
numbers = sortNumbers(topLevel,currentSort,sizeOfArray,numbers);
}
int sortNumbers(int p,int c,int l,int numbers)
{
// Swap if they are in the wrong order
if( numbers[c-1] > numbers[c] )
{
int temp = numbers[c-1];
numbers[c-1] = numbers[c];
numbers[c] = temp;
}
// If we have finished this level and need to go to next level
if( c == l )
{
c = 0;
p++;
}
// Finished
if( p == l+1 )
{
return numbers;
}
// Continue to the next place
return sortNumbers(p,c+1,l, numbers);
}
Sort50Rec.cpp: In function 'void loop()': Sort50Rec:19: error: invalid conversion from 'int*' to 'int' Sort50Rec:19: error: initializing argument 4 of 'int sortNumbers(int, int, int, int)' Sort50Rec:19: error: incompatible types in assignment of 'int' to 'int [49]' Sort50Rec.cpp: In function 'void genNumbers()': Sort50Rec:40: error: 'numbers' was not declared in this scope Sort50Rec.cpp: In function 'int sortNumbers(int, int, int, int)': Sort50Rec:47: error: invalid types 'int[int]' for array subscript Sort50Rec:47: error: invalid types 'int[int]' for array subscript Sort50Rec:49: error: invalid types 'int[int]' for array subscript Sort50Rec:50: error: invalid types 'int[int]' for array subscript Sort50Rec:50: error: invalid types 'int[int]' for array subscript Sort50Rec:51: error: invalid types 'int[int]' for array subscript Sort50Rec.cpp: In function 'void displayNumber()': Sort50Rec:71: error: 'numbers' was not declared in this scope
Thanks
int sortNumbers(p,c,l,numbers)