Here are my solutions to homework 1. Please let me know if you find an error!
Problem 3.3 in Austin/Chancogne.
/* * ========================================================== * prog_sphere.c : Compute volume of a sphere * * Written By: Mark Austin October 1996 * ========================================================== */ #include <stdio.h> /* Standard Input/Output function declarations */ #include <math.h> /* Math functions, such as sqrt(x). */ int main( void ) { float fRadius; /* Radius of sphere */ float fVolume; /* Volume of sphere */ /* [a] : Prompt User for radius of sphere */ printf("Please Enter radius of sphere : "); scanf("%f%*c", &fRadius ); /* [b] : Compute and print volume of sphere */ printf("Radius of sphere = %f\n", fRadius); fVolume = 4.0/3.0*M_PI*pow(fRadius, 3.0); printf("Volume of sphere = %f\n", fVolume); }
Problem 3.5 in Austin/Chancogne.
/* * =========================================================== * prog_triangle.c : Compute Area of Triangle * * -- Side lengths of triangle are read in from Keyboard * -- Area of triangle is printed to screen * * Written By: Mark Austin October 1996 * =========================================================== */ #include <stdio.h> /* Standard Input/Output function declaration */ #include <math.h> /* Mathematical function declaration */ #include <stdlib.h> /* For declaration of exit() function */ #define min(a, b) (((a) < (b)) ? (a) : (b)) #define min3(a, b, c) min(min(a, b), c) #define max(a, b) (((a) > (b)) ? (a) : (b)) #define max3(a, b, c) max(max(a, b), c) int main(void) { float fA, fB, fC; /* Side lengths A, B, C of triangle */ float fS; /* S = (A + B + C)/2 */ float fLongest, fShortest; /* longest and shortest side lengths */ float fMiddle; /* Middle side length */ float fArea; /* Computed Area of Triangle */ /* [a] : print program heading */ printf("Compute Area of Triangle with Predefined Side Lengths\n"); printf("=====================================================\n"); /* [b] : Prompt user for side lengths of triangle */ printf("Input Side Lengths of Triangle\n"); printf("Enter Side a : "); scanf("%f%*c", &fA); printf("Enter Side b : "); scanf("%f%*c", &fB); printf("Enter Side c : "); scanf("%f%*c", &fC); /* [c] : Print side lengths of the triangle */ printf( "Side Lengths are : %12.5g, %12.5g, %12.5g\n", fA, fB, fC ); /* [d] : If any of the side lengths is less than or equal to zero, exit */ if (fA <= 0.0 || fB <= 0.0 || fC <= 0.0) { printf("Cannot Compute Area - Lengths of triangle sides must all be \n"); printf(" greater than zero. \n"); exit(1); } /* [e] : If sum of shorter two sides is less than longest side, exit */ fLongest = max3(fA, fB, fC); fShortest = min3(fA, fB, fC); fMiddle = (fA + fB + fC) - (fLongest + fShortest); if (( fShortest + fMiddle ) <= fLongest ) { printf("Cannot Compute Area - Sum of shorter two sides is less than\n"); printf(" or equal to the longest side. \n"); exit(1); } /* [f] : Compute and print area of triangle */ fS = (fA + fB + fC)/2.0; fArea = sqrt(fS*(fS-fA)*(fS-fB)*(fS-fC)); printf("Computed Area of Triangle = %12.5g\n", fArea); }
Problem 4.6 in Austin/Chancogne.
/* * ========================================================= * prog_limits.c -- Show limits of various variable types. * * Written By: Mark Austin October 1996 * ========================================================= */ #include <limits.h> #include <float.h> #include <stdio.h> int main( void ) { /* [a] : print heading for output */ printf("================================================================= \n"); printf("TYPE BYTES BITS Minimum Maximum \n"); printf("================================================================= \n"); /* [b] : signed and unsigned characters */ printf("char (signed) "); printf("%4d", sizeof (char)); printf("%8d", CHAR_BIT*sizeof (char)); printf("%17i", SCHAR_MIN ); printf("%17i\n", SCHAR_MAX ); printf("char (unsigned) "); printf("%4d", sizeof (unsigned char)); printf("%8d", CHAR_BIT*sizeof (unsigned char)); printf("%17i", 0 ); printf("%17i\n", UCHAR_MAX ); /* [c] : signed, unsigned, long and short integers */ printf("int (signed) "); printf("%4d", sizeof (int)); printf("%8d", CHAR_BIT*sizeof (int)); printf("%17i", INT_MIN ); printf("%17i\n", INT_MAX ); printf("int (unsigned) "); printf("%4d", sizeof (unsigned int)); printf("%8d", CHAR_BIT*sizeof (unsigned int)); printf("%17i", 0 ); printf("%17u\n", UINT_MAX ); printf("int (long) "); printf("%4d", sizeof (long int)); printf("%8d", CHAR_BIT*sizeof (long int)); printf("%17i", LONG_MIN ); printf("%17i\n", LONG_MAX ); printf("int (unsigned long)"); printf("%4d", sizeof (unsigned long int)); printf("%8d", CHAR_BIT*sizeof (unsigned long int)); printf("%17i", 0 ); printf("%17u\n", ULONG_MAX ); /* [d] : floating point numbers */ printf("\n"); printf("float "); printf("%4d", sizeof (float)); printf("%8d", CHAR_BIT*sizeof (float)); printf("%17g", FLT_MIN ); printf("%17g\n", FLT_MAX ); printf("double "); printf("%4d", sizeof (double)); printf("%8d", CHAR_BIT*sizeof (double)); printf("%17g", DBL_MIN ); printf("%17g\n", DBL_MAX ); printf("double (long) "); printf("%4d", sizeof (long double)); printf("%8d", CHAR_BIT*sizeof (long double)); printf("%17Lg", LDBL_MIN ); printf("%17Lg\n", LDBL_MAX ); }
The output generated by this program on a DECSTATION 5000 is as follows:
================================================================= TYPE BYTES BITS Minimum Maximum ================================================================= char (signed) 1 8 -128 127 char (unsigned) 1 8 0 255 int (signed) 4 32 -2147483648 2147483647 int (unsigned) 4 32 0 4294967295 int (long) 4 32 -2147483648 2147483647 int (unsigned long) 4 32 0 4294967295 float 4 32 1.17549e-38 3.40282e+38 double 8 64 2.22507e-308 1.79769e+308 double (long) 16 128 3.3621e-4932 1.18973e+4932
Program output on other hardware platforms may be different.
Developed in February 2000 by Mark Austin
Copyright © 2000, Departments of Civil and Mechanical Engineering, University of Maryland