Here are my solutions to homework 1. Please let me know if you find an error!
Problem 3.2 in Austin/Chancogne.
/* * =================================================================== * prog_temperature.c : Converting Temperature in Celsius to Farenheit * * -- Temperature in degree Celsius is read in from keyboard. * -- Equivalent Temperature in Farenheit is printed to the screen * * Written By: Mark Austin October 1996 * =================================================================== */ # include <stdio.h> /* Standard Input/Output function declaration */ int main (void) { float fCels; /* Temperature in Celsius */ float fFaren; /* Temperature in Farenheit */ /* [a] : Print program header */ printf("Convert Temperature in Celsius to Fahrenheit\n"); printf("============================================\n\n"); /* [b] : Prompt user for temperature in degree Celsius */ printf("Input Temperature in Degree Celsius: "); fflush(stdout); scanf("%f", &fCels); /* [c] : Compute Equivalent Temperature in Farenheit */ fFaren = 9/5.0*fCels + 32.0; /* [d] : Print Results to Screen */ printf("\n"); printf("====================================================\n"); printf("User Defined Temperature in Degree Celsius : %6.2f\n", fCels); printf("Equivalent Temperature in Degree Farenheit : %6.2f\n", fFaren); }
Problem 3.4 in Austin/Chancogne.
/* * =========================================================== * Compute natural period of vibration for mass-spring system. * * Written By : Mark Austin April 1998 * =========================================================== */ #include <stdio.h> #include <math.h> #include <stdlib.h> int main( void ) { float fMass, fK1, fK2, fPeriod; /* Prompt user for coefficients of mass-spring system */ printf("Please enter values for mass and spring stiffness : \n"); printf("Enter mass m : "); scanf("%f%*c", &fMass); printf("Enter spring k1 : "); scanf("%f%*c", &fK1 ); printf("Enter spring k2 : "); scanf("%f%*c", &fK2 ); /* Print mass-spring parameters to screen */ printf("The parameters you have entered are:\n"); printf("Mass = %g\n", fMass ); printf("Stiffness k1 = %g\n", fK1 ); printf("Stiffness k2 = %g\n", fK2 ); /* Check that input values are positive */ if (fMass <= 0) { printf("Mass m must be greater than zero\n"); exit(1); } if (fK1 <= 0) { printf("Spring stiffness k1 must be greater than zero\n"); exit(1); } if (fK2 <= 0) { printf("Spring stiffness k2 must be greater than zero\n"); exit(1); } /* Compute natural period of mass-spring system */ fPeriod = 2 * M_PI * sqrt(fMass/(fK1 + fK2)); printf("Natural period = %g\n", fPeriod ); }
Problem 3.6 in Austin/Chancogne.
/* * =========================================================== * Compute area and perimeter of triangle. * * Written By : Mark Austin April 1998 * =========================================================== */ #include <stdio.h> #include <math.h> int main( void ) { float fX1, fX2, fX3; float fY1, fY2, fY3; float fSide1, fSide2, fSide3; float fS, fPerimeter, fArea; /* Print welcome message */ printf("Welcome to the triangle area and perimeter program\n"); printf("==================================================\n"); /* Prompt user for (x,y) coordinate values */ printf("Please enter (x,y) coordinate values : \n"); printf("Enter fX1 : "); scanf("%f%*c", &fX1 ); printf("Enter fY1 : "); scanf("%f%*c", &fY1 ); printf("Enter fX2 : "); scanf("%f%*c", &fX2 ); printf("Enter fY2 : "); scanf("%f%*c", &fY2 ); printf("Enter fX3 : "); scanf("%f%*c", &fX3 ); printf("Enter fY3 : "); scanf("%f%*c", &fY3 ); /* Print mass-spring parameters to screen */ printf("The coordinates you have entered are:\n"); printf("Point 1 : (x,y) = (%g,%g)\n", fX1, fY1 ); printf("Point 2 : (x,y) = (%g,%g)\n", fX2, fY2 ); printf("Point 3 : (x,y) = (%g,%g)\n", fX3, fY3 ); /* Compute triangle side lengths. */ fSide1 = sqrt( (fX1 - fX2)*(fX1 - fX2) + (fY1 - fY2)*(fY1 - fY2) ); fSide2 = sqrt( (fX2 - fX3)*(fX2 - fX3) + (fY2 - fY3)*(fY2 - fY3) ); fSide3 = sqrt( (fX3 - fX1)*(fX3 - fX1) + (fY3 - fY1)*(fY3 - fY1) ); /* Compute and print perimeter and area. */ fPerimeter = fSide1 + fSide2 + fSide3; fS = (fSide1 + fSide2 + fSide3)/2; fArea = sqrt( fS*(fS-fSide1)*(fS-fSide2)*(fS-fSide3) ); printf("Perimeter = %g\n", fPerimeter ); printf("Area = %g\n", fArea ); }
Example. Let's suppose that the executable program is called TRIANGLE. Here is a script of I/O:
prompt >> TRIANGLE Welcome to the triangle area and perimeter program ================================================== Please enter (x,y) coordinate values : Enter fX1 : 1 Enter fY1 : 1 Enter fX2 : 3 Enter fY2 : 4 Enter fX3 : 3 Enter fY3 : 1 The coordinates you have entered are: Point 1 : (x,y) = (1,1) Point 2 : (x,y) = (3,4) Point 3 : (x,y) = (3,1) Perimeter = 8.60555 Area = 3 prompt >>
from a typical run of the program.
Problem 4.2 in Austin/Chancogne.
#include <stdio.h> main ( void ) { int ii; for (ii = 0; ii < 26; ii = ii + 1 ) printf(" %c %d \t\t %c %d \n", 65 + ii , 65 + ii, 97 + ii, 97 + ii ); }
The abbreviated output is:
prompt >> ASCII A 65 a 97 B 66 b 98 C 67 c 99 D 68 d 100 E 69 e 101 ... lines of output removed ... Y 89 y 121 Z 90 z 122 prompt >>
Here, ASCII is the compiled program.
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.