Here are my solutions to homework 3. Please let me know if you find an error!
Problem 9.1 in Austin/Chancogne.
The abbreviated solution is:
/* * ====================================================================== * prog_quadratic4.c : This is Problem 9.1 in book. * * Solve Quadratic Equations : Coefficients are read in from keyboard * : Roots of Quadratic are printed to screen. * * Note : Algorithm takes into account possible loss of accuracy when * two floating point numbers of almost equal size are subtracted. * ====================================================================== */ #include <stdio.h> /* Standard Input/Output function declarations */ #include <math.h> /* Math functions, such as sqrt(x). */ #include <stdlib.h> /* Declaration for exit(1). */ float discriminant( float , float , float ); int mySign( float ); void readCoefficients ( float *, float *, float * ); int main( void ) { float fA, fB, fC; /* Coefficients for Quadratic Equation */ float fRoot1, fRoot2; /* Real Roots 1 and 2 of Quadratic Equation */ float fDiscriminant; /* Discriminant of Quadratic */ float fQ; /* Quotient for Numerical Algorithm */ char caBuffer[8]; /* Buffer array to hold input from screen */ /* [a] : Print Welcome Message */ printf("Welcome to the Quadratic Equation Solver (Version 1)\n"); printf("====================================================\n"); /* [b] : Read in Set of Equations */ printf("\nWould you like to input a quadratic quations (yes/no/quit) ? "); scanf("%s%*c", caBuffer); if(strcmp(caBuffer,"yes") != 0) exit (1); /* [c] : Read coefficients from the keyboard */ readCoefficients ( &fA, &fB, &fC ); /* [d] : Print Quadratic Equation to Screen */ printf("Equation you have entered is : %g.x^2 + %g.x + %g\n", fA, fB, fC); /* [e] : Compute Roots of simplified equations : fA equals zero */ .... details are the same as in the book .... /* [f] : Compute Roots of Quadratic Equation : fA not equal to zero */ .... details are the same as in the book .... } /* * =================================================== * Function to compute sign of a floating point number * =================================================== */ int mySign( float fX ) { .... details are the same as in the book .... } /* * ====================================================== * Function to Compute Discriminant of Quadratic Equation * ====================================================== */ float discriminant( float dA, float dB, float dC ) { return(dB*dB - 4.0*dA*dC); } /* * ====================================================== * readCoefficients () -- read coefficients from keyboard. * * Input : float *fpA -- Pointer to Coefficient "a". * : float *fpB -- Pointer to Coefficient "b". * : float *fpC -- Pointer to Coefficient "c". * Output : void. * ====================================================== */ void readCoefficients ( float *fpA, float *fpB, float *fpC ) { printf("Please enter coefficients for equation a.x^2 + b.x + c\n"); printf("Enter coefficient a : "); scanf("%f%*c", fpA ); printf("Enter coefficient b : "); scanf("%f%*c", fpB ); printf("Enter coefficient c : "); scanf("%f%*c", fpC ); }
Problem 9.2 in Austin/Chancogne.
/* * ====================================================================== * prog_countblanks.c : Count blanks in character string. * * Written By : Mark Austin March 1998 * ====================================================================== */ #include <stdio.h> /* Standard Input/Output function declarations */ #include <math.h> /* Math functions, such as sqrt(x). */ int countblanks( char * ); int main( void ) { char caMessage [] = "This string contains four blanks"; printf("The test string is : \"%s\"\n", caMessage ); printf("It contains %d blanks \n", countblanks( caMessage )); } /* * ==================================================================== * countblanks() : Count number of blanks in a character string. * * Input : char *cpString -- pointer to character string. * Output : int -- number of blanks in the character string. * ==================================================================== */ int countblanks( char *cpString ) { int ii, iNoBlanks = 0; ii = 0; while ( cpString[ii] != '\0' ) { if ( cpString[ii] == ' ' ) { iNoBlanks = iNoBlanks + 1; } ii = ii + 1; } return ( iNoBlanks ); }
Example. The output generated by the executable program BLANKS is:
prompt >> BLANKS The test string is : "This string contains four blanks" It contains 4 blanks prompt >>
Problem 9.3 in Austin/Chancogne.
/* * =============================================================== * Convert date from an integer to character string representation * * Written By : Mark Austin April 1998 * =============================================================== */ #include <stdio.h> #include <stdlib.h> /* Function declaration */ void DateToString( char *, int, int, int ); /* * ============================== * Test Program * ============================== */ int main( void ) { enum { LengthOfDate = 50 }; /* Length of array for date */ char caDate[ LengthOfDate ]; /* Character array for the date */ DateToString( caDate, 25, 12, 1998 ); printf("Christmas Day is : %s \n", caDate ); DateToString( caDate, 29, 2, 2000 ); printf("Last Day in February, 2000 is : %s \n", caDate ); DateToString( caDate, 31, 4, 1998 ); printf("Last Day in April, 1998 is : %s \n", caDate ); } /* * ====================================================================== * DateToString() : Convert integer representation of date into character * string. * * Input : char * -- Pointer to character string array. * : int iDay -- Day. * : int iMonth -- Month. * : int iYear -- Year. * Output : void. * ====================================================================== */ void DateToString( char *cpDate , int iDay, int iMonth, int iYear ) { int iaNoDaysInMonth[ 12 ] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; char *cpMonth[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; char caDay[8]; /* Check that "month" falls within defined ranges */ if( iMonth < 1 || iMonth > 12 ) { printf("Error : Month should be in range 1 through 12 i\n"); exit(1); } /* Check that "day" falls within defined ranges */ if( iYear%4 == 0 ) iaNoDaysInMonth[ 1 ] = 29; if(iDay < 1 || iDay > iaNoDaysInMonth [ iMonth - 1 ] ) { printf("Error : There are only %3d days in %s\n", iaNoDaysInMonth [ iMonth - 1 ], cpMonth [ iMonth-1 ] ); exit(1); } /* Build character array containing date */ sprintf (caDay,"%2d", iDay); strcpy (cpDate, caDay ); strcat (cpDate, "-"); strcat (cpDate, cpMonth[ iMonth-1 ]); strcat (cpDate, "-"); sprintf (caDay,"%2d", iYear); strcat (cpDate, caDay ); }
Example. Here is the program output:
prompt >> DATETOSTRING Christmas Day is : 25-December-1998 Last Day in February, 2000 is : 29-February-2000 Error : There are only 30 days in April prompt >>
Developed in February 2000 by Mark Austin
Copyright © 2000, Departments of Civil and Mechanical Engineering, University of Maryland