


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Class: INTRO COMPUTER PROG FOR ENGR; Subject: Computer Engineering; University: University of Alabama - Huntsville; Term: Unknown 1989;
Typology: Exams
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Electrical and Computer Engineering
1 of 24
Electrical and Computer Engineering
2 of 24
int Power (/* in / int x, // Base number / in */ int n) // Power to raise base to { int result; // Holds intermediate powers of x result = 1; while (n > 0) { result = result * x; n--; } return result; } Usage y = Power(5, 3)
Electrical and Computer Engineering
3 of 24
#include
int Min (int val1, int val2, int val3);
int main() { int num1; int num2; int num3; int min_num;
Electrical and Computer Engineering
4 of 24
cout << "Please enter an integer value for num1: " << endl; cin >> num1; cout << "Please enter an integer value for num2: ” << endl; cin >> num2; cout << "Please enter an integer value for num3: " << endl; cin >> num3;
min_num = Min(num1, num2, num3);
cout << "The minimum number of the three entered is ” << min_num << endl; }
Electrical and Computer Engineering 5 of 24
int Min (int val1, int val2, int val3) { int result;
result = val1;
if (val2 < result) result = val2; if (val3 < result) result = val3;
return result; } Electrical and Computer Engineering 6 of 24
#include
float power (int base, int exponent);
int main() { float result; int base; int exponent;
cout << "Please enter integer base value " << endl; cin >> base;
Electrical and Computer Engineering
7 of 24
cout << "Please enter integer exponent value " << endl; cin >> exponent;
result = power(base, exponent); cout << base << " raised to the " << exponent << " power is " << result << endl; }
Electrical and Computer Engineering
8 of 24
float power (int base, int exponent) { int i =1; float result = 1.0;
while (i <= abs(exponent)) { result = result * base; i++; } if (exponent < 0) result = 1.0/result; return result; }
Electrical and Computer Engineering
9 of 24
Electrical and Computer Engineering
10 of 24
Electrical and Computer Engineering 11 of 24
Electrical and Computer Engineering 12 of 24
Electrical and Computer Engineering
19 of 24
float CrewMoment( /in/ int crew ) // Number of crew members
// Computes the crew moment arm in inch-pounds from the number of // crew members. Global constant PERSON_WT is used as the weight // of each crew member // Precondition: // crew == 1 OR crew == 2 // Postcondition: // Function value == Crew moment arm, based on the crew // parameter
{ const float CREW_DISTANCE = 143.0; // Distance to crew seats // from front
return float(crew) * PERSON_WT * CREW_DISTANCE; } Electrical and Computer Engineering
20 of 24
float PassengerMoment( /in/ int passengers ) { const float ROW1_DIST = 219.0; // Distance to row 1 seats const float ROW2_DIST = 265.0; // Distance to row 2 seats const float ROW3_DIST = 295.0; // Distance to row 3 seats const float ROW4_DIST = 341.0; // Distance to row 4 seats float moment = 0.0; // Running total of moment if (passengers > 6) // For passengers 7 and 8 { moment = moment + float (passengers - 6) * PERSON_WT * ROW4_DIST; passengers = 6; // 6 remain } if (passengers > 4).. // For passengers 5 and 6 . return moment; }
Electrical and Computer Engineering
21 of 24
float CargoMoment( /in/ int closet, // Weight in closet /in/ int baggage ) // Weight of baggage
// Computes the total moment arm for cargo loaded into the // front closet and aft baggage compartment // Precondition: // 0 <= closet <= 160 && 0 <= baggage <= 525 // Postcondition: // Function value == Cargo moment arm, based on the closet // and baggage parameters
{ const float CLOSET_DIST = 182.0; // Distance to closet const float BAGGAGE_DIST = 386.0; // Distance to baggage return float(closet) * CLOSET_DIST + float(baggage) * BAGGAGE_DIST; } Electrical and Computer Engineering
22 of 24
float FuelMoment( /* in */ int fuel ) // Fuel in gallons { float fuelWt; // Weight of fuel in pounds float fuelDistance; // Distance from front of plane
fuelWt = float(fuel) * LBS_PER_GAL; if (fuel < 60) fuelDistance = float(fuel) * 314.6; else if (fuel < 361) fuelDistance = 305.8 + (-0.01233 * float(fuel - 60)); else if (fuel < 521) fuelDistance = 303.0 + ( 0.12500 * float(fuel - 361)); else fuelDistance = 323.0 + (-0.04444 * float(fuel - 521)); return fuelDistance * fuelWt; }
Electrical and Computer Engineering 23 of 24
// Stub for GetYear function in the ConvertDates program
{ cout << “Get Year was called here. Returning \”1949\”.” << endl; year = “1948”; }
Electrical and Computer Engineering 24 of 24
float FuelMoment (int); int main() { int testVal; cout << “Fuel moment for gallons from 10 – 565 in steps” << “of 15:” << endl; testVal = 10; while (testVal <= 565) { cout << FuelMoment(testVal) << endl; testVal = testVal + 15; } }