INFSCI 0015 Data Structures Lecture 3: Simple Programs - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

INFSCI 0015 Data Structures Lecture 3: Simple Programs

Description:

An exchange kiosk (P.I. Airport) German marks (DM) US dollars (USD) Required data: ... What we can learn: Style. Make program more readable. Indentation (use ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 19
Provided by: peterbru
Category:

less

Transcript and Presenter's Notes

Title: INFSCI 0015 Data Structures Lecture 3: Simple Programs


1
INFSCI 0015 - Data StructuresLecture 3 Simple
Programs
  • Peter Brusilovsky
  • http//www2.sis.pitt.edu/peterb/0015-011/

2
Problem Exchange Kiosk
  • An exchange kiosk (P.I. Airport)
  • German marks (DM) ? US dollars (USD)
  • Required data
  • Exchange rate
  • How many DM
  • Comission
  • USD DMExchangeRate - Comission

3
Example 3_1 Exchange Kiosk
  • / Exchange kiosk /
  • include ltstdio.hgt
  • main()
  • float dollars_for_mark / exchange rate /
  • int commission / commission in dollars /
  • float marks / how many marks /, dollars
  • dollars_for_mark 0.666
  • commission 3
  • marks 100
  • dollars marks dollars_for_mark - commission
  • printf("For 6.2f marks you will get 6.2f
    dollars!\n" , marks, dollars)

4
What we can learn Style
  • Make program more readable
  • Indentation (use Tabs!)
  • Empty lines
  • Blanks inside expressions
  • Make programs more understandable
  • Comments
  • Meaningful names for variables

5
Bad Code for Exchange Kiosk
  • include ltstdio.hgt
  • main()
  • float n int k float m,
  • r
  • n0.666
  • k3m100rmn-k
  • printf("For 6.2f marks you will get 6.2f
    dollars!\n",m,r)

6
What we can learn Process
  • Programming write code line by line?
  • Programming is problem solving
  • Understand problem
  • Design solution
  • Implement solution (yes, coding!)
  • Test / debug solution
  • And often go over

7
Specifying the problem
  • The first step in solving any problem is
    understanding the problem. We call this
    specifying the problem.
  • You can think of a programming task as a word
    problem.
  • What information is given? This is the starting
    point.
  • What is the desired result?
  • How do you get there?

8
Designing the solution
  • Designing the solution
  • What information is needed?
  • What steps need to be performed?
  • First design the solution - then, implement the
    program. Remember, we should be able to design
    the solution without regard to the actual
    programming language.

9
Design for Exchange Kiosk
  • Required data
  • exchange rate, commission, how many DM
  • Expected result
  • how many USD
  • Design
  • Get data
  • Calculate USD
  • Print result

10
Example 3_1 Exchange Kiosk
  • / Example 3.1 Exchange kiosk
  • Course IS 0015
  • Author Peter Brusilovsky
  • This program calculates the amount of dollars
  • received in an exchange kiosk for the given
  • amount of German marks /
  • include ltstdio.hgt
  • main()
  • float dollars_for_mark / exchange rate /
  • int commission / commission in dollars /
  • float marks / marks given /
  • float dollars / dollars returned /
  • / get data /
  • / calculate USD /
  • / print result /

11
Example 3_1 Exchange Kiosk
  • main()
  • float dollars_for_mark / exchange rate /
  • int commission / commission in dollars /
  • float marks / marks given /
  • float dollars / dollars returned /
  • / get data /
  • dollars_for_mark 0.666
  • commission 3
  • marks 100
  • / calculate USD /
  • dollars marks dollars_for_mark - commission
  • / print result /
  • printf("For 6.2f marks you will get 6.2f
    dollars!\n" , marks, dollars)

12
Ex 3_2 Better Exchange Kiosk
  • / Example 3.2 Better exchange kiosk /
  • include ltstdio.hgt
  • main()
  • float dollars_for_mark 0.55 / exchange rate
    /
  • int commission 3 / commission in dollars /
  • int marks float dollars
  • / get data /
  • printf("Marks to exchange? ")
  • scanf("d",marks)
  • / calculate USD /
  • dollars marks dollars_for_mark - commission
  • / print results /
  • printf("For d marks you will get 6.2f
    dollars!\n" , marks, dollars)

13
Getting data inside
  • Assignment
  • Initialization of variables
  • int commission 3
  • Input data
  • scanf("d",marks)
  • The computer will read the symbols from standard
    input, consider it as an integer (d tells about
    it) and assign to the given variable marks.
  • Consider as a magic sign for now

14
Example 3_3 Capital growth
  • include ltstdio.hgt
  • main()
  • float interest_rate / interest rate in
    percents /
  • int capital / starting capital in dollars /
  • float annual_interest / annual interest in
    dollars /
  • printf("Capital () and interest rate (xx.x)
    ")
  • scanf("d f",capital, interest_rate)
  • annual_interest capital interest_rate / 100
  • printf("Interest 6.2f Total 9.2f\n",
    annual_interest, capital annual_interest)

15
More about input
  • Input specification is important!
  • scanf("f",dollars_for_mark)
  • scanf("d",marks)
  • The computer will read the symbols from standard
    input, consider it according the given input
    specification (i.e., f or d) and assign to the
    given variable
  • Several values could be entered
  • scanf("d f",capital, interest_rate)

16
Type conversion Assignment
50.8
?
?
?
  • The value to the right of the assignment
    operation is converted to the type of the
    variable to the left
  • Possible loss of information - beware!

17
More numeric types
  • Integral types
  • int
  • short (usually 2 bytes)
  • long (usually 4 bytes)
  • Floating point types
  • float
  • double

18
Conversions
  • int i long l short s float f double d
  • Safe conversion (to broader type)
  • l s l i i s
  • d f d i f i / converting int to float,
    like 100 to 100.0 /
  • Unsafe conversion (loosing information)
  • i f / truncation, like 99.9 to 99 /
  • s l / dropping bits /
  • f d / rounding/truncation /
Write a Comment
User Comments (0)
About PowerShow.com