INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING - PowerPoint PPT Presentation

1 / 65
About This Presentation
Title:

INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING

Description:

AN ALGORITHM IS A STEP BY STEP PROCEDURE FOR SOLVING A PROBLEM. ... The world's largest ball of twine is located in Cawker City, Kansas. ... – PowerPoint PPT presentation

Number of Views:269
Avg rating:3.0/5.0
Slides: 66
Provided by: cisd7
Category:

less

Transcript and Presenter's Notes

Title: INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING


1
  • INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING

2
  • WHAT IS A PROBLEM?
  • AN UNRESOLVED MATTER REQUIRING ATTENTION.
  • A SOLUTION?
  • A PROCESS CONTAINING THE ANSWER(S)
  • TO SOLVING A PROBLEM.

3
PROBLEM SOLVING AND PROGRAM DEVELOPMENT
  • DEFINITION
  • ANALYSIS AND SPECIFICATION
  • DESIGN
  • IMPLEMENTATION
  • TESTING

4
  • PROBLEM SOLVING AND
  • PROGRAM DEVELOPMENT CYCLE

DEFINITION OF THE PROBLEM ANALYSIS AND
SPECIFICATION OF THE PROBLEM DESIGN THE
SOLUTION TEST THE SOLUTION IMPLEMENT THE
SOLUTION (CODING) DEBUG THE CODE TEST THE
CODE PRODUCTION
5
  • ALGORITHMS
  • AN ALGORITHM IS A STEP BY STEP PROCEDURE FOR
    SOLVING A PROBLEM.
  • AN ALGORITHM DOES NOT HAVE TO FOLLOW
  • ANY GRAMMATICAL RULES, IT REPRESENTS
  • A HIGH LEVEL SOLUTION TO A PROBLEM.

6
ATTRIBUTES OF AN ALGORITHM
  • CORRECTNESS
  • COMPLETENESS
  • EFFICIENCY

7
PROBLEM FIND THE AVERAGE OF 3 TEST SCORES
THE ALGORITHM
  • 1 GET test1, test2, test3
  • 2 CALCULATE average
  • 3 DISPLAY average

8
PROBLEMThe worlds largest ball of twine is
located in Cawker City, Kansas.
  • How much does the ball weigh?
  • How many miles would the twine reach if it were
    unrolled?
  • PROBLEM
  • Given the radius of a spherical ball of twine,
    compute the weight of the ball.
  • A program must be designed to answer this
    problem.
  • The programs behavior
  • What are the objects
  • Operations
  • Algorithm

9
Algorithm
  • Declare constant PI.
  • Output a prompt for the radius of a spherical
    ball to cout.
  • Input a real value from cin and store it in
    radius.
  • Output a prompt for the density of a spherical
    ball to cout.
  • Input a real value from cin and store it in
    density.
  • Compute weight density 4.0 PI pow(radius,
    3)/3.0.
  • Output weight to cout.
  • This sequence of instructions is called a
    pseudocode algorithm
  • This algorithm is a blue print for our problem
    and can be translated into a program

10
  • PROGRAMMING
  • THE PROCESS OF DEFINING THE SET OF INSTRUCTIONS
    THAT MAKE UP A PROGRAM.
  • PROGRAMMING LANGUAGES PROVIDE A SET OF RULES,
    SYMBOLS, AND SPECIAL WORDS TO CONSTRUCT A PROGRAM.

11
LANGUAGE ENVIRONMENTS
  • HIGH - LEVEL LANGUAGES
  • LOW - LEVEL LANGUAGES
  • MACHINE LANGUAGE

12
  • THE C LANGUAGE
  • EVOLVED FROM THE C LANGUAGE IN THE EARLY 1980s AT
    BELL LABORATORIES.
  • C PROVIDES CAPABILITIES FOR OBJECT ORIENTED
    PROGRAMMING.

13
  • DATA
  • DATA ARE A COLLECTION OF RELATED OBJECTS TO BE
    ANALYZED AND PROCESSED BY A PROGRAM.
  • CAN BE NUMERIC, ALPHABETIC OR A COMBINATION OF
    BOTH (ALPHANUMERIC). ALSO IT CAN EXPRESS A
    LOGICAL VALUE (TRUE OR FALSE).

14
  • INFORMATION
  • DATA THAT HAVE BEEN PROCESSED AND
  • ARE MEANINGFUL FACTS, CONCEPTS,
  • AND KNOWLEDGE.

