Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS161 Introduction to Computer Science Topic #4.

Similar presentations


Presentation on theme: "1 CS161 Introduction to Computer Science Topic #4."— Presentation transcript:

1 1 CS161 Introduction to Computer Science Topic #4

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

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

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

5 CS161 Topic #45 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

6 CS161 Topic #46 Default Precision -- Examples float test; cout << “Please enter a real number”; cin >> test; cout << test; InputResulting Output 1.234567891.23457 10.2345678910.2346 100.23456789100.235 1000.234567891000.23 100000.23456789100000

7 CS161 Topic #47 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.234567891e+04 (Exponential notation)

8 CS161 Topic #48 Another way to do this... #include 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 difference between cout.precision(3)andcout <<setprecision(3)

9 CS161 Topic #49 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

10 CS161 Topic #410 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

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

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

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

14 CS161 Topic #414 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.23001.23 1.201.2 To display trailing zeros we use: cout.setf(ios::showpoint); InputResulting Output

15 CS161 Topic #415 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.23001.231.20 To display trailing zeros we use: cout.setf(ios::showpoint); InputResulting Output

16 CS161 Topic #416 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!!!!

17 CS161 Topic #417 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! But...a good start...


Download ppt "1 CS161 Introduction to Computer Science Topic #4."

Similar presentations


Ads by Google