/* Programmer: Dylan McDonald Class & Section: CS 54, Section Date: 18 September 2007 Purpose: To simulate a broken elevator and what happens to it */ #include #include #include using namespace std; int main() { /* well known constants (some better known than others) */ const double GRAVITY = 9.80665; const double ELEV_IMPULSE = (GRAVITY + 2.0) * 11.5931; const double TIME_STEP = .1; const double ROOM_HEIGHT = 42 * 3.2; const double DRAG_CONSTANT = 7.008; /* program variables */ int randomFall; bool fallen = false; double impulse = 0, mass, probability; double acceleration, fallTime = 0.0; double currVelocity, currDisplacement = 0.0; double prevVelocity = 0.0, prevDisplacement = 0.0; unsigned short i; /* run the elevator */ for (i = 0; i < 4; i++) { /* add the impulse from each ride */ do { cout << "Mass of elevator & passengers: "; cin >> mass; } while (mass < 100.0 || mass > 1000.0); impulse += ELEV_IMPULSE * mass; } cout << "The elevator has withstood " << impulse << " N * s of impulse." << endl; /* setup the probability */ srand(time(NULL)); randomFall = (rand() % static_cast(impulse)) + 1; probability = impulse / (impulse + randomFall); /* now see if it fell */ cout << "There is a " << (probability * 100.0) << "% chance the cable is broken!" << endl; probability *= 1000.0; randomFall = rand() % 1000; if (randomFall < probability) { fallen = true; cout << "All aboard the injury express! The cable broke!" << endl; } /* now watch it fall - wheeee! */ while (fallen && currDisplacement < ROOM_HEIGHT) { /* calculate our current three */ acceleration = ((mass * GRAVITY) - (DRAG_CONSTANT * prevVelocity * prevVelocity)) / mass; currVelocity = prevVelocity + acceleration * TIME_STEP; currDisplacement = prevDisplacement + currVelocity * TIME_STEP; /* save them as our previous three & advance time */ prevVelocity = currVelocity; prevDisplacement = currDisplacement; fallTime += TIME_STEP; } /* what happened? */ if (fallen) cout << "The impact velocity of the elevator and peoples was " << currVelocity << " m / s and took " << fallTime << " s to fall." << endl; return 0; }