PDA

View Full Version : bubble sorting in C



quickshot89
May 9th, 2009, 10:00 PM
my current code is as follows


#pragma config(Sensor, S2, soundSensor, sensorSoundDB)
#pragma config(Sensor, S3, lightSensor, sensorLightActive)
#pragma config(Sensor, S4, sonarSensor, sensorSONAR)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

int objectA = 0; //this is a count value used to count the amount of objects seen
//int VarC = SensorValue(lightSensor); code disabled for program, test use only
//int VarB = SensorValue(sonarSensor); code disabled for program, test use only
//int Vara = SensorValue(soundSensor); code disabled for program, test use only
int lightA[60]; //array for light values
int soundA[60]; //array for sound values
int sonarA[60]; //array for sonar values(distance)
int count = 0; //this is used for the arrays counting
int lightB[60]; //use this array for the sorted values
//int soundB[60]; //use this array for the sorted values
int sonarB[60]; //use this array for the sorted values
int len1 = 60;


void countvalue() //this part of the program is used to count values and store them into the array every second
{
lightA[count] = SensorValue(lightSensor);
soundA[count] = SensorValue(soundSensor);
sonarA[count] = SensorValue(sonarSensor);
wait1Msec(1000);
//soundB[count] = soundA[count];
lightB[count] = soundA[count];
sonarB[count] = sonarA[count];
count++; //adds one to the count value so the previous value is not over-written, so value 2 goes into array 2,
}

void bubble1 (int soundA, int len1) //len1 = 60, as there are 60 readings
{
int i, j, temp;
int test;
for (i = len1 -1; i > 0; i--)
{
test=0;
for(j = 0; j > 1; j++)
{
if(soundA[j] > soundA[j+1])
{
temp = soundA[j];
soundA[j] = soundA[j+1];
soundA[j+1] = temp;
test=1;
}
}
if(test==0) break;
}
}

task main()
{
nNxtButtonTask = -2; //disables the exit button
ClearTimer(T1); //clears the timer
eraseDisplay(); //clears the display
bubble1();

nMotorPIDSpeedCtrl[motorC] = mtrSpeedReg; //syncs the motors
nMotorPIDSpeedCtrl[motorB] = mtrSpeedReg;

while(time1[T1] < 60000) //this part of the code runs for 60 seconds, then stops but does not end the program
{
countvalue(); //runs the countvalue() subroutine
if(SensorValue(sonarSensor) > 26)//robot runs forward unless distance is less than 26 cm

{
motor[motorC]=30; //robot runs forward with a speed of 30
motor[motorB]=30;
}
else //if an object is detected within 26cm, the following code runs
{
motor[motorC] = -30; //robot reverses
motor[motorB] = -30;
wait1Msec(1000); //waits 1 second
motor[motorC] = -20; //turns left
motor[motorB] = 20;
wait1Msec(1000); //waits 1 second
objectA++; //adds 1 to the count of object's seen
nxtDisplayTextLine(2, "objects seen: %d", objectA); //displays on the screen the count of objects seen
}
}

//while(!nNxtButtonPressed}
}
{

}


the problem is that the bubble1() function is not correctly working, as i am getting an error of:

**Error**:Procedure call Parameters don't match declaration for 'bubble1(short soundA, short len1)'

yes i know i shouldnt be using global intergers etc, but the way we are using the code its the only option

Sinkingships7
May 9th, 2009, 10:56 PM
I hope that's not the way you actually formatted the program when you wrote it. Assuming I'm correct, please paste the code in a more readable format. You'll get better responses.

:)

camper365
May 9th, 2009, 11:05 PM
at least you put the curly braces on their own line, please indent more lines. I can't read that.

quickshot89
May 9th, 2009, 11:38 PM
1st post updated with code tags, sorry about that

dwhitney67
May 9th, 2009, 11:44 PM
This is how bubble1() is defined:


void bubble1 (int soundA, int len1)
{
...
}

This is how you are calling it:


