//----------------------------------------------------------------------------- // File: HelloMath3.cpp // Class: None // Parent: None // Children: None // Purpose: Reads a file with a matrix, performs operations, writes result to file //----------------------------------------------------------------------------- // The following are standard C/C++ header files. // If a filename is enclosed inside < > it means the header file is in the Include directory. // If a filename is enclosed inside " " it means the header file is in the current directory. #include // Character Types #include // Mathematical Constants #include // Variable Argument Lists #include // Standard Input/Output Functions #include // Utility Functions #include // String Operations #include // Signals (Contol-C + Unix System Calls) #include // Nonlocal Goto (For Control-C) #include // Time and Date information #include // Verify Program Assertion #include // Error Codes (Used in Unix system()) #include // Floating Point +Constants #include // Implementation Constants #include // Standard Definitions #include // Exception handling (e.g., try, catch throw) //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // Prototypes for local functions (functions not called by code in other files) //----------------------------------------------------------------------------- bool GetStringFromKeyboard( char inputString[], unsigned long sizeOfString ); bool GetStringFromFile( char inputString[], unsigned long sizeOfString, FILE *fptr ); bool GetDoubleRowElementsFromFile( double *array, unsigned int numberOfCols, FILE *fptr); bool GetDoubleMatrixElementsFromFile( double *array, unsigned int numberOfRows, unsigned int numberOfCols, FILE *fptr); bool WriteDoubleRowElementsToFile( const double *array, unsigned int numberOfCols, int precision, FILE *fptr ); bool WriteDoubleMatrixElementsToFile( const double *array, unsigned int numberOfRows, unsigned int numberOfCols, int precision, FILE *fptr); void AddMatrices( const double *arrayA, const double *arrayB, double *arraySum, unsigned int numberOfElements ); bool WriteDoubleToFileWithSpaceAndSpecifiedPrecision( double x, int precision, FILE *fptr); const char* ConvertStringToDouble( const char *s, double &returnValue, double defaultValue ); const char* ConvertStringToLong( const char *s, long &returnValue, long defaultValue ); FILE* FileOpenWithMessageIfCannotOpen( const char *filename, const char *attribute); //----------------------------------------------------------------------------- int main( void ) { // Write " Hello math!" to the screen and then put a newline printf( " Hello math!\n" ); // Open a file to read data FILE *fptr = FileOpenWithMessageIfCannotOpen( "HelloMath3In.txt", "r" ); // End program if file error if( fptr == NULL ) return 1 ; // Create a 5x7 array of doubles and read the elements of the array from the file double arrayMatrixIn[35]; GetDoubleMatrixElementsFromFile( arrayMatrixIn, 5, 7, fptr ); // Calculate M+M double arrayMatrixOut[35]; AddMatrices( arrayMatrixIn, arrayMatrixIn, arrayMatrixOut, 35 ); // Write results to file HelloMath3Out.txt FILE *fptr2 = FileOpenWithMessageIfCannotOpen( "HelloMath3Out.txt", "w+" ); WriteDoubleMatrixElementsToFile( arrayMatrixOut, 5, 7, 1, fptr2); // Keep the screen displayed until the user presses the Enter key printf( "\n\n Press Enter to terminate the program: " ); int key = getchar(); return 0; } //----------------------------------------------------------------------------- bool GetStringFromKeyboard( char inputString[], unsigned long sizeOfString ) { return GetStringFromFile( inputString, sizeOfString, stdin ) ; } //----------------------------------------------------------------------------- bool GetStringFromFile( char inputString[], unsigned long sizeOfString, FILE *fptr ) { return fgets( inputString, sizeOfString, fptr ) != NULL; } //----------------------------------------------------------------------------- bool GetDoubleRowElementsFromFile( double *array, unsigned int numberOfCols, FILE *fptr ) { // Reserve enough space to get the longest line in the file. char inputLineFromFile[200]; // Get the line from the file and return false if failed bool ableToGetLine = GetStringFromFile( inputLineFromFile, 200, fptr ); if( !ableToGetLine ) return false; // Get each number in the line const char* pointerToCharacterAfterNumber = inputLineFromFile; for( unsigned int i=0; i < numberOfCols; i++ ) { double nextValueInString; pointerToCharacterAfterNumber = ConvertStringToDouble( pointerToCharacterAfterNumber, nextValueInString, 1.0 ); // Check to ensure a valid number was encountered if( pointerToCharacterAfterNumber == NULL ) return false; // Otherwise, stuff the valid number into the array array[i] = nextValueInString; } // Everything succeeded return true; } //----------------------------------------------------------------------------- bool GetDoubleMatrixElementsFromFile( double *array, unsigned int numberOfRows, unsigned int numberOfCols, FILE *fptr) { double *rowArray = array; for( unsigned int i=0; i < numberOfRows; i++ ) { if( GetDoubleRowElementsFromFile( rowArray, numberOfCols, fptr ) == false ) return false; // To get the next row, move the rowArray pointer to the proper location in array rowArray += numberOfCols; } // Everything succeeded return true; } bool WriteDoubleToFileWithSpaceAndSpecifiedPrecision( double x, int precision, FILE *fptr) { // Ensure the precision (number of digits in the mantissa after the decimal point) makes sense. // Next, calculate the field width so it includes one extra space to the right of the number. if( precision < 0 || precision > 17 ) precision = 5; int fieldWidth = precision + 4; // Create the format specifier and print the number char format[20]; sprintf( format, "%%- %d.%dg", fieldWidth, precision ); //fprintf(fptr,"%%- %g",x); return fprintf( fptr, format, x ) > 0; } //----------------------------------------------------------------------------- bool WriteDoubleRowElementsToFile( const double *array, unsigned int numberOfCols, int precision, FILE *fptr ) { for( unsigned int i=0; i < numberOfCols; i++ ) { double nextElement = array[i]; WriteDoubleToFileWithSpaceAndSpecifiedPrecision( nextElement, 3, fptr); } fprintf( fptr, "\n" );\ return true; } //----------------------------------------------------------------------------- bool WriteDoubleMatrixElementsToFile( const double *array, unsigned int numberOfRows, unsigned int numberOfCols, int precision, FILE *fptr) { const double *rowArray = array; for( unsigned int i=0; i < numberOfRows; i++ ) { WriteDoubleRowElementsToFile( rowArray, numberOfCols, precision, fptr ); rowArray += numberOfCols; } return true; } //----------------------------------------------------------------------------- void AddMatrices( const double *arrayA, const double *arrayB, double *arraySum, unsigned int numberOfElements ) { for( unsigned int i=0; i