/* Filename: HW7.c*/ /* This program solves the quadratic formula */ /****************************************************************** * * * Programmer: Matthew Glynn * * Date : 03/21/2007 * * Section Number: B36 * * Filename: HW7.c * * Description: This program solves the quadratic formula * * and checks for invalid input * ******************************************************************/ /* libraries to be used */ #include #include #include #include /* Declare functions */ void readcoefficients ( int *a, int *b, int *c ); void factors( int *a, int *b, int *c ); void quadraticcoefficient( int *pointer ); int main ( void ) {/* start main */ int valuea, valueb, valuec; int *a=&valuea, *b=&valueb, *c=&valuec; printf("\n This is a quadratic-equation calculator\n"); printf("\n Format: ax^2 + bX + c \n\n"); readcoefficients( a, b, c ); factors( a, b, c ); return 0; }/* end main */ /****************************************************************** * * * Programmer: Matthew Glynn * * Name: readcoefficients * * Function: This function prints the main menu of the program. * * Algorithm: uses printf to display messages and the * * quadraticcoefficient function * * Input: int *a, int *b, int *c * * * ******************************************************************/ void readcoefficients( int *a, int *b, int *c ) { printf("\n Enter the value of a: "); quadraticcoefficient( a ); /*a becomes quadraticcoefficient pointer after function is ran*/ printf("\n Enter the value of b: "); quadraticcoefficient( b ); printf("\n Enter the value of c: "); quadraticcoefficient( c ); } /****************************************************************** * * * Programmer: Matthew Glynn * * Name: factors * * Function: This function solves the quadratic formula * * and checks for invalid input * * Algorithm: uses pointers s, b, and c in the quadratic formula * * Input: int *a, int *b, int *c * * * ******************************************************************/ void factors(int *a, int *b, int *c) { double disc, X1, X2; disc = (*b)*(*b) - 4*(*a)*(*c); if(disc >=0 ) { disc = sqrt ( disc );/*works*/ X1 = (-(*b) + disc ) / 2*(*a); X2 = (-(*b) - disc ) / 2*(*a); printf("\n The roots are:\n\n\t%1f\n and\n\t%1f\n\n", X1, X2); } else { printf("\n The roots are a complex conjugative pair: \n"); printf("\n\t%d + %c%.01f", -(*b), 251, disc); printf("\n\t--------------------\n\t\t%d\n and \n",2*(*a)); printf("\n\t%d + %c%.01f", -(*b), 251, disc); printf("\n\t--------------------\n\t\t%d\n and \n",2*(*a)); } return 0; } /****************************************************************** * * * Programmer: Matthew Glynn * * Name: quadraticcoefficient * * Function: brings in coefficients and checks for bad input * * Algorithm: This function puts coefficients as pointers * * Input: int *pointer * ******************************************************************/ void quadraticcoefficient ( int *pointer ) { char array1[10]; int number, counter, coefficient; do { gets( array1 );/*get input from user*/ number = 1; for( counter = 0; array1[counter] != '\0'; counter = counter + 1 ) { if( !isdigit( array1[counter] )) /*check for if input is digit from 1-10*/ { number = 0; break; } } if( number == 0 ) { printf("\n\t Error: bad input; Please try again\n\n\t\tvalue:"); } else { coefficient = atoi(array1); /*use array to integer*/ } }while ((number == 0)||(coefficient ==0)); *pointer = coefficient; } /************************************************************************* * Programmed by: Matt Glynn * *************************************************************************/