bubble1();

Do you see anything missing there?? Hint: parameters.

monkeyking
May 9th, 2009, 11:47 PM
bubble1 expects 2 arguments,
you don't supply any in bubble1 functioncall in your main.

quickshot89
May 10th, 2009, 12:13 AM
ah im such an idiot, from the site where i got the code i was under the impression i had to pre-program the arrays, instead i do it that way

my next question is, i have a 60 second timer on the task main. when the 60 seconds is up, i want the program to continue, but at the moment it ends after 60 seconds, how do i go about keeping it allive after 60 seconds so i can make things appear on the screen?

samjh
May 10th, 2009, 12:27 AM
Your code is so poorly formatted, it's hard to keep track of what's going on.

You have not code after the while(time1[T1] < 60000) loop. Hence that's why it ends. What do you mean by "so I can make things appear on the screen"? If you are talking about being able to read the output before the terminal window closes, insert some code to accept a keystroke (any character) after the while(time1[T1] < 60000) loop. I won't suggest the code because that will be too easy; use Google.

Ptero-4
May 10th, 2009, 12:29 AM
maybe a getch(); (or whatever equivalent linux uses, getch(); is a turboC/turboC++ for DOS function that pauses the program execution until you press the "ENTER" key) at the end (before the last curly brace) will do.

quickshot89
May 10th, 2009, 12:37 AM
not sure what you mean. I havnt been programming C for long. and the software and hardware im using is limited to a very simplised version of C (its the lego NXT robot kit)

samjh
May 10th, 2009, 12:38 AM
getch() is not portable. It exists only on some compilers. It is not included in GCC.

Just use a basic character input function from:
http://www.cppreference.com/wiki/c/io/start

Shpongle
May 10th, 2009, 12:43 AM
maybe a getch(); (or whatever equivalent linux uses, getch(); is a turboC/turboC++ for DOS function that pauses the program execution until you press the "ENTER" key) at the end (before the last curly brace) will do.

a scanf() should do it too!,

quickshot89
May 10th, 2009, 12:54 AM
so if i was to put the bubble sort after the while loop(well, 60 second loop)

that should prevent the program from ending, if i put a wait for 10min timer on?

samjh
May 10th, 2009, 01:07 AM
You will have to be clearer about what you want. At the moment, your question is very vague.

First, in what context are you running this program? Is this a terminal program?

Second, why do you want a 60 second timer? What is it for? Where do you want your bubble sort to run in relation to the 60 second timer -- before the timer or after it?

Third, what do you want to do after the bubble sort is completed? Do you want the program to pause before exiting, or are you going to run some more code?

quickshot89
May 10th, 2009, 01:15 AM
objectives are:

1) make the robot roam for 60 seconds avoiding objects
2) take a reading of light sound and sonar values every second and place into an array
3) after 60 seconds bubble sort the readings into order
4) when button a is pressed, display readings from sonar, button b is light, and button c is sound

i know how to do the display, im just stuck on keeping the program running after the 60 second roam, and then getting the bubble sort to work

sorry if im vauge, its 1.15am and im having to stay up to try and get the documentation of the task done :( energy drinks help thou :)

monkeyking
May 10th, 2009, 02:18 AM
#pragma config(Sensor, S2, soundSensor, sensorSoundDB)
#pragma config(Sensor, S3, lightSensor, sensorLightActive)
#pragma config(Sensor, S4, sonarSensor, sensorSONAR)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

int objectA = 0; //this is a count value used to count the amount of objects seen
//int VarC = SensorValue(lightSensor); code disabled for program, test use only
//int VarB = SensorValue(sonarSensor); code disabled for program, test use only
//int Vara = SensorValue(soundSensor); code disabled for program, test use only
int lightA[60]; //array for light values
int soundA[60]; //array for sound values
int sonarA[60]; //array for sonar values(distance)
int count = 0; //this is used for the arrays counting
int lightB[60]; //use this array for the sorted values
//int soundB[60]; //use this array for the sorted values
int sonarB[60]; //use this array for the sorted values
int len1 = 60;


