Solution for Exercise 16-7

ex1607

#include <stdio.h>

void half(void);
void twice(void);

int age;
float feet;

int main()
{
    printf("How old are you: ");
    scanf("%d",&age);
    printf("How tall are you (in feet): ");
    scanf("%f",&feet);
    printf("You are %d years old and %.1f feet tall.\n",age,feet);
    half();
    twice();
    printf("But you're not really %d years old or %.1f feet tall.\n",age,feet);
    return(0);
}

void half(void)
{
    float a,h;

    a=(float)age/2.0;
    printf("Half your age is %.1f.\n",a);
    h=feet/2.0;
    printf("Half your height is %.1f.\n",h);

}

void twice(void)
{
    age*=2;
    printf("Twice your age is %d.\n",age);
    feet*=2;
    printf("Twice your height is %.1f\n",feet);
}

Output

How old are you: 39
How tall are you (in feet): 6
You are 39 years old and 6.0 feet tall.
Half your age is 19.5.
Half your height is 3.0.
Twice your age is 78.
Twice your height is 12.0
But you're not really 78 years old or 12.0 feet tall.

Notes

* This code could easily have been written without using external variables. In fact, if you're for it, consider this challenge: Re-write this code so that external variables aren't required. Give yourself five For Dummies bonus points if you attempt this challenge. Click here to see my bonus solution.