Array not sorting

This should be relatively straightforward bubble sort, but I can't seem to find the error. In the code, the math 'cube_vol' is the largest variable, and when the array is sorted, it should be the last on the list. But the array doesn't sort. Any ideas?

/**********LIBRARIES**********/

#include <math.h>  // https://www.tutorialspoint.com/arduino/arduino_math_library.htm


#define led_1 12  //led on pin 12


void setup() 
{
  Serial.begin(9600);
  pinMode(led_1, OUTPUT);  

  double length = 8.55;  //length of side
  double area_math;
  double cube_vol;
  double sphere_vol;
  const double pi_value = 3.14;
  const int array_size = 3;
  double array_values[array_size] = {0};

  area(length, &area_math);
  cube(length, &cube_vol);
  sphere(length, pi_value, &sphere_vol);
  print_values(area_math, cube_vol, sphere_vol);
  array_sort(array_size, array_values);
  array_insert(area_math, cube_vol, sphere_vol, array_values);
  array_print(array_size, array_values);
}

void loop() 
{

}

/**********FUNCTION DEFINITIONS**********/

void area(double length, double*ptrArea)
{
  //square length
  *ptrArea = square(length);  
}

void cube(double length, double*ptrCube_vol)
{
  //cube volume
  *ptrCube_vol = pow(length, 3);  // x to power of y
}

void sphere(double length, const double pi_value, double*ptrSphere_vol)
{
  //sphere volume
  *ptrSphere_vol = (1.33333333) * pi_value * pow((length * 0.5), 3);   // where (4 / 3) = 1.3333333
}

void print_values(double area_math, double cube_vol, double sphere_vol)
{
  //area
  Serial.print("Area = ");
  Serial.println(area_math);
  Serial.flush();
  //cube
  Serial.print("Cube volume = ");
  Serial.println(cube_vol);
  Serial.flush();
  //sphere
  Serial.print("Sphere volume = ");
  Serial.println(sphere_vol);
  Serial.flush();
}


void array_insert(double area_math, double cube_vol, double sphere_vol, double array_values[])
{
  array_values[0] = area_math;
  array_values[1] = cube_vol;
  array_values[2] = sphere_vol;   
}


void array_sort(int array_size, double array_values[])
{
  Serial.println("Sorting...");
  
  //sort via pointers
  for (int pass=0; pass < array_size - 1; pass++)
  {
    for (int i=0; i < array_size - 1; i++)
    {
      if (array_values[i] > array_values[i+1])
      {
        //swap positions
        double hold = array_values[i];  
        array_values[i] = array_values[i+1]; 
        array_values[i+1] = hold; 
      }
    }
  } 
}

void array_print(int array_size, double array_values[])
{   
  for (int i=0; i<array_size; i++)
  {
    Serial.println(array_values[i]);    
  }
}

Why not "insert, sort, print"?

Hi,
see now if work :

you were sorting an empty array.

I changed the order of function calls.
from:
array_sort(array_size, array_values);
array_insert(area_math, cube_vol, sphere_vol, array_values);

to:
array_insert(area_math, cube_vol, sphere_vol, array_values);
array_sort(array_size, array_values);

/**********LIBRARIES**********/

#include <math.h>  // https://www.tutorialspoint.com/arduino/arduino_math_library.htm


#define led_1 12  //led on pin 12


void setup()
{
  Serial.begin(9600);
  pinMode(led_1, OUTPUT);

  double length = 8.55;  //length of side
  double area_math;
  double cube_vol;
  double sphere_vol;
  const double pi_value = 3.14;
  const int array_size = 3;
  double array_values[array_size] = {0};

  area(length, &area_math);
  cube(length, &cube_vol);
  sphere(length, pi_value, &sphere_vol);
  print_values(area_math, cube_vol, sphere_vol);
  array_insert(area_math, cube_vol, sphere_vol, array_values);
  array_sort(array_size, array_values);
  array_print(array_size, array_values);
}

void loop()
{

}

/**********FUNCTION DEFINITIONS**********/

void area(double length, double*ptrArea)
{
  //square length
  *ptrArea = square(length);
}

void cube(double length, double*ptrCube_vol)
{
  //cube volume
  *ptrCube_vol = pow(length, 3);  // x to power of y
}

void sphere(double length, const double pi_value, double*ptrSphere_vol)
{
  //sphere volume
  *ptrSphere_vol = (1.33333333) * pi_value * pow((length * 0.5), 3);   // where (4 / 3) = 1.3333333
}

void print_values(double area_math, double cube_vol, double sphere_vol)
{
  //area
  Serial.print("Area = ");
  Serial.println(area_math);
  Serial.flush();
  //cube
  Serial.print("Cube volume = ");
  Serial.println(cube_vol);
  Serial.flush();
  //sphere
  Serial.print("Sphere volume = ");
  Serial.println(sphere_vol);
  Serial.flush();
}


void array_insert(double area_math, double cube_vol, double sphere_vol, double array_values[])
{
  array_values[0] = area_math;
  array_values[1] = cube_vol;
  array_values[2] = sphere_vol;
}


void array_sort(int array_size, double array_values[])
{
  Serial.println("Sorting...");

  //sort via pointers
  for (int pass = 0; pass < array_size - 1; pass++)
  {
    for (int i = 0; i < array_size - 1; i++)
    {
      if (array_values[i] > array_values[i + 1])
      {
        //swap positions
        double hold = array_values[i];
        array_values[i] = array_values[i + 1];
        array_values[i + 1] = hold;
      }
    }
  }
}

void array_print(int array_size, double array_values[])
{
  for (int i = 0; i < array_size; i++)
  {
    Serial.println(array_values[i]);
  }
}

Great catch with the program flow error, thanks!

"Great catch" ?
Seriously?
It jumps off the page.

One small thing you might want to add, to save a few cycles: If you get through the entire inner loop of the sort WITHOUT having to do any swaps, you are done. You can skip the remainder of the outer loop.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.