Upload
steven-anderson
View
214
Download
0
Embed Size (px)
Citation preview
1
CS161 CS161
Introduction to Introduction to Computer ScienceComputer Science
Topic #4
CS161 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 functionfloat 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 flagg++ -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 -- Examplesfloat test;
cout << “Please enter a real number”;
cin >> test;
cout << test;
Input Resulting Output
1.23456789 1.2345710.23456789 10.2346100.23456789 100.2351000.23456789 1000.23100000.23456789 100000
CS161 Topic #4 7
To Change Precisionfloat test;
cout << “Please enter a real number”;
cout.precision(3); //3 instead of 6!!
cin >> test; cout << test << endl;
Input Resulting Output
1.23456789 1.2310.23456789 10.2100.23456789 10010000.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 difference between
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;
Input Resulting 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;
Input Resulting Output
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);
Input Resulting Output
1.2300 1.23(for an precision of 3 or greater)
CS161 Topic #4 13
Displaying Trailing Zerosfloat test;
cout.precision(4);
cout.setf(ios::showpoint);
cin >>test; cout << test <<endl;
cout.unsetf(ios::showpoint); //reset...
cout <<test;
Input Resulting Output
1.2300 1.2301.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.231.20 1.2
• To display trailing zeros we use:cout.setf(ios::showpoint);
Input Resulting 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.231.20 1.20
• To display trailing zeros we use:cout.setf(ios::showpoint);
Input Resulting 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...