I got a problem where I have to first create a function that calculates the vertex height of a given struct of parabolas and returns 0 for parabola und 1 if not. Then sort them with qsort using the vertex height as parameter which ascends. Also if a == 0 (not a parabola), those will be sorted after the parabolas. Then I have to use a test-main to print example-parabolas and see if they got sorted correctly. Global variables are not allowed.

I think I got the calculation of vertex height, but I have no clue what I have to write into the function for qsort, tutorials say that I have to compare a and b and return 1, -1 or 0, but I don't know how to "translate" that for my case. I'm kinda new to programming and have not quite gotten the concept of pointers or referncing functions in c.

The given header.h

Code:

#ifndef header
#define header 1
struct parabola {
double a;
double b;
double c;
};
int vertexheight(struct parabola * p, double *y);
void sort_parabola(struct parabola * p, int n);
#endif

my program

Code:

#include <stdio.h>
#include <stdlib.h>
#include "header.h"
int compare();
int vertexheight(struct parabola * p, double *y) {
int rc = 0;
if (p->a == 0) {
rc = 1;
} else {
*y = p->c - ((p->b*p->b) / (4*p->a));
}
return rc;
}
int compare(const void * a, const void * b) {
//?
return 0;
}
void sort_parabola(struct parabola * p, int n) {
qsort(p, n, sizeof(struct parabola), compare);
}
int main() {
struct parabola p[] = {
{1,2,3},
{2,5,-19}, {0,-100,-56}, {-967,24,-24}, {36,2,70},
{5,72,0}, {75,-4,55}, {20,41,7},
{-1,0,0}
};
double y;
int i, size = sizeof(p) / sizeof(struct parabola);
sort_parabola(p, sizeof(p) / sizeof(struct parabola));
for (i = 0; i < size; i++){
//output
}
return 0;
}

The sorted output should be something like this:

Code:

[0]
{5.000000,72.000000,0.000000},
y=-259.200000
[1]
{-967.000000,24.000000,-24.000000},
y=-23.851086
[2]
{2.000000,5.000000,-19.000000},
y=-22.125000
[3]
{20.000000,41.000000,7.000000},
y=-14.012500
[4]
{-1.000000,0.000000,0.000000},
y=0.000000
[5]
{1.000000,2.000000,3.000000},
y=2.000000
[6]
{75.000000,-4.000000,55.000000},
y=54.946667
[7]
{36.000000,2.000000,70.000000},
y=69.972222

Thanks a lot if someone can help me with this!