I'm trying to return the solutions of the pq-formula as a dynamically created array. What's the correct way to do that? This is my function:
double *pq (double a, double b) { double x1=(-1)*(a/2)-sqrt((a/2)*(a/2)-b); double x2=(-1)*(a/2)+sqrt((a/2)*(a/2)-b); double *arr[]=(double *)malloc(2*sizeof(double)); arr[2]={{x1}, {x2}}; return arr; } Also, why do I get an 'expected an expression' error on arr[2]={{x1}, {x2}}; ?
My main function:
int main () { double *arr[2]={0}, a=0.00, b=0.00; scanf("%lf %lf", a,b); if ((a*a)-(b*a)>=0) { for (int i=0; i<2; i++) { arr[i] = pq(a,b); } } else { printf("Es gibt keine reellen L\224sungen."); } for (int i=0; i<2;i++) { printf("%lf", arr[i]); } return 0; }
arrvariable is the wrong type, and even if corrected you'll breach your array (indexing is zero based, so[1]is the max index for an array of length 2).