void countvalue() //this part of the program is used to count values and store them into the array every second
{
lightA[count] = SensorValue(lightSensor);
soundA[count] = SensorValue(soundSensor);
sonarA[count] = SensorValue(sonarSensor);
wait1Msec(1000);
//soundB[count] = soundA[count];
lightB[count] = soundA[count];
sonarB[count] = sonarA[count];
count++; //adds one to the count value so the previous value is not over-written, so value 2 goes into array 2,
}

void bubble1 (int soundA, int len1){
int i, j, temp;
int test;
for (i = len1 -1; i > 0; i--){
test=0;
for(j = 0; j > 1; j++){
if(soundA[j] > soundA[j+1]){
temp = soundA[j];
soundA[j] = soundA[j+1];
soundA[j+1] = temp;
test=1;
}
}
if(test==0)
break;
}
}

task main(){
nNxtButtonTask = -2; //disables the exit button
ClearTimer(T1); //clears the timer
eraseDisplay(); //clears the display
bubble1();

nMotorPIDSpeedCtrl[motorC] = mtrSpeedReg; //syncs the motors
nMotorPIDSpeedCtrl[motorB] = mtrSpeedReg;
while(time1[T1] < 60000) {
countvalue(); //runs the countvalue() subroutine
if(SensorValue(sonarSensor) > 26){
motor[motorC]=30; //robot runs forward with a speed of 30
motor[motorB]=30;
} else {
motor[motorC] = -30; //robot reverses
motor[motorB] = -30;
wait1Msec(1000); //waits 1 second
motor[motorC] = -20; //turns left
motor[motorB] = 20;
wait1Msec(1000); //waits 1 second
objectA++; //adds 1 to the count of object's seen
nxtDisplayTextLine(2, "objects seen: %d", objectA);
}
}
//while(!nNxtButtonPressed}
}
{

}

If you like the funky colors and nice format you could try it next time.


You questions has nothing to do with programmingquestions.
It's way too abstract.

If you just want the program to sleep for 60 seconds,
just use sleep(milisecs).
Or do you want to input values every second into some arrays for a full minute?

quickshot89
May 10th, 2009, 02:49 AM
right, i dont see how my questions arnt about programming, as i said before, im using the lego NXT mindstorms kit, along with robotC, which is an enviroment for learning C, so far, ive managed to get the robot to run around for 60 seconds, taking a reading every second, and for it to avoid objects.

not trying to sound aggresive here, but im just for alittle help to get the code to sort values after 60 seconds, and for the program not to end so i can then display the min and max values on the robots screen :confused:

dwhitney67
May 10th, 2009, 03:07 AM
right, i dont see how my questions arnt about programming, as i said before, im using the lego NXT mindstorms kit, along with robotC, which is an enviroment for learning C, so far, ive managed to get the robot to run around for 60 seconds, taking a reading every second, and for it to avoid objects.

not trying to sound aggresive here, but im just for alittle help to get the code to sort values after 60 seconds, and for the program not to end so i can then display the min and max values on the robots screen :confused:

Here's some very simplistic code:


for (;;)
{
/* collect measurements for approximately 60 seconds */
for (int period = 0; period < 60; ++period)
{
/* collect measurements */

sleep(1);
}

/* sort data and display it */
}


If you want better timing, then sleep() is not practical. You may want to consider using a real-time timer, but I haven't a clue if your robot's C-compiler supports this.

monkeyking
May 10th, 2009, 03:11 AM
Hi again

I don't know lego mindstorm or some strange language build for these lego machines.

So when you are using expresssion like 'make robot roam',
I can't help you.

But saying something like.
My sorting function doesn't work for the last element in the array,
this would be much more helpfull.

Concerning your code,
there are some problems that I can spot.

1. argument for bubble1.
You write 'int soundA', this will shadow your global array called soundA,

