3. Procedures - ENGR 101 (Sec 100), Fall 2010 1 Functions...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ENGR 101 (Sec 100), Fall 2010 1 Functions and Procedures ENGR 101, Lecture 3: 15 Sep 10 Announcements Project 1 due tonight (Wed 9pm) Look for Project 2 release by then Warning: Considerably more difficult, time consuming Due Wednesday, 22 Sep at 9pm Example: Your Weight on Mars Weight : the force exerted on a body by gravity The Universal Law of Gravitation states For earth, define Your weight on earth: W = GmM R 2 R =radius M =planet mass m =your mass G =6.672x10-11 N m 2 /kg 2 g = GM E R E 2 9.8 m / s 2 Your Weight on Mars Using these formulas, we can calculate the ratio of a bodys weight on another planet P to its weight on earth: Given weight on earth and the mass and radius of any planet, the bodys weight on that planet is: W P W E = GmM P R P 2 mg = GM P gR P 2 = 6.8 10 12 M P R P 2 W P = 6.8 10 12 M P R P 2 W E ENGR 101 (Sec 100), Fall 2010 2 Writing the Program What do we want it to do? What is your weight on earth? 175 Weight on Mars is 65.9 Start with Scaffolding #include <iostream> using namespace std; int main( ){ return 0; } Input and Output Statements #include <iostream> using namespace std; int main( ){ cout << "What is your weight on earth?" << endl; cin >> earthweight; cout << "Weight on mars is " << marsratio * earthweight << endl; return 0; } Declare Variables #include <iostream> using namespace std; int main( ){ double marsmass = 6.4e23; double marsrad = 3.4e6; double earthweight; double marsratio; cout << "What is your weight on earth?" << endl; cin >> earthweight; cout << "Weight on mars is " << marsratio * earthweight << endl; return 0; } ENGR 101 (Sec 100), Fall 2010 3 Add the Central Equation #include <iostream> using namespace std; int main( ){ double marsmass = 6.4e23; double marsrad = 3.4e6; double earthweight; double marsratio; cout << "What is your weight on earth?" << endl; cin >> earthweight; marsratio = 6.8e-12 * marsmass/(marsrad*marsrad); cout << "Weight on mars is " << marsratio * earthweight << endl; return 0; } Add Comments for the Reader #include <iostream> // by program author using namespace std; // Computes your weight // when on mars int main( ){ double marsmass = 6.4e23; // mass of mars double marsrad = 3.4e6; // radius of mars double earthweight; // weight on earth double marsratio; // wt ratio mars::earth cout << "What is your weight on earth?" << endl; cin >> earthweight; // compute the weight of the person on Mars marsratio = 6.8e-12 * marsmass/(marsrad*marsrad); cout << "Weight on mars is " << marsratio * earthweight << endl; return 0; } What if you wanted to calculate your weight on Jupiter too? Computing Weight on Two Planets #include <iostream> // by program author using namespace std; // Computes your weight // when on mars and jupiter int main( ){ double marsmass = 6.4e23; // mass of mars double marsrad = 3.4e6; // radius of mars double jupmass = 1.9e27; // mass of jupiter double juprad = 7.0e7; // radius of jupiter double earthweight;...
View Full Document

This note was uploaded on 05/25/2011 for the course ENGR 101 taught by Professor Ringenberg during the Fall '07 term at University of Michigan.

Page1 / 19

3. Procedures - ENGR 101 (Sec 100), Fall 2010 1 Functions...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online