/* * ====================================================================== * Demonstrate -- Dynamic Memory Allocation for Vectors * -- Printing of Vectors * -- Use of Dynamic Allocation in Vector Addition * ====================================================================== * Copyright (C) 1995-96 by Mark Austin and David Mazzoni. * * 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 July 1995 * ====================================================================== */ #include #include "miscellaneous.h" /* Declarations for vector functions */ void vecPrint( char *, float *, int ); float *vecAdd( float *, float *, int ); int main( void ) { enum { VectorLength = 3 }; float *fpA, *fpB, *fpC; /* [a] : Allocate and initialize vectors "A" and "B" */ fpA = (float *) safeCalloc( VectorLength, sizeof(float) , __FILE__, __LINE__ ); fpA [0] = 1; fpA [1] = 2; fpA [2] = 3; fpB = (float *) safeCalloc( VectorLength, sizeof(float) , __FILE__, __LINE__ ); fpB [0] = -3; fpB [1] = 4; fpB [2] = 5; /* [b] : Compute "A + B" */ fpC = vecAdd( fpA, fpB , VectorLength ); /* [c] : Print Contents of Vectors */ vecPrint( "A", fpA , VectorLength ); vecPrint( "B", fpB , VectorLength ); vecPrint( "A+B", fpC , VectorLength ); /* [d] : Free vector memory */ free ( fpA ); free ( fpB ); free ( fpC ); } /* * ================================================================ * vecAdd() : Addition of two vectors * * Input : float *fpA -- pointer to float array * float *fpB -- pointer to float array * int iLength -- length of float array * Output : float *fpC -- pointer to array containing vector sum * ================================================================ */ float *vecAdd( float *fpA, float *fpB, int iLength ) { float *fpC; int iRow; fpC = (float *) safeCalloc( iLength, sizeof(float) , __FILE__, __LINE__ ); for(iRow = 0 ; iRow < iLength; iRow++ ) fpC [ iRow ] = fpA [ iRow ] + fpB [ iRow ]; return ( fpC ); } /* * ================================================================ * vecPrint() : Naive Implementation of Vector Print * * Input : char *cpMessage -- pointer to vector message. * float *fpVector -- pointer to float array * int iLength -- length of float array * Output : void. * ================================================================ */ void vecPrint( char *cpMessage , float *fpVector , int iLength ) { int iRow; if(cpMessage != (char *) NULL) printf ("\nVECTOR \"%s\" \n\n", cpMessage ); else printf("\nVECTOR : \"UNTITLED\" \n\n"); for(iRow = 1; iRow <= iLength; iRow++) printf(" %3d %16.5e\n", iRow, fpVector[ iRow-1 ]); }