/* Example on functions ____________________________________ Jacob Y. Kazakia jyk0 Ocober 18, 2000 Programming assignment Recitation Instructor: J.Y.Kazakia Recitation Section 01 ____________________________________ Purpose: This program illustrates the use of a function which returns two values to the calling function. The main function provides the type and the length of a steel beam ( in feet ) and receives from the function weight the weight of the beam ( in pounds ) and an estimate of the error in the calculation of the weight. Algoritm: The algorithm of the calculation is described in the function weight below. */ #include #include void weight ( int k, double length, double lerror, double& w, double& werror); main() { int k = 2; // The beam is of type 2 double L = 12.4; // the length of the beam in feet double E = 0.001; // the(+ or - )error in the length specification in feet double wbeam = 0.0 ; // the weight of the beam double werror=0.0 ; // the error (+ or -) in the calculation of the weight cout << setiosflags(ios::fixed) << setprecision (2); weight ( k, L, E, wbeam, werror); // USING THE FUNCTION weight cout << "\n\n The weight of the beam is : " << wbeam ; cout << " lb with a possible error of " << werror << " lb"; cout << "\n\n enter a character and hit enter to exit "; char hold; cin >> hold; } // DEFINITION OF THE FUNCTION WEIGHT void weight ( int k, //the type of the beam (input) double length, //the length of the beam ( input) double lerror, //the error in the length of the beam(input) double& w, //the calculated weight (output) double& werror) //the estimate of the error in the weight(output) /* Purpose: This function calculates the weight of a beam, from its length and its type. It also provides an estimate of the error in the weight calculation. Algorithm: In this function we consider only three different types of beams: Type 1: s24x100 cross sectional area: 29.4 sq in. with an error of (+ or -) 0.02 sq in. Type 2: s10x35 cross sectional area: 10.3 sq in. with an error of (+ or -) 0.01 sq in. Type 1: c12x30 cross sectional area: 8.82 sq in. with an error of (+ or -) 0.002 sq in. The specific weight of steel is 0.284 lb / cubic in. The weight of the beam w is calculated as: w = (length * 12 )* cross sec. area * specific weight The error in the weigth calculation is obtained by the difference of the weight so obtained and the weight obtained using increased length and inreased cross sectional area ( increased by the amount of error indicated). */ { double area; // cross sectional area in square inches ( local variable) double aerror; // error in crosectional area ( local variable) double sw = 0.284;// specific weight in lb per cubic in. ( local variable) double wplus ; // the weight of increased dimensions ( local variable) if ( k == 1) { area = 29.4; aerror = 0.02; } else if ( k == 2 ) { area = 10.3; aerror = 0.01; } else if ( k == 3) { area = 8.82; aerror = 0.002; } else { cout << "\n\n invalid type " ; area = 0.0 ; aerror = 0.0; } w = area * length * 12.0 * sw; wplus = ( area + aerror) * (length + lerror)* 12 * sw ; werror = wplus - w; } /* THE OUTPUT : The weight of the beam is : 435.27 lb with a possible error of 0.46 lb enter a character and hit enter to exit */