/* * ===================================================================== * Program to dynamically allocate and print two small matrices * * Copyright (C) 1998 by Mark Austin and David Chancogne. * * This software is provided "as is" without express or implied warranty. * Permission is granted to use this software on any computer system, * and to redistribute it freely, subject to the following restrictions: * * 1. The authors are not responsible for the consequences of use of * this software, even if they arise from defects in the software. * 2. The origin of this software must not be misrepresented, either * by explicit claim or by omission. * 3. Altered versions must be plainly marked as such, and must not * be misrepresented as being the original software. * 4. This notice is to remain intact. * * Written by : Mark Austin * ===================================================================== */ #include #include "miscellaneous.h" /* function declarations */ void matPrint( char *, double **, int , int ); double ** matAllocSequentialDouble( int , int ); double ** matAllocIndirectDouble( int , int ); int main( void ) { enum { NoRows = 4 , NoColumns = 4 }; double **dppA; double **dppB; int iRow, iCol; /* [a] : Allocate memory for small 4 x 4 matrices */ dppA = matAllocSequentialDouble( NoRows , NoColumns ); dppB = matAllocIndirectDouble( NoRows , NoColumns ); /* [b] : Initialize matrix element values */ for( iCol = 1; iCol <= NoColumns; iCol++ ) for( iRow = 1; iRow <= NoRows; iRow++ ) { dppA [ iRow - 1 ][ iCol - 1 ] = iRow - iCol; dppB [ iRow - 1 ][ iCol - 1 ] = iRow - iCol; } /* [c] : Print matrix */ matPrint( "A", dppA, NoRows, NoColumns); matPrint( "B", dppB, NoRows, NoColumns); } /* * ==================================================================== * matAllocSequentialDouble() : Memory allocation of matrices with * sequential storage pattern. * * matAllocIndirectDouble() : Memory allocation of matrices with * indirect storage pattern. * * Input : int iNoRows -- number of rows in matrix. * int iNoColumns -- number of columns in matrix. * Output : double **dppM -- pointer to pointer to allocated matrix. * ==================================================================== */ double ** matAllocSequentialDouble( int iNoRows , int iNoColumns ) { double ** dppM; int iRow; dppM = (double **) safeMalloc( iNoRows * sizeof(double *), __FILE__, __LINE__ ); dppM [0] = (double *) safeMalloc( iNoRows * iNoColumns * sizeof(double), __FILE__, __LINE__ ); for( iRow = 1; iRow < iNoRows; iRow++ ) dppM [ iRow ] = dppM [ 0 ] + (iRow * iNoColumns); return ( dppM ); } double ** matAllocIndirectDouble( int iNoRows , int iNoColumns ) { double ** dppM; int iRow; dppM = (double **) safeMalloc( iNoRows * sizeof(double *), __FILE__, __LINE__); for( iRow = 0; iRow < iNoRows; iRow++ ) dppM [ iRow ] = (double *) safeMalloc( iNoColumns * sizeof(double), __FILE__, __LINE__ ); return ( dppM ); } /* * ======================================================================= * matPrint() : Naive implementation of matrix print * * Input : char *cpMatrixName -- pointer to matrix name. * double **dppM -- pointer to pointer to allocated matrix. * int iNoRows -- pumber of rows in matrix. * int iNoColumns -- pumber of columns in matrix. * ======================================================================= */ void matPrint( char *cpMatrixName , double **dppM , int iNoRows , int iNoColumns ) { int iRow, iCol; printf("\n\"MATRIX %s\"\n\n", cpMatrixName); for( iRow = 1; iRow <= iNoRows; iRow++ ) { for( iCol = 1; iCol <= iNoColumns; iCol++ ) printf(" %12.5f ", dppM [ iRow - 1 ][ iCol - 1 ] ); printf("\n"); } }