Hey to all, I was wondering if anyone knew how to do something called a bubble s
ID: 3867201 • Letter: H
Question
Hey to all, I was wondering if anyone knew how to do something called a bubble sort? The competition director said our program should display the calculations
of each turbine in ascending order and the only thing I can find online is something called a bubble sort. The code for it seems confusing. It's the last part of the program but we have no idea
how to do something like that. Any advice would be much appreciated, we have no idea how to even implement something like that.
For reference here is the program thus far.
#include<stdio.h>
#include<string.h>
#include<math.h>
#define row 10
#define col 3
#define PI 3.147
typedef struct turbine_struct
{
float r;
float speed;
float power;
} turbine;
int menuOption(void);
float area(float radius); //Area Function
float power(float Density, float Area, float Speed, float k, float x); //Power Function
/* change the function prototypes to take arrays of structs rather than 2-d arrays of floats */
void windTurbinePowerCalculator(turbine wind[row], float x, float k, float airDensity);
void marineTurbinePowerCalculator(turbine marine[row], float x, float k, float m, float waterDensity);
void analysisMarineTurbine(float m, float waterDensity);
void TurbineDat(turbine wind[row], turbine marine[row]);
int main()
{
char firstName[25];
char lastName[25];
int choice, loopcount, n = 1, flag = 1;
int ROW, c;
float x = 0.000001;
float tidalFlowSpeed;
float airDensity = 1.225;
float waterDensity = 1000;
float k = 0.40; //Let k = the power coefficient for wind.
float m = 0.35; //Let m = the power coefficient for marine.
float Area, Power;
/* float windTurbine[row][col]; */
/* float marineTurbine[row][col]; */
turbine wind[row]; /* row is going to be the maximum number of turbines we can load into this array */
turbine marine[row]; /* note that col is gone. */
printf(" | uPower Corporation | ");
printf(" ------------------------ ");
printf(">>Please enter your first name: ");
scanf("%s", &firstName);
printf(" >>Please enter your last name: ");
scanf("%s", &lastName);
printf(" >>Welcome Analyst: [ %s %s ] ", firstName, lastName);
getchar();
getchar();
// set all to zero
for (ROW = 0; ROW < row; ++ROW)
//for (c = 0; c < col; ++c)
{
wind[ROW].r = 0.0; /* initialize the variables in the structs to zero */
wind[ROW].speed = 0.0; /* access the struct members using the (.) */
wind[ROW].power = 0.0;
marine[ROW].r = 0.0;
marine[ROW].speed = 0.0;
marine[ROW].power = 0.0;
}
while (flag)
{
choice = menuOption();
switch (choice)
{
case 0:
printf(" | User has exited | ");
flag = 0;
break; //break case 0.
case 1:
windTurbinePowerCalculator(wind, x, k, airDensity);
break; //break case 1.
case 2:
marineTurbinePowerCalculator(marine, x, k, m, waterDensity);
break; //break case 2.
case 3:
analysisMarineTurbine(m, waterDensity);
break; //break case 3.
case 4:
TurbineDat(wind, marine);
break; //break case 4.
default:
printf(" Invalid choice. Please choose a valid menu option. ");
break; //break default.
} //end swtich.
} //end while (flag).
} //end main.
int menuOption(void)
{
int choice = 0;
printf(" ----------MENU---------- ");
printf(" >> 0: Exit ");
printf(" >> 1: Wind Turbine ");
printf(" >> 2: Marine Turbine ");
printf(" >> 3: Analysis ");
printf(" >> 4: Turbine Table Data ");
printf(" Please enter your choice: ");
scanf("%d", &choice);
return choice;
}
void windTurbinePowerCalculator(turbine wind[row], float x, float k, float airDensity)
{
int loopcount, n = 1;
int c;
float Area, windSpeed;
float Radius;
printf(" | Wind Turbine - Power Calculator | ");
printf("User can enter up to 10 turbines. ");
printf(">>Enter the number of turbines to be calculated: ");
scanf("%d", &n);
loopcount = 1;
while (loopcount <= n && loopcount < row + 1)
{
int index = loopcount - 1;
printf(">>Enter the radius for turbine |%d| in meters: ", loopcount);
scanf("%f", &Radius);
printf(">>Enter the wind speed for turbine |%d| in meters/second: ", loopcount);
scanf("%f", &windSpeed);
wind[index].r = Radius; //Turbine Radius
Area = area(Radius); //PI * pow(Radius, 2); //Area
wind[index].speed = windSpeed; //Wind Speed
wind[index].power = power(airDensity, Area, windSpeed, k, x); //((airDensity * Area * pow(windSpeed, 3) * k) / 2) * x; //Power
printf(" |Power = %.3f megawatts| ", wind[index].power);
loopcount++;
printf("Data entered: %f : %f : %f ", wind[index].r, wind[index].speed, wind[index].power);
}
}
void marineTurbinePowerCalculator(turbine marine[row], float x, float k, float m, float waterDensity)
{
int loopcount, n = 1, flag = 1;
int c;
float tidalFlowSpeed;
float Area;
float Radius;
printf(" | Marine Turbine - Power Calculator | ");
printf("User can enter up to 10 turbines. ");
printf(">>Enter the number of marine turbines to be calculated: ");
scanf("%d", &n);
loopcount = 1;
while (loopcount <= n && loopcount < row + 1)
{
int index = loopcount - 1;
printf(">>Enter the radius for turbine |%d| in meters: ", loopcount);
scanf("%f", &Radius);
printf(">>Enter the tidal flow speed for turbine |%d| in meters/second: ", loopcount);
scanf("%f", &tidalFlowSpeed);
marine[index].r = Radius; //Turbine Radius
Area = area(Radius); //PI * pow(Radius, 2); //Area
marine[index].speed = tidalFlowSpeed; //Tidal Flow Speed
marine[index].power = power(waterDensity, Area, tidalFlowSpeed, k, x); //((waterDensity * Area * pow(tidalFlowSpeed, 3) * k) / 2) * x; //Power
printf(" |Power = %.3f megawatts| ", marine[index].power);
loopcount++;
printf("Data entered: %f : %f : %f ", marine[index].r, marine[index].speed, marine[index].power);
}
}
void analysisMarineTurbine(float m, float waterDensity)
{
float Power, tidalFlowSpeed, Radius;
printf(" | Analysis - Marine Turbine Specifications | ");
printf(">>Enter the power of a wind turbine in megawatts: ");
scanf("%f", &Power);
printf(">>Enter the tidal flow speed in meters/second: ");
scanf("%f", &tidalFlowSpeed);
Radius = sqrt((2 * Power) / (waterDensity * PI * pow(tidalFlowSpeed, 3) * m)) * 1000;
printf(" |Radius = %.3f meters| ", Radius);
printf(">>The marine turbine radius has to be %.3f meters in order to produce %.3f megawatts of power. ", Radius, Power);
}
void TurbineDat(turbine wind[row], turbine marine[row])
{
int ROW, c;
printf(" | Wind Turbine Data |: ");
printf("Turbine Radius(m) Wind Speed (m/sec) Power (MW) ");
for (ROW = 0; ROW < row; ++ROW)
if (wind[ROW].r != 0.0)
{
printf("%f ", wind[ROW].r);
printf("%f ", wind[ROW].speed);
printf("%f ", wind[ROW].power);
printf(" ");
}
printf(" | Marine Turbine Data |: ");
printf("Turbine Radius(m) Tidal Flow Speed (m/sec) Power (MW) ");
for (ROW = 0; ROW < row; ++ROW)
if (marine[ROW].r != 0.0)
{
printf("%f ", marine[ROW].r);
printf("%f ", marine[ROW].speed);
printf("%f ", marine[ROW].power);
printf(" ");
}
}
float area(float radius)
{
return PI * pow(radius, 2);
}
float power(float Density, float Area, float Speed, float k, float x)
{
return ((Density * Area * pow(Speed, 3) * k) / 2) * x;
}
Explanation / Answer
The bublesort algorithm is as follows:
In bubble sort, we do comparisons of adjacent elements. For example in case of array we will compare array[j] and array[j+1]. In case if array[j] is more than array[j+1] we swap the two values.We keep on doing that untill we reach end of the array.
So if we start the above mentioned opeartion with first and second element of the array then after the first pass, the maximum element will definatly reach at the last. Now again we will do the above mentioned stop up to n-1 as nth place is sorted (i.e it aleady conatins the correct element).
The above content is depicted in the code below:
We just need to replace the integer array with array of turbine calculations and we can get a sorted values. In the below function n represents the size of the array
void bubbleSort(int arr[], int n)
{
int i, j;
int temp;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}