CS161 Introduction to Computer Science - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

CS161 Introduction to Computer Science

Description:

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 ... – PowerPoint PPT presentation

Number of Views:283
Avg rating:3.0/5.0
Slides: 18
Provided by: Offic48
Category:

less

Transcript and Presenter's Notes

Title: CS161 Introduction to Computer Science


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

3
To Raise to Power of...
  • In C there is no operator that will raise some
    number by another
  • For example, for x3 you cant type
  • x3 ILLEGAL!
  • Instead, you must use the pow function
  • float answer
  • answer pow(x, 3)

4
To Raise to Power of...
  • To use the power function, we must do two very
    important things...
  • include the math library
  • include ltmath.hgt
  • compile our programs using a -lm flag
  • g -lm prog2.cpp

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

6
Default Precision -- Examples
  • float test
  • cout ltlt Please enter a real number
  • cin gtgt test
  • cout ltlt test

Input Resulting Output
1.23456789 1.23457 10.23456789 10.2346 100.234
56789 100.235 1000.23456789 1000.23 100000.23456
789 100000
7
To Change Precision
  • float test
  • cout ltlt Please enter a real number
  • cout.precision(3) //3 instead of 6!!
  • cin gtgt test cout ltlt test ltlt endl

Input Resulting Output
1.23456789 1.23 10.23456789
10.2 100.23456789 100 10000.23456789 1e04
(Exponential notation)
8
Another way to do this...
  • include ltiomanip.hgt
  • float test
  • cout ltlt Please enter a real number
  • cin gtgt test
  • cout ltltsetprecision(3) ltlt test ltlt 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 ltltsetprecision(3)

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

10
How does width work...
  • float test
  • cout.precision(4) cout.width(10)
  • cin gtgttest cout ltlt test
  • cout ltltendl ltlttest

Input Resulting Output
1.23456789 1.235 1.235

11
Another way to do this...
  • include ltiomanip.hgt
  • float test
  • cout.precision(4)
  • cin gtgttest
  • cout ltltsetw(10) ltlt test
  • cout ltltendl ltlttest

Input Resulting Output
1.23456789 1.235 1.235
12
Trailing Zeros
  • For real numbers, trailing zeros are discarded
    when displayed
  • To display trailing zeros we use
  • cout.setf(iosshowpoint)

Input Resulting Output
1.2300 1.23 (for an precision of 3 or
greater)
13
Displaying Trailing Zeros
  • float test
  • cout.precision(4)
  • cout.setf(iosshowpoint)
  • cin gtgttest cout ltlt test ltltendl
  • cout.unsetf(iosshowpoint) //reset...
  • cout ltlttest

Input Resulting Output
1.2300 1.230 1.23
14
Displaying Dollars and Cents!
  • There is another meaning to precision...
  • if we put in our programs
  • cout.setf(iosfixed,iosfloatfield)
  • then, subsequent precision applies to the number
    of digits after the decimal point!
  • cout.precision(2) cout ltlttest
  • 1.2300 1.23
  • 1.20 1.2
  • To display trailing zeros we use
  • cout.setf(iosshowpoint)


Input Resulting Output
15
Displaying Dollars and Cents!
  • Since we ALSO want trailing zero displayed...do
    all three
  • cout.setf(iosfixed,iosfloatfield)
  • cout.precision(2)
  • cout.setf(iosshowpoint)
  • cout ltlttest
  • 1.2300 1.23
  • 1.20 1.20
  • To display trailing zeros we use
  • cout.setf(iosshowpoint)


Input Resulting Output
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 DONT WANT TO SEE THIS!!!!


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...
Write a Comment
User Comments (0)
About PowerShow.com