# 17. Write a Program to Calculate Number of Days Between Two Dates. - PowerPoint

Document Sample

```					                                               1

CS 17
• Computer Logic

Chapter 2 Beginning Problem-Solving Concepts
for the Computer

Sonny Huang
2
Chapter 2 Beginning Problem-Solving
Concepts for the Computer

Outline
Constants and Variables
Rules for Naming and Using Variable
Data Types
Numeric Data
Character Data - Alphanumeric Data
Logical Data
Other Data Types
Rules for Data Types
Examples of Data Types
Functions
Operators
Expressions and Equations
3

Objectives

Although problems that are arise in daily life are many
types, problems that can be solved on computers
generally consist of only three:
1. Computational : Mathematical
2. Logical: relational, logical, use in decision making
3. Repetitive:

All programmers need to know those computer
fundamentals in order to write up expressions and
equations to solve problems.

Programmer takes data, the unorganized facts , and
information, the organized facts, relevant them to a
problem and defines them as constants or variables.
4

Objectives

Operators are many signs and symbols that show
relationships between the constant and variables in the
expression and equations that make up the solution.

Programmer has to know all of the operators, how to
use them, as well as, the hierarchy of the operator.

Operators are combined with constants and variables to
create expressions and equations.

Expressions and instructions are used in instructions
that are the building blocks of the solution.
5

Objectives

Functions are set of instructions that are so commonly
used that they are built into a computer language or
application to save programmers’ trouble to rewrite
them.
6

Constant
A constant is a value, that is, a specific alphabetical
and/or numeric value, that never changes during the
processing of all the instructions in a solution.
Constants can be any type of data - numeric,
alphabetical, or special symbols.

In program, a constant is giving a name and location in
memory. During the execution, the constant is given a
value and then is referred to by its name. After the
initialization, the constant can not be changed during
the execution of the program.

Such as: Tax rate, highway speed limits, San Joaquin,
Delta College, PI, total car weight, highest capacity of
people in a restaurant, ..etc.
7

Variable

A variable is giving a name and location in memory.
During the execution, the variable’s value can be
changed

Such as: Salary, student amount each year, student
score, height, total amount of goods stored in the
warehouse, ..etc.
8

Rules of naming variables:

The rules for naming variables differ from language
to language, application to application, and
company to company. However, the general rule is
to name the variable as near to its meaning as
possible.

Some languages and applications have character-
length limitations, reserved words, ..etc, we can
9

Rules of naming variables:

1. Name a variable according to what it represents.

2. Do not use spaces in a variable.

3. Do not use a dash or any other symbol that is
used as mathematical operator in a variable name.

4. After you have introduced a variable name that
represents a specific data item, this exact variable
name must be used in all places where the data item
is used.

5. Be consistent when using uppercase and
lowercase characters. - case sensitive.
10

Name and value of a variable

The name is the label the computer uses to find the correct
memory location.

The value is the content of the location.
11

Data Types

To process solutions, the computer must have data. Data
are unorganized facts. They go into the computer as input
and are processed by the program. What is returned to the
user is output, or information. This information is printed
in the form of report or stored in the form of digital
format.
Input                         Output
Data            Data Processed into                Report
information

Checks                                  Output
Input
Deposits             Calculates the                 Balance
Bk. Chgs                balance                      sheet
12

Data Types

The data the computer uses are of many different types.
The computers must be told the data type of each variable
or constant. The most common data types are numeric,
character, and logical. A few languages and applications
also use the data as a data type. Other languages allow
the programmer to define the data type.
13

Numeric Data

Numeric Data include all types. Numeric is the only
data type that can be used in calculations. The subtypes
of numeric data include integers and real numbers.

Integers are whole numbers. They can be positive and
negative. Programs are using integer when there is no
reason for using partial numbers, as they are using as a
counter.

Real number, or floating pointing numbers, are whole
number plus decimal part. A real number can be
expressed in science notation.
14

Numeric Data

Numeric data are used in business for values, such as
rate of pay, salary, tax, or price, that have calculations
performed on them.

Numbers such as an account number, telephone
number, or zip codes, which would not have
calculations performed on them, would not be
necessarily designed as numeric data.
15

Character Data - Alphanumeric Data

The character data set, alphanumeric data set, consists of all
single digit numbers, letters, and special characters
available to the computer placed within quotation marks.

The ASCII(American Standard Code for Information
Interchange) character set contains 256 characters. The
first 128 comprise a standards set, and the second 128
differ with each computer.

Characters can not be used for calculations even if they
consist of only numbers. When more than one character is
put together, the computer consider this item a string.
16

Character Data - Alphanumeric Data

Character and string data can be compared and arranged in
ascending and descending order, and can be joint together
by concatenation.

If you are using numerical data, there is no way to hold
only with string data.
17

Logical Data

Logical data consist of two pieces of data in the data set -
the words TRUE and FALSE. These are used in making
yes-or-no decisions.
18

Other Data Types

Date data type(Julian Calendar Date) is a number of the
dates from a certain date in the past. The use of this data
type for the date allows the user to subtract one date from
another date to calculate the number of days between
dates. The date is printed in the date format instead of a
number. The date data type is a numeric data type as you
can perform mathematical calculations on any date.

User defined data type: Programmer must specify items in
the data set for each user-defined data type. If a data item
is not contained within the data set, it is not part of the data
type.
19

Rules for Data Types

1. The data that define the value of a variable or a constant
will most commonly be one of three data types: numeric,
character, or logical.

2. The programmer designates the data type during the
programming process. The computer then associates the
variable name within the designated data type.

3. Data types can not be mixed. When the computer expects
a certain data type, the user must use the type or the
computer will return an error message.
20

Rules for Data Types

4. Each of the data types use what is called a data set. The
numeric data uses the set of all base 10 numbers; the
character type uses the set of all characters available to the
computer; the logical data type uses the set of data
consisting of the words TRUE and FALSE. The use of
any data outside the data set results in an error.

5. Any numeric item that must be used in calculations
resulting in a numeric result must be designated as numeric
data type. All other numbers should be designated as
character or character-string data types, even if data are all
numbers.
21

Functions

Functions are small sets of instructions that perform
specific task and return values. They are usually built into
a computer language or application. Functions are used as
parts of instructions in a solution. Because they are basic
tasks that are used repeatedly in the problem-solving
process, by using them a programmer can shorten the
problem-solving time and improve readability of the
solution.

Most programming languages allow programmers to write
their own functions. Libraries of functions can be added to
many languages.
22

Classes of Functions

1. Mathematical functions: Often used in science and

2. String functions: used to manipulate string variables.
Copy, concatenate, length.

3. Conversion functions: are used to convert data from one
data type to another.

4. Statistical functions: are used to calculate maximum and
minimum, etc.

5. Utility functions: access information outside the program
and the language in the computer system.
23

Form of a Function

The form of a function is the name of the function
followed by an open parenthesis, followed by the data
needed to perform the function and concluded by a closed
parenthesis:

functionname(data)

The value of the result of the function is returned in the
name of the function.

Functions use data. These data are listed as part of
function are called parameters. Functions normally do not
alter the parameters.
24

Form of a Function

A parameter can be a constant, a variable, or an
expression. An expression is a calculation which has not
been given a permanent memory location in the computer.

The name of the function may vary from language to
language and from application to application.
25

Table 2.4 Functions
26
Table 2.4 Functions, continued
27

Operators

The computer has to be told how to process data. This
task is accomplished through the use of operator.
Operators are the data connectors within expressions and
equations. They tell the computer what type of
processing(mathematical, logical, ..,etc.) needs to be
done. The types operators used in calculations and
problem solving include mathematical, relational, and
logical operators. Without these operators very little
processing can be done.

Operands are the data that the operator connect and
processes.
28

Operators

The resultant is the answer that results when the
operation is completed.

multiplication, division, integer division, and power.

Relational operators include equal to, less than, greater
than less then or equal to, greater than or equal to, and
not equal to. A programmer uses relational operator to
program decisions. The operands of a relational operator
can be either numeric or character(a string); however,
both operands must be of the same data type. The
resultant of a relational operator is logical data type true
or false.
29

Operators

The programmer designs one action or set of actions that
will follow when a relation is TRUE, an another action
or set of actions that will follow when the expression is
FALSE.

The use of relational operators is the only way for the
computer to make decisions.

It can also be used for controlling repetitive instructions
called loops.

Logical operators are used to connect relational
expressions(decision-making expressions) and to
perform operations on logical data.
30
Table 2.5 Operators and Their Computer Symbols
31
Table 2.6 Definitions of the Logical Operators
32

Operators

These mathematical, relational, and logical operators
have a hierarchy, or precedence, an order in which their
operations take place. Programmers can use parentheses
to reorder the normal processing sequence.

1+5*5/6-1*3
1+5*5/(6-1)*3

Each type of operator requires a certain data type for
operands and determines the data type of the resultant.
33
Table 2.7 Hierarchy of Operations
34

Expressions and Equations

Expressions and equations make up part of the
instructions in the solution to a computer problem. An
expression process data, the operands, through the use of
operators.

Length * Width

An equation stores the resultant of an expression in a
memory location in the computer through the equal(=)
sign.

Area= Length * Width

The resultant of the expression Length * Width would
then be assigned to a memory location called Area.
35

Expressions and Equations

Equations are often called assignment statements because
the variable on the left hand side of the equal sign is
assigned the value of the expression on the right hand
side.

The equal sign does not mean equals; instead, it means
replaced by or is assigned the value of.

N=N+1
36
Writing evaluation expressions and
equations
Example 1 Setting up a Numeric Expression
4Y
X(3Y+4) -
X+6
1. Write all parentheses, operands, and operators on a
single line with the dividend.
X(3Y+4)-4Y/X+6

2. The computer does not use any implied operators as
used in mathematics. Insert all implied operators.
X*(3*Y+4)-4*Y/X+6

3. Insert all parentheses where the hierarchy need to be
reordered.
X*(3*Y+4)-4*Y/(X+6)
37
Writing evaluation expressions and
equations
Example 2 Setting Up a Mathematical Equation

Y + 3 = X(Z+5)

1. Write all parentheses, operands, and operators on a
single line with the dividend.
Y + 3 = X(Z+5)

2. Use mathematical rules to complete the equation so
that there is one variable on the left side of the equation
sign.
Y + 3 -3= X(Z+5) -3
Y = X(Z+5)-3
38
Writing evaluation expressions and
equations

3. Follow the steps in Example 1 to complete the
equation.
Y = X * (Z+5)-3
39
Writing evaluation expressions and
equations

Example 3 Setting Up a Relational expression

X is less than Y + 5

X< Y+5
40
Writing evaluation expressions and
equations

Example 4 Setting Up a Logical expression

The customer can cash a check(True or False)
41
Writing evaluation expressions and
equations

Example 5 Evaluating a Mathematical Expression
It is important for a programmer to test all the equations
and expressions are correct.

5*(X+Y) - 4 * Y/(Z+6)             Y=5 Y=3        Z=6
Operation                         Resultant
1 X+Y                                            5
2 Z+6                                           12
3 5 * resultant of 1                            25
4 4*Y                                           12
5 Resultant of 4 / Resultant of 2                1
6 Resultant of 3 - Resultant of 5               24
42
Writing evaluation expressions and
equations

Example 6 Evaluating a Relational Expression

A-2> B

A=6 B=8

Operation          Res ultant
1 A -2                        4
2 Res ultant of 1 > B FALSE
43
Writing evaluation expressions and
equations

Example 7 Evaluating a Logical Expression

A AND B OR C AND A
A = TRUE B = FALSE C = TRUE

Operation                       Resultant
1 A AND B                          FALSE
2 C AND A                          TRUE
3 Resultant of 1 OR resultant of 2 TRUE
44
Writing evaluation expressions and
equations

Example 8 Evaluating an Equation That uses Both
Relational and Logical Operators

F = NOT (A < B) AND ( C OR D)
A = 4 B = 2 C = TRUE     D = FALSE

Operation                                              Resultant
1 A<B                                                      FALSE
2 C OR D                                                   TRUE
3 NOT the Resultant of 1                                   TRUE
4 Resultant of 3 AND resultant of 2                        TRUE
5 Store the resultant of 4 in the memory location called F
45
Writing evaluation expressions and
equations
Example 9 Developing a Table of All Possible
Resultants of a Logical Expression
A        B
A
T        T
T
T        F
F                         F        T
F        F

A          B         C
T          T         T           A       B     NOT A OR B
T          T         F           T       T         T
T          F         T           T       F         F
T          F         F
F          T         T
F       T         T
F          T         F           F       F         T
F          F         T
46
Writing evaluation expressions and
equations

Example 10 Developing a Logical Expression from a
Given Problem.

Many times a programmer will be given the policies of a
company and then be required to set up a logical
expression from those policies.

Problem: A large department store has its own charge
card. The policy for a customer to charge an item is that
the customer must have a valid charge card and either a
balance of less than \$500 or a charge of less than \$50.
47
Writing evaluation expressions and
equations

1. List the items the charge is dependent on and their data
types:
a. The charge card(logical data type)
b. The balance (numeric data type)
c. The charge amount(numeric data type)

2. Write down these items as variables along with
condition on each.
charge_card       balance < 500          amount < 50

3. Put in the logical operators and Parentheses.
charge_card AND (balance < 500 OR amount < 50)
48
Writing evaluation expressions and
equations

4.   The expression can be used in various ways:

a. assignment statement:
OKToChg
= charge_card AND (balance < 500 OR amount < 50)

b. decision statement:
IF charge_card AND (balance < 500 OR amount < 50)
THEN PRINT “OKAY TO CHARGE”

ELSE PRINT “NOT OKAY TO CHARGE”
49
Writing evaluation expressions and
equations
Example 1 Setting up a Numeric Expression
4Y
X(3Y+4) -
X+6
1. Write all parentheses, operands, and operators on a
single line with the dividend.
X(3Y+4)-4Y/X+6

2. The computer does not use any implied operators as
used in mathematics. Insert all implied operators.
X*(3*Y+4)-4*Y/X+6

3. Insert all parentheses where the hierarchy need to be
reordered.
X*(3*Y+4)-4*Y/(X+6)
50

Problem 5 c Solution

•   11   2 . Sit down

•   12   15. Stop

```
DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 396 posted: 12/1/2010 language: English pages: 50
Description: 17. Write a Program to Calculate Number of Days Between Two Dates. document sample
How are you planning on using Docstoc?