2. I don't really get how you are trying to sort you array, I would swap elements next to each other like.


void sort_soundA(int Length_of_array){
for (i=0; i<Length_of_array-1; i++) {
for (j=0; j<Length_of_array-1-i; j++)
/* compare the two neighbors */
if (soundA[j+1] < soundA[j]) {
tmp = soundA[j]; /* swap a[j] and a[j+1] */
soundA[j] = soundA[j+1];
soundA[j+1] = tmp;
}
}
}


Hope it helps and good luck

quickshot89
May 10th, 2009, 03:37 AM
yeah your code worked perfectly for sound, thanks alot, ive changed the code for sonar and light, could you just confirm if ive got this right?



void sort_sonarA(int length)
{
for (o=0; p<len1-1; o++)
{
for (p=0; p<len1-1-o; p++)
/* compare the two neighbors */
if (sonarA[p+1] < sonarA[p])
{
tmp1 = sonarA[p]; /* swap a[p] and a[p+1] */
sonarA[p] = sonarA[p+1];
sonarA[p+1] = tmp1;
}
}
}

void sort_lightA(int length)
{
for (n=0; m<len1-1; n++)
{
for (m=0; m<len1-1-n; m++)
/* compare the two neighbors */
if (lightA[m+1] < lightA[m])
{
tmp2 = lightA[m]; /* swap a[m] and a[m+1] */
lightA[m] = lightA[m+1];
lightA[m+1] = tmp2;
}
}
}


or should i just use a wait function between each sort (say 10 seconds) and keep the values as i and j and tmp?

monkeyking
May 10th, 2009, 04:01 AM
Hi good you got it working :)

I don't know this legostorm language, so

Depending on the naziness of the language I would make tmp and the indices for the loops localized for a general sort routine.

something like


void general_sort(int *array_to_sort, int length){
int tmp;
int i,j
for (i=0; i<length-1; i++) {
for (j=0; j<length-1-i; j++)
/* compare the two neighbors */
if (array_to_sort[j+1] < array_to_sort[j]) {
tmp = array_to_sort[j];
array_to_sort[j] = array_to_sort[j+1];
array_to_sort[j+1] = tmp;
}
}
}
//now you can sort like
general_sort(soundA,60);
general_sort(soundB,60);


But you don't need to wait 10 seconds,
just as long as you input your new values into the variables before using them,
you should be fine.


good luck

quickshot89
May 10th, 2009, 09:47 AM
ok i tried that general sort, im now getting Array specifier invalid for variable 'array_to_sort'

i dont undertand why im getting that error :S

dwhitney67
May 10th, 2009, 01:12 PM
The sort function posted by MonkeyKing looks good; your compilation error may stem from how you are calling the function.

Please post the following:
1. How you declare the array.
2. How you are calling general_sort().

P.S. Btw, it is possible that syntactically you are doing everything correct, but conceivable that your specialized compiler is causing the problem.

quickshot89
May 10th, 2009, 01:23 PM
i deciede to try using a seperate sort for each of the 3 sensors, and it worked this time! it seems by using global you would have to wort out how to reset the values every time a sort of an array has been done, way too much work for the amount of time i have left. below is my code that meets about80% of what i needed it to do, if i can find how to do the rest of this this should be acceptable as it sorts the values and then displays them on the screen to prove they have been sorted


#pragma config(Sensor, S2, soundSensor, sensorSoundDB)
#pragma config(Sensor, S3, lightSensor, sensorLightActive)
#pragma config(Sensor, S4, sonarSensor, sensorSONAR)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

int objectA = 0; //this is a count value used to count the amount of objects seen
int lightA[60]; //array for light values
int soundA[60]; //array for sound values
int sonarA[60]; //array for sonar values(distance)
int count = 0; //this is used for the arrays counting
int lenarray = 60;
int tmp;
int i,j;
int test;
int temp;
int n,m;
int tep;
int g,h;

void sort_soundA(int lenarray)
{
for (i=0; i<lenarray-1; i++)
{
for (j=0; j<lenarray-1-i; j++)
if (soundA[j+1] < soundA[j])
{
tmp = soundA[j];
soundA[j] = soundA[j+1];
soundA[j+1] = tmp;
test=1;
}
}
if(test==0) break;
}

void sort_lightA(int lenarray)
{
for (n=0; n<lenarray-1; n++)
{
for (m=0; m<lenarray-1-n; m++)
if (lightA[m+1] < lightA[m])
{
temp = lightA[m];
lightA[m] = lightA[m+1];
lightA[m+1] = temp;
test=1;
}
}
if(test==0) break;
}

void sort_sonarA(int lenarray)
{
for (g=0; g<lenarray-1; g++)
{
for (h=0; h<lenarray-1-g; h++)
if (sonarA[h+1] < sonarA[h])
{
tep = sonarA[h];
sonarA[h] = sonarA[h+1];
sonarA[h+1] = tep;
test=1;
}
}
if(test==0) break;
}

void countvalue() //this part of the program is used to count values and store them into the array every second
{
lightA[count] = SensorValue(lightSensor);
soundA[count] = SensorValue(soundSensor);
sonarA[count] = SensorValue(sonarSensor);
wait1Msec(1000);
count++; //adds one to the count value so the previous value is not over-written, so value 2 goes into array 2,
}

void noroam()

{
motor[motorC] = 0;
motor[motorB] = 0;
}

void roam()
{
while(time1[T1] < 60000) //this part of the code runs for 60 seconds, then stops but does not end the program

{
countvalue();
nMotorPIDSpeedCtrl[motorC] = mtrSpeedReg; //syncs the motors
nMotorPIDSpeedCtrl[motorB] = mtrSpeedReg;

if(SensorValue(sonarSensor) > 26)//robot runs forward unless distance is less than 26 cm

{
motor[motorC]=20; //robot runs forward with a speed of 30
motor[motorB]=20;
}
else
{
motor[motorC] = -30; //robot reverses
motor[motorB] = -30;
wait1Msec(1000); //waits 1 second
motor[motorC] = -20; //turns left
motor[motorB] = 20;
wait1Msec(1000); //waits 1 second
objectA++;
}
}
}

task main()
{
ClearTimer(T1);


eraseDisplay(); //clears the display
roam();
noroam();
while(1==1)
{
eraseDisplay();
nxtDisplayTextLine(0, "Min light: %d",lightA[0]);
nxtDisplayTextLine(1, "Max light: %d",lightA[59]);
nxtDisplayTextLine(3, "Min sound: %d",soundA[0]);
nxtDisplayTextLine(4, "Max sound: %d",soundA[59]);
nxtDisplayTextLine(6, "Min sonar: %d",sonarA[0]);
nxtDisplayTextLine(7, "Max sonar: %d",sonarA[59]);
sort_soundA(60);
sort_lightA(60);
sort_sonarA(60);
wait10Msec(200);
}
}


sorry if the code has not alligned properly, ive now been away for 28 hours and my eyes hurt like nothing ive felt before, thanks for all the help so far, its been a huge help

(cant wait till next year when i do proper C programming, as the robots are more of an easy way to help introduce ideas)

monkeyking
May 10th, 2009, 02:28 PM
i deciede to try using a seperate sort for each of the 3 sensors, and it worked this time! it seems by using global you would have to wort out how to reset the values every time a sort of an array has been done, way too much work for the amount of time i have left. below is my code that meets about80% of what i needed it to do, if i can find how to do the rest of this this should be acceptable as it sorts the values and then displays them on the screen to prove they have been sorted


So what do you want?

quickshot89
May 10th, 2009, 05:25 PM
it didnt work the 1st time i tried it :(

monkeyking
May 10th, 2009, 07:34 PM
You really need to work on your ability to communicate your programming problems.

What are the remaining 20% you need to program?

what is working,
what is not working.