// file: lecture_03/functs_00.cc // // local include files // #include "example.h" // function: mmult // // arguments: // float* c: output matrix // float* a: operands // float* b: operands // long nrows: number of rows // long ncols: number of cols // // return: returns a boolean value indicating status // // This method multiplies two matrices (c = a * b). // bool mmult(float* c, float* a, float* b, long nrows, long ncols) { // loop over the matrix a // for (long i = 0; i < nrows; i++) { for (long j = 0; j < ncols; j++) { c[i * nrows + j] = a[i * nrows + j] + b[i * nrows + j]; } } // exit gracefully // return true; } // function: minit // // arguments: // float* a: operands // long nrows: number of rows // long ncols: number of cols // // return: returns a boolean value indicating status // // This method initializes a matrix. // bool minit(float* a, long nrows, long ncols) { // loop over the rows // for (long i = 0; i < nrows; i++) { // loop over the cols // for (long j = 0; j < ncols; j++) { // set the value // if (i == j) { *a++ = (float)1.0; } else { *a++ = (float)0.0; } } } // exit gracefully // return true; } // function: mprint // // arguments: // float* a: operands // long nrows: number of rows // long ncols: number of cols // FILE* fp: a valid file pointer // // return: returns a boolean value indicating status // // This method prints a matrix. // bool mprint(float* a, long nrows, long ncols, FILE* fp) { // loop over the rows // for (long i = 0; i < nrows; i++) { // print the row // fprintf(fp, "%5d:", i); // loop over the cols // for (long j = 0; j < ncols; j++) { // print the value // fprintf(fp, "%10.4f ", *a++); } // print a new line // fprintf(fp, "\n"); } fprintf(fp, "\n"); // exit gracefully // return true; }