IHYPRESS.NET > C PROGRAMMING
HOME | CLASSIC ASP | C | HTML/CSS | JAVASCRIPT | PERL | PHP | PYTHON | RUBY
C > 06. Functions II (Pointers) [PROGRAM #06]
CODERESULT
<
/* This function takes a real number and separates it */
/* into three parts: a sign (a character value), a    */
/* whole part (an integer) and a fraction (double)    */

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

/* the function returns the sign but the pointer parameters */
/* will modifiy the corresponding variable arguments in    */
/* main program                                            */

char
separate (double number, int *w, double *f)
{
	char s;
	double magnitude;  /* the absolute value of the number */
	
	/* testing if positive or negative and assignation of sign */
	if (number < 0)
		s = '-';
	else	if (number == 0)
				s = ' ';
			else
				s = '+';

	magnitude = fabs (number);
	*w = floor (magnitude);
	*f = magnitude - *w;
	return (s);
}


int
main (void)
{
	double n; /* the original number */
	
	/* the variables that will be filled by the function */
	char sign;
	int whole;
	double fraction;

	printf ("Enter a real number: ");
	scanf ("%lf", &n);
	
	/* calling the function */
	sign = separate (n, &whole, &fraction);

	/* printing the report */
	printf ("Parts of %f: \n", n);
	printf ("===============\n\n");
	printf ("Sign is: %c \n", sign);
	printf ("Whole part is %d \n", whole);
	printf ("Fraction part is %f \n", fraction);

	return (0);
}



Legend: preprocessor directives | variable declarations main function | helper functions | user-defined structures comments
Enter a real number: -46.73
Parts of -46.730000:
===============

Sign is: -
Whole part is 46
Fraction part is 0.730000
>
COPYRIGHT © 2021 IHYPRESS.NET. A DIVISION OF IHY PRESS, KIRKLAND, WA 98083 USA. ALL RIGHTS RESERVED.