15
  • CHARACTER SETS
  • USED FOR THE INTERNAL
  • REPRESENTATION OF DATA.
  • ASCII (AMERICAN STANDARD CODE FOR INFORMATION
    INTERCHANGE)
  • EBCDIC (EXTENDED BINARY CODED DECIMAL
    INTERCHANGE CODE)

16
BASIC OBJECT TYPES (IN C)
  • 1. int
  • 2. float
  • 3. char

17
int
0 -2002 15 0000000000000
015 15
18
  • float
  • See your text for discussion of
  • Float, double and long double on page 62.
  • 23.4
  • 3.456
  • -7.890
  • DIGITS OF ACCURACY (PRECISION)
  • AND MAGNITUDE
  • EXAMPLE
  • 1,234,567,890
  • CAN BE REPRESENTED AS
  • 1234568x10
  • (WITH 7 DIGITS OF ACCURACY.)

3
19
  • CONVERSION RULE
  • 1. DECIDE ON ACCURACY OF DIGITS (WITH LAST
    ROUNDED).
  • 2. INCREMENT BY 1 (TO THE POWER OF 10 AS THE
    MULTIPLIER) TO MOVE THE DECIMAL TO THE LEFT.
  • 1 2 3 4 5 6 7 8 9 0 x 10

0
ACCURACY
MAGNITUDE
ROUNDED
TO BE IGNORED
20
  • FINAL REPRESENTATION
  • IN PRACTICE, THIS IS CARRIED ONE STEP FORWARD.
    THE DECIMAL POINT IS PLACED BEFORE THE FIRST
    SIGNIFICANT DIGIT.
  • 0 . 1 2 3 4 5 6 8 x 10

10
ACCURACY
MAGNITUDE
FLOATING POINT NUMBER
21
  • char
  • ALPHANUMERIC CHARACTERS INCLUDE
  • LETTERS, DIGITS AND SPECIAL SYMBOLS.
  • ( 'a' 'A' '0' '1' '2' '' '-' '' ' ' )
    ETC.
  • EACH CHARACTER IS ENCLOSED IN SINGLE QUOTES.

22
OTHER OBJECT TYPES
  • ENUMERATED
  • COMPOSITE
  • DYNAMIC
  • CLASS

23
ENUMERATED
  • enum SU, MO, TU, WE, TH, FR, SA
  • enum Boolean FALSE, TRUE
  • enum plus , minus -
  • enum winter 1, spring, summer, fall
  • enum on, off switch

24
Composite,Dynamic,Class
  • Composite composed of many data types
  • Dynamic dynamic vs static type objects. Static
    type is determined during compilation time and
    not changed during execution time. Dynamic type
    of an object is determined during the execution
    time not the compilation time.
  • Class Description of a group of objects with the
    same properties. A kind of type.

25
  • SYNTAX / SEMANTICS
  • SYNTAX RULES TO WRITE VALID INSTRUCTIONS.
  • SEMANTICS THE MEANING OF INSTRUCTIONS.

26
  • IDENTIFIERS
  • ARE USED TO NAME THINGS IN C
  • (VARIABLE NAMES, FUNCTIONS, ETC.).
  • SOME ARE DEFINED IN THE LANGUAGE AND
  • ARE RESERVED FOR SPECIFIC USE
  • (RESERVED WORDS).

27
  • VARIABLE DATA OBJECTS
  • VALUES THAT ARE IDENTIFIED BY SYMBOLIC NAMES.
  • MAY CHANGE FROM TIME TO TIME DURING PROGRAM
    EXECUTION.
  • EACH VARIABLE NAME IDENTIFIES A STORAGE LOCATION
    IN THE COMPUTER'S MEMORY.
  • CAN BE ANY OF THE DEFINED OBJECT TYPES.

28
  • NAMING RULES
  • 1. FIRST CHARACTER MUST BE A LETTER.
  • 2. LENGTH OF THE NAME CAN VARY, CHARACTERS 2 TO
    N CAN BE A LETTER, A DIGIT, OR AN UNDERSCORE.
  • 3. A C RESERVED WORD CAN NOT BE USED AS AN
    IDENTIFIER.

29
  • VALID VARIABLE OBJECT NAMES
  • total
  • Score1
  • X
  • ScoreOne
  • score_one

30
NOT VALID VARIABLE OBJECT NAMES Score One 1score
31
VARIABLE DECLARATIONS
A STATEMENT THAT ASSOCIATES AN IDENTIFIER WITH
AN OBJECT SO THAT IT MAY REFER TO THAT OBJECT
BY NAME.
type variable_name_list
  • .
  • .
  • float total, average
  • int score1, score2
  • char letter_grade
  • .
  • .

32
  • CONSTANT DATA OBJECT
  • QUANTITIES WHICH VALUES DO NOT CHANGE
  • DURING PROGRAM EXECUTION.
  • CAN BE NUMERIC OR CHARACTER TYPE.

33
  • CONSTANT VALUES
  • ALL NUMBERS, INTEGER AND REAL ARE CONSTANTS. SO
    ARE SINGLE CHARACTERS AND SERIES OF CHARACTERS
    (STRINGS).

34
NAMED CONSTANT A SPECIFIED VALUE ASSOCIATED
WITH A CONSTANT NAME (IDENTIFIER). THIS VALUE
CANNOT BE CHANGED DURING PROGRAM EXECUTION.
35
CONSTANT DEFINITIONS
  • .
  • .
  • const char BLANK ' '
  • const float TEST_WEIGHT 0.25
  • const float ASSIGNMENT_WEIGHT 0.10
  • const int MAX_STUDENTS 100
  • const char MESSAGE
  • "Invalid data - reenter"
  • const char FLAG "true"
  • .
  • .

const type constant_name expression
36
  • EXPRESSIONS
  • ARE MADE UP OF VARIABLES, CONSTANTS AND
    OPERATORS.
  • operand
  • OR
  • operand operator operand

37
  • THE FOLLOWING ARE VALID EXPRESSIONS
  • weight ( a sphere) density volume
  • volume 4?r3 / 3
  • weight density 4?r3 / 3
  • (num 2) / 3.56
  • TAX_RATE
  • test
  • income TAX_RATE

38
  • ARITHMETIC EXPRESSION OPERATORS
  • ADDITION
  • - SUBTRACTION
  • MULTIPLICATION
  • / DIVISION
  • DIVISION (MODULUS)
  • (REMAINDER FROM INTEGER DIVISION)

39
EXAMPLES
  • C
  • sum 5.6
  • sum 5.6
  • sum / value

MATH SUM 5.6 SUM x 5.6 SUM VALUE
40
EXAMPLES
VALUE 29 15 120 4.8 1 0 0 7 1 0
  • EXPRESSION
  • 13 16
  • 27 - 12
  • 8 15
  • 24.0 / 5.0
  • 9 / 8
  • 7 / 8
  • 9 9
  • 7 8
  • 9 8
  • 0 6

41
  • PRECEDENCE
  • LOWEST (LEFT TO RIGHT)
  • -
  • HIGHEST (LEFT TO RIGHT)
  • /

42
EXAMPLES
10 / 2 4 5
20
  • 5.0 3.0 / 10.0 3.0 15.0
  • 1.5 4.5

43
  • WHEN TWO CONSTANT OR VARIABLE OBJECTS OF THE
  • SAME TYPE ARE COMBINED USING ONE OF THE
  • BASIC ARITHMETIC OPERATORS, THE RESULT IS
  • THE SAME TYPE AS THE OPERAND.
  • 5 6 11
  • 5.0 6.0 11.0

44
  • INTEGER DIVISION YIELDS INTEGER RESULTS
  • 8 / 5 1
  • 9 / 5 1
  • 10 / 5 2
  • REAL DIVISION YIELDS REAL RESULTS
  • 8.0 / 5.0 1.6
  • 9.0 / 5.0 1.8
  • 10.0 / 5.0 2.0

45
  • MODULUS OPERATION YIELDS INTEGER RESULTS
  • 8 5 3
  • 9 5 4
  • 10 5 0

46
  • PARENTHESES
  • CAN BE USED TO MODIFY THE ORDER OF EVALUATION.
  • SUBEXPRESSIONS ARE EVALUATED, THE RESULTS ARE
    COMBINED TO EVALUATE THE COMPLETE EXPRESSION.
  • THE COMPUTATION OF THE INNERMOST PARENTHESES ARE
    PERFORMED FIRST.

47
5.0 2 / ( 4 2 ) 8 10.0
1.25
  • 5 2 / ( 4 2.0 )
  • 8.0
  • 0.25
  • 5.25

48
  • ( 5 3 ( 2 4 / 2 ) 7 / 2 ) / 3

2
4
12
3
17
20
6
49
  • ( 5 ( 11 - 5 ) 2 ) 4 9
  • ( 5 6 2 ) 4 9
  • 60 4 9
  • 240 9
  • 249

50
  • THE UNARY OPERATORS
  • X
  • - ( A B )
  • N ( - 2 )
  • UNARY OPERATIONS HAVE THE HIGHEST PRIORITY.

51
cin gtgt input_list(gtgt IS THE INPUT OPERATOR)
INPUT OPERATIONS
DATA CAN BE STORED IN MEMORY IN TWO WAYS THE
INPUT STATEMENT OR THE ASSIGNMENT STATEMENT.
  • cin gtgt score1 gtgt score2

52
cout ltlt output_list(ltlt IS THE OUTPUT OPERATOR)
OUTPUT OPERATIONS
THE RESULTS OF PROGRAM EXECUTION CAN BE DISPLAYED
USING THE OUTPUT STATEMENT.
  • cout ltlt "The total score is "
  • cout ltlt total
  • OR
  • cout ltlt "The total score is "
  • ltlt total

53
TO ADVANCE THE PRINTING MARKER TO THE START OF
THE NEXT LINE, ltlt endl SHOULD BE INCLUDED AT THE
END OF A cout OPERATION.
  • cout ltlt "The total score is "
  • ltlt total ltlt endl
  • cout ltlt "The letter grade is "
  • ltlt letter_grade ltlt endl

54
The total score is 185.000 The letter grade is
A
DISPLAYS
55
THE ASSIGNMENT STATEMENT
variable_name expression
  • THE "" IS THE ASSIGNMENT OPERATOR.
  • WHEN THE STATEMENT IS BEING EXECUTED, THE
    FOLLOWING OCCURS
  • 1. EVALUATE THE EXPRESSION.
  • 2. PLACE THE VALUE ONTO THE VARIABLE'S STORAGE
    LOCATION.

56
ANOTHER EXAMPLE ASSUME THE FOLLOWING
DECLARATIONS.
  • float total, average
  • int score1, score2, counter
  • .
  • .

total average score1 score2 counter

? ?

? ? ?
total, average, score1, score2 AND counter ARE
UNDEFINED.
57
AND THE CODE SEGMENT
  • .
  • .
  • cin gtgt score1 gtgt score2
  • counter 0
  • .
  • .

total average score1 score2 counter

? ?

87 98 0
INITIAL VALUES
total AND average ARE STILL UNDEFINED.
58
. . total score1 score2 counter counter
1 . .
87 98
total average score1 score2 counter

185.0 ?

87 98 1
0 1
average IS STILL UNDEFINED.
59
  • THE ASSIGNMENT STATEMENT DOES NOT IMPLY EQUALITY.
  • sum num
  • AND
  • num sum
  • ARE NOT THE SAME.

60
sum num
sum 456 num 456
sum 123 num 456
num sum
sum 123 num 456
sum 123 num 123
61
USING COMMENTS
// comment OR / comment /
  • // This is a one-line comment
  • / This is a comment too,
  • but a long one /

62
COMPILER DIRECTIVES
A COMPILER DIRECTIVE IS REPLACED DURING
TRANSLATION BY THE NAMED C LIBRARY HEADER FILE.
include ltfilenamegt
include ltiostream.hgt
63
PROGRAM CONSTRUCTION
  • PROGRAMS BEGIN WITH OPTIONAL COMMENTS TO IDENTIFY
    THE NAME OF THE FILE IN WHICH THE PROGRAM IS
    STORED AND A BRIEF DESCRIPTION.
  • THE COMPILER DIRECTIVES FOLLOW THE COMMENTS.
  • THE PROGRAM STARTS WITH THE IDENTIFIER main
    FOLLOWED BY AN OPTIONAL LIST OF PROGRAM'S
    ARGUMENTS IN THE PARENTHESES ( ) AND THE
    PROGRAM'S BODY INCLUDED WITHIN THE BRACKETS .

64
  • // FILE FindAvg1.cpp
  • // Finds the average of three test scores
  • include ltiostream.hgt
  • void main ()
  • // local data
  • const int NUMBER_OF_TESTS 3
  • float test1, test2, test3, average
  • // Get the grade marks
  • cout ltlt "Enter the three test marks "
  • cin gtgt test1 gtgt test2 gtgt test3
  • average (test1 test2 test3) /
  • NUMBER_OF_TESTS
  • // Display the average
  • cout ltlt "The average of the 3 test marks is "
  • ltlt average ltlt endl
  • return

65
PROGRAM INTERACTION
Enter the three test marks 70.0 80.0 90.0 The
average of the 3 test marks is 80.000
Write a Comment
User Comments (0)
About PowerShow.com