The third chapter in Stephen Prata’s C Primer Plus.
Programming exercises and their solutions:
1. Experiment with how your system handles integer and floating point overflows and floating point underflows; those. write a program that has such problems. (See the discussion of limits.h and float.h in Chapter 4 for the largest and smallest values.)
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main(void)
{
printf("Max int value = %d\n", INT_MAX);
printf("Max float val = %e\n", FLT_MAX);
printf("Min float val = %e\n", FLT_MIN);
printf("Int overflow: %d + 1 = %d\n", INT_MAX, INT_MAX + 1);
printf("Float-point overflow: %e * 2 = %e\n", FLT_MAX, FLT_MAX * 2);
printf("Float-point underflow: %e / 2 = %e", FLT_MIN, FLT_MIN / 2);
getchar();
return 0;
}
2. Write a program that prompts you to enter some value in ANSI code, such as 66, and then prints out the character that corresponds to the entered code.
#include <stdio.h>
int main(void)
{
char ascii;
printf("Enter ASCII code:\n");
scanf("%d", &ascii);
printf("ASCII code for %d is %c", ascii, ascii);
getchar();getchar();
return 0;
}
3. Write a program that emits a warning beep and then outputs the following text:
Frightened by the sudden sound, Vika cried out:
“In the name of all the stars, what was that!”
#include <stdio.h>
int main(void)
{
printf("\aScared by sudden sound, Vika yelled:\n");
printf("\"By the all stars, what was it!\"");
getchar();
return 0;
}
4. Write a program that reads a floating point number and outputs it first in decimal notation, then exponential notation, and then binary exponential notation if the system supports it. The output should be in the following format (the actual number of exponent digits displayed varies by system):
Enter floating point value: 64.25
Fixed point notation: 64.250000
Exponential notation: 6.425000e+Ol
binary exponential notation: Oxl.Olp+6
#include <stdio.h>
int main(void)
{
float num1;
printf("Enter float value: ");
scanf("%f", &num1);
printf("Fixed-point: %f\n", num1);
printf("Exponential: %e\n", num1);
printf("P notation: %a\n", num1); // doesn't work in ANSI C
getchar();getchar();
return 0;
}
5. There are approximately 3.156 x 107 seconds in a year. Write a program that prompts you to enter your age in years and then displays the equivalent value in seconds.
#include <stdio.h>
int main(void)
{
int years, seconds;
printf("Your age:\n");
scanf("%d", &years);
seconds = years * 3.156e7;
printf("Your live already more than %d seconds", seconds);
getchar();getchar();
return 0;
}
6. The mass of one water molecule is approximately 3.0 x 10-23 grams. A quart of water weighs approximately 950 grams. Write a program that prompts you to enter the volume of water in quarts and displays the number of water molecules in that volume.
#include <stdio.h>
int main(void)
{
float quarts, grams, molecules;
printf("How much water do you drink daily (in quarts):\n");
scanf("%f", &quarts);
grams = quarts * 950;
molecules = grams / 3.0e-23;
printf("You consume daily %e water molecules", molecules);
getchar();getchar();
return 0;
}
7. There are 2.54 centimeters in an inch. Write a program that prompts you to enter your height in inches and then displays that height in centimeters. Or, if you prefer, the program can ask for height in centimeters and convert it to inches.
#include <stdio.h>
int main(void)
{
float height_cm, height_inches;
float cm_to_inch = 2.54;
printf("Your height in cm:\n");
scanf("%f", &height_cm);
height_inches = height_cm / cm_to_inch;
printf("Your height in inches is %f", height_inches);
getchar();getchar();
return 0;
}
8. In the American system of measurement units, a pint is 2 cups, a cup is 8 ounces, an ounce is 2 tablespoons, and a tablespoon is 3 teaspoons. Write a program. which prompts you to enter the volume in cups and displays the equivalent values in pints, ounces, tablespoons, and teaspoons. Why is floating point better than integer type for this program?
#include <stdio.h>
int main(void)
{
float cups, pints, ounces, tablespoons, teaspoons;
printf("Enter number of cups:\n");
scanf("%f", &cups);
pints = cups / 2;
ounces = cups * 8;
tablespoons = ounces * 2;
teaspoons = tablespoons * 3;
printf("%.2f cups = %.2f pints\n", cups, pints);
printf("%.2f cups = %.2f ounces\n", cups, ounces);
printf("%.2f cups = %.2f tablespoons\n", cups, tablespoons);
printf("%.2f cups = %.2f teaspoons", cups, teaspoons);
getchar();getchar();
return 0;
}
I will be glad to see your comments!