1 / 17

CS161 Introduction to Computer Science

CS161 Introduction to Computer Science. Topic #4. Today in CS161. Assignments/Questions What is needed for Program #2? Using the Math Library Input/Output and Formatting Sample Algorithm. To Raise to Power of. In C++ there is no operator that will raise some number by another

liseli
Download Presentation

CS161 Introduction to Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS161 Introduction to Computer Science Topic #4

  2. Today in CS161 • Assignments/Questions • What is needed for Program #2? • Using the Math Library • Input/Output and Formatting • Sample Algorithm CS161 Topic #4

  3. To Raise to Power of... • In C++ there is no operator that will raise some number by another • For example, for x3 you can’t type: • x**3 ILLEGAL! • Instead, you must use the pow function float answer; answer = pow(x, 3); CS161 Topic #4

  4. To Raise to Power of... • To use the power function, we must do two very important things... • include the math library: #include <math.h> • compile our programs using a -lm flag g++ -lm prog2.cpp CS161 Topic #4

  5. Next, to Format our Output • We must learn about precision • By default, real numbers are displayed with no more than 6 digits, plus the decimal point • This means that 6 significant digits are displayed in addition to the decimal point and a sign if the number is negative CS161 Topic #4

  6. Default Precision -- Examples float test; cout << “Please enter a real number”; cin >> test; cout << test; InputResulting Output 1.23456789 1.23457 10.23456789 10.2346 100.23456789 100.235 1000.23456789 1000.23 100000.23456789 100000 CS161 Topic #4

  7. To Change Precision float test; cout << “Please enter a real number”; cout.precision(3); //3 instead of 6!! cin >> test; cout << test << endl; InputResulting Output 1.23456789 1.23 10.23456789 10.2 100.23456789 100 10000.23456789 1e+04 (Exponential notation) CS161 Topic #4

  8. Another way to do this... #include <iomanip.h> float test; cout << “Please enter a real number”; cin >> test; cout <<setprecision(3) << test << endl; • setprecision is a manipulator • To use it, we must include the iomanip.h header file • There is no differencebetween • cout.precision(3) and cout <<setprecision(3) CS161 Topic #4

  9. What is “width”? • The width of a field can be set with: cout.width(size); • If what you are displaying cannot fit, a larger width is used • to prevent the loss of information • Important • Width is only in effect for the next output CS161 Topic #4

  10. How does width work... float test; cout.precision(4); cout.width(10); cin >>test; cout << test; cout <<endl <<test; InputResulting Output 1.23456789 1.235 1.235 CS161 Topic #4

  11. Another way to do this... #include <iomanip.h> float test; cout.precision(4); cin >>test; cout <<setw(10) << test; cout <<endl <<test; InputResultingOutput 1.23456789 1.235 1.235 CS161 Topic #4

  12. Trailing Zeros • For real numbers, trailing zeros are discarded when displayed • To display trailing zeros we use: cout.setf(ios::showpoint); InputResulting Output 1.2300 1.23 (for an precision of 3 or greater) CS161 Topic #4

  13. Displaying Trailing Zeros float test; cout.precision(4); cout.setf(ios::showpoint); cin >>test; cout << test <<endl; cout.unsetf(ios::showpoint); //reset... cout <<test; InputResultingOutput 1.2300 1.230 1.23 CS161 Topic #4

  14. Displaying Dollars and Cents! • There is another meaning to precision... • if we put in our programs: cout.setf(ios::fixed,ios::floatfield); • then, subsequent precision applies to the number of digits after the decimal point! cout.precision(2); cout <<test; 1.2300 1.23 1.20 1.2 • To display trailing zeros we use: cout.setf(ios::showpoint); InputResulting Output CS161 Topic #4

  15. Displaying Dollars and Cents! • Since we ALSO want trailing zero displayed...do all three: cout.setf(ios::fixed,ios::floatfield); cout.precision(2); cout.setf(ios::showpoint); cout <<test; 1.2300 1.23 1.20 1.20 • To display trailing zeros we use: cout.setf(ios::showpoint); InputResulting Output CS161 Topic #4

  16. Now... the Algorithm... • Your algorithm is not: • I sat down at the terminal • I got into the editor • I entered my program • I tried to compile it but got errors • I DON”T WANT TO SEE THIS!!!! CS161 Topic #4

  17. Your Algorithm... • First define the major tasks • Then break down these into subtasks • For example, the major tasks might be: • Welcome the user • Get the loan amount, interest rate, duration • Calculate the monthly payment • Display the results • Sign off Message Not Detailed Enough! Not Detailed Enough! But...a good start... CS161 Topic #4

More Related