//----------------------------------------------------------------------------- // File: BioE215Lab1Part3DAJ.cpp // Class: None // Parent: None // Children: None // Purpose: Lab 1: Adding Matrices //----------------------------------------------------------------------------- // 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) #include "BioE215StringModule.h" // Additional String Functions created for BioE215 //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // Prototypes for local functions (functions not called by code in other files) //----------------------------------------------------------------------------- FILE* FileOpenWithMessageIfCannotOpen( const char *filename, const char *attribute); 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 AddMatricies( const double *arrayA, const double *arrayB, double *arraySum, unsigned int numberOfElements ); //----------------------------------------------------------------------------- int main( int numberOfCommandLineArguments, char *arrayOfCommandLineArguments[] ) { // Write " Hello math!" to the screen and then put a newline printf( "Hello Math Exercises 3!\n" ); // Open Input File FILE *inputFILE = FileOpenWithMessageIfCannotOpen("HelloMath3In.txt", "r"); FILE *outputFILE = FileOpenWithMessageIfCannotOpen("HelloMath3Out.txt", "w"); double arrayReadFromFile[250]; double arraySum[250]; GetDoubleMatrixElementsFromFile(arrayReadFromFile, 5, 7, inputFILE); AddMatricies(arrayReadFromFile, arrayReadFromFile, arraySum, 35); WriteDoubleMatrixElementsToFile(arraySum, 5, 7, 1, outputFILE); fclose(outputFILE); fclose(inputFILE); printf( "Press any key to exit the program."); int key = getchar(); // A normal program exit returns 0 (other return values usually signal an error) return 0; } //----------------------------------------------------------------------------- FILE* FileOpenWithMessageIfCannotOpen( const char *filename, const char *attribute) { FILE *returnValue = fopen(filename, attribute); if(returnValue == NULL) { printf("Error: The Specified File Could Not be Opened. Check File name and Attribute. \n"); } return returnValue; } //----------------------------------------------------------------------------- bool GetDoubleRowElementsFromFile( double *array, unsigned int numberOfCols, FILE *fptr) { bool didErrorOccur = false; float readValue = 0; char readInRowArray[50]; char *startPointer = readInRowArray; char *endPointer; char *errorCode = fgets(readInRowArray, 50, fptr); for(unsigned int i = 1; i <=numberOfCols; i++) { double matrixElement = strtod(startPointer, &endPointer); *(array + (i)) = matrixElement; startPointer = endPointer; endPointer = NULL; } if (errorCode == NULL) didErrorOccur = true; return didErrorOccur; } //----------------------------------------------------------------------------- bool GetDoubleMatrixElementsFromFile( double *array, unsigned int numberOfRows, unsigned int numberOfCols, FILE *fptr) { bool didErrorOccur = false; double *arrayStartPtr = array; double *arrayCurrentPtr = array; for(unsigned int i = 1; i <= numberOfRows; i++) { didErrorOccur = GetDoubleRowElementsFromFile(arrayCurrentPtr, numberOfCols, fptr); arrayCurrentPtr = array + numberOfCols*(i); } return didErrorOccur; } //----------------------------------------------------------------------------- bool WriteDoubleRowElementsToFile( const double *array, unsigned int numberOfCols, int precision, FILE *fptr) { bool didErrorOccur = false; for(unsigned int i = 1; i <= numberOfCols; i++) { didErrorOccur = WriteDoubleToFileInSpecifiedPrecision(*(array+i), precision, fptr); } return didErrorOccur; } //----------------------------------------------------------------------------- bool WriteDoubleMatrixElementsToFile( const double *array, unsigned int numberOfRows, unsigned int numberOfCols, int precision, FILE *fptr) { bool didErrorOccur = false; for(unsigned int i = 1; i <= numberOfRows; i++) { didErrorOccur = WriteDoubleRowElementsToFile(array + (i-1)*numberOfCols, numberOfCols, precision, fptr); fprintf(fptr, "\n"); } return didErrorOccur; } //----------------------------------------------------------------------------- void AddMatricies( const double *arrayA, const double *arrayB, double *arraySum, unsigned int numberOfElements ) { for (unsigned int i = 1; i <= numberOfElements; i++) { *(arraySum+i) = *(arrayA+i) + *(arrayB+i); } }