C and - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

C and

Description:

The various levels of abstraction. User view the computer from level 6 ... Once the algorithm is translated into a programming language abstraction: ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 40
Provided by: csetSpU
Category:

less

Transcript and Presenter's Notes

Title: C and


1
C and Object-Oriented Programming Overview
2
Analysis and Design
  • Goals for this Lesson
  • Understand an example of program development
  • Understand the characteristics of a good
    algorithm
  • Understand how algorithmic patterns can help you
    design programs
  • Provide the deliverables for two phases of
    program development Analysis Design
  • Complete exercises for Analysis Design Projects

3
Program Development
  • One program development strategy has these three
    steps
  • Analysis Understand the problem
  • Design Organize the solution
  • Implementation Get the solution running
  • Program development is the progression from
    analysis to design to implementation
  • There are deliverables from each phase

4
Deliverables
  • Analysis deliverable
  • A document that lists the data objects
  • Design deliverable
  • An algorithm that outlines a solution
  • Implementation deliverable
  • An executable program that provides the solution,
    error free

5
Analysis
  • Synonyms
  • inquiry, examination, study
  • Activities
  • Read and understand the problem statement
  • Name the pieces of information (data) necessary
    to solve the problem
  • these data names are part of the solution
  • data names (identifiers) should be standard

6
Self-Help Exercise
  • Example
  • Compute maximum airplane takeoff weight.
  • Name a data object that stores the answer
  • _________________________
  • Name the data object(s) that must be supplied to
    compute that answer
  • _________________________

7
There is more to datathan the names ...
  • The data things are called objects and have these
    three important characteristics
  • a name
  • state a.k.a value(s)
  • a set of operations to manipulate the state
  • Encapsulation
  • conceals the functional details of a class

8
Input and Output
  • It will help in the early programming projects to
    distinguish objects that are either input or
    output
  • Output Information the computer must display
    after the processing has occurred
  • Input Information the user must supply to solve
    the problem.

9
Sample problems help
  • It also helps to provide sample problems
  • given specific input data, determine the output
    (result shown on the screen)
  • Find the length of a line
  • x1 1.0
  • y1 1.0 Input
  • x2 5.0
  • y2 4.0
  • length 5.0 Output

5
3
4
10
Other Sample Problems
11
Summary of Analysis
  • Activities performed during analysis
  • Read and understand the problem
  • Decide what object(s) represent the answerthe
    output
  • Decide what object(s) the user must enter to get
    the answerthe input
  • Create a document that summarizes the analysis.
    This document is input for the next phase of
    program developmentdesign

12
An Example of Analysis
  • Produce the analysis deliverable for the
    following problem
  • Problem Using this course grade scale,
  • Grading Element Percent of Grade
  • Test 1 (0.0 to 100.0) 25
  • Test 2 (0.0 to 100.0) 25
  • Final Exam (0.0 to 100.0) 50
  • compute a course grade as a weighted average for
    any combination of two tests and one final exam.

13
Self-Help Exercise
  • Complete this analysis document and two sample
    problems see slide 1-9 for three examples
  • Problem Object Input or Sample Sample
  • Statement Names Output Problem 1 Problem 2
  • Compute
  • course
  • grade
  • using
  • grading
  • elements

14
Design
  • Synonyms of design (verb)
  • model, think, plan, devise, pattern, propose,
    outline
  • We'll use these design tools
  • algorithms
  • algorithmic patterns
  • algorithm walkthroughs

15
Algorithms
  • An algorithm is a set of activities (usually
    step-by-step) that solves a problem.
  • An algorithm must
  • list activities that are to be performed
  • list these activities in the proper order

16
Bake a Cake
  • A recipe (a.k.a. an algorithm)
  • Preheat Oven
  • Grease Pan
  • Mix ingredients
  • Place ingredients into pan
  • Place pan in oven
  • Remove pan after 35 minutes
  • Switch some activities around
  • What's missing?

17
Algorithmic Patterns
  • Pattern Anything shaped or designed to serve as
    a model or guide in making something else.
  • Algorithmic Pattern A pattern that occurs
    frequently during program development.
  • The Input/Process/Output (IPO) Pattern is used
    during this lesson.

18
IPO Algorithmic Pattern
  • Pattern Input/Process/Output (IPO)
  • Problem The program requires input to
    generate the desired Info.
  • Outline 1. obtain input data from user
  • 2. process input data
  • 3. output the results

19
Patterns ala Alexander
  • "Each pattern describes a problem which occurs
    over and over again in our environment, and then
    describes the core of the solution to that
    problem, in such a way that you can use this
    solution a million times over, without ever doing
    it the same way twice."
  • From A Pattern Language, Christopher Alexander,
    Oxford University Press

20
Example of Algorithm Design
  • The deliverable from this design phase will be an
    algorithm.
  • The IPO patterns provides a guide to design this
    more specific algorithm (that is a bit sketchy)
  • IPO Model One Specific IPO Case Study
  • Input Obtain test1, test2, and finalExam
  • Process Compute the course grade
  • Output Display the course grade

21
Refining steps in algorithms
  • We often need to refine some steps.
  • For example, Compute the course grade might now
    be refined with the C mathematical addition
    and multiplication operators and the names for
    the objects
  • courseGrade test1 0.25
  • test2 0.25
  • finalExam 0.50

22
Algorithm Walkthrough
  • Suggestion Use an algorithm walkthrough to
    review the algorithm
  • Simulate what the computer would do if given the
    instructions.
  • if input occurs, copy values by object names
  • if processing occurs, change an object's value
  • if output occurs, write that output

23
An example walkthrough
  • I) Retrieve some example values from the user and
    store them into the objects as shown
  • test1 ? 80 test2 ? 90 finalExam ? 100
  • P) Use input data and compute courseGrade as
    follows
  • courseGrade (0.25test1)(0.25test2)(0.50final
    Exam)
  • (0.2580.0) (0.2590.0)
    (0.50100.0)
  • (20.0 22.5 50.0)
  • courseGrade 92.5
  • O) Show the course grade value to the user
  • Note Retrieve the data stored in courseGrade to
    show
  • 92.5

24
Implementation
  • Synonyms for Implementation
  • accomplishment, making good, execution
  • Implementation deliverable computer program

25
The Virtual Machine Concept
  • Let's accept the fact that we're not going to
  • translate our algorithms into machine code
  • implement the operating system needed to execute
    programs
  • and so on
  • Instead, let's view the computer as a virtual
    machine
  • an abstract view of the computer

26
Seven Levels of Abstraction
  • Level Virtual Machine (abstraction)
  • 6 Application Software
  • 5 Programming language
  • 4 Assembly language
  • 3 Operating system
  • 2 Machine language
  • 1 Microprogram
  • 0 Digital logic

27
The various levels of abstraction
  • User view the computer from level 6
  • Programmers view the computer from level 5 (this
    is where we are at now)
  • We translate a level into a lower level using the
    tools of that lower level.
  • The tool we'll used is known as C

28
Example Translations
  • Psueduocode algorithm
  • Display the value of courseGrade
  • Our programming language translation
  • cout ltlt "Course grade " ltlt courseGrade
  • Once the algorithm is translated into a
    programming language abstraction
  • use the compiler to generate machine code
  • use the linker to create executable program
  • run the program
  • test the program

29
C Source Code
  • include ltiostream.hgt // for stdcout, stdcin,
    and stdendl
  • int main()
  • // Declare and initialize the numeric objects
    (variables)
  • double test1 0.0
  • double test2 0.0
  • double finalExam 0.0
  • double courseGrade 0.0
  • // I) Obtain input
  • cout ltlt "Test 1 "
  • cin gtgt test1
  • cout ltlt "Test 2 "
  • cin gtgt test2
  • cout ltlt "Final Exam "
  • cin gtgt finalExam
  • // P) Process courseGrade
  • courseGrade (0.25test1) (0.25test2)
    (0.50finalExam)
  • // O) Display the value of courseGrade
  • cout ltlt "Course grade " ltlt courseGrade ltlt endl
  • return 0

Optional Demo grade.cpp
30
Testing
  • Testing occurs at any point in program
    development
  • Analysis example problems
  • Design algorithm walkthroughs
  • Implementation run program with several sets
    of input data
  • A running program isn't always right.
  • but we can gain confidence that it is

31
Overview of the program development process by
levels of abstraction
32
Objects and Classes
  • Objects are entities stored in memory.
  • We understand an objects through its
  • the value(s) it stores
  • the operations that can be applied
  • The case study used four numeric objects (double)
  • values each object of the double class stores
    one floating point number
  • operations operations such as input (cingtgt),
    output cout ltlt, assignment courseGrade ,
    addition , and multiplication

33
Objects
  • Every object has
  • A name
  • State--value(s) stored in memory
  • Operations
  • examples , , -, cin gtgt, cout ltlt, sqrt,
    length, width push_back, pop, capacity, resize

34
Characteristics of Objects
  • Name
  • All four double objects have their own unique
    name.
  • State
  • The state of the double class objects was set
    either through an input operation like this
  • cin gtgt test1
  • or through an assignment operation
  • courseGrade 0.25 test1 0.25 test2
  • 0.50 finalExam

35
Operations applied to objects
  • Operations
  • Addition and multiplication operations are
    applied to some double objects
  • 0.25 test1 0.25 test2 0.50 finalExam
  • There is an input operation applied to the
    keyboard object named cin
  • cin gtgt test1 // This alters test1
  • The state of courseGrade is examined through an
    output operation (cout is the name of the
    screen)
  • cout ltlt courseGrade

36
Classes
  • A class defines the value(s) stored by an object
    and the operations that may be applied to objects
    of that class.
  • A program may utilize many instances of the same
    class.
  • Each instance of class is also know as an object

37
One class--many objects
aNumber
maximum_Takeoff_Weight
x
double
finalExam
test1
y
test2
38
Some Standard C Classes
39
Self-Help Exercise
  • Which class of object and what name would you use
    to represent each of the following?
  • The number of students in a course _____ _____
  • An effective annual percentage rate _____ ______
  • A persons name _______ ________
  • Obtain keyboard input ______ _________
  • An employee (trick quest.) ________ ________
  • All employees in a company _______ _________
Write a Comment
User Comments (0)
About PowerShow.com