Monday, April 1, 2013

A function with two results

This program shows a function that generates two results. One result is returned, the other accessed from the main via an address


/* Problem: This program shows a function that generates two results. 
One result is returned (-), the other accessed from the main via an address (+). */



#include <stdio.h>
#include <math.h>

double
quadratic (int a, int b, int c, double* xplus)
{
 double xminus;

 xminus  = (-b - sqrt (b * b - 4 * a * c))/ (2 * a);
 *xplus  = (-b + sqrt (b * b - 4 * a * c))/ (2 * a);
 return (xminus);
}


int
main (void)
{
 int a = 10, b = 40, c = 30;
 double xplus, xminus;

 /* calling the function. the address of xplus is sent as an argument */
 xminus = quadratic (a, b, c, &xplus);
 
 printf ("Using +: %lf\n", xplus);
 printf ("Using -: %lf\n", xminus);

 return (0);
}

Legend: preprocessor directives | variable declarations | main program | helper functions | user-defined structures | comments
Using +: -1.000000
Using -: -3.000000

No comments:

Post a Comment