0% found this document useful (0 votes)
13 views

LN 2

(1) There are three types of problems that can be solved on computers: computational problems involving mathematical processing, logical problems involving relational or logical processing, and repetitive problems involving repeating instructions. (2) Computers use constants and variables to solve problems. Constants never change value during program execution while variables may change. Variables are given names and assigned memory locations to store their values. (3) Proper naming conventions for variables increase readability and maintainability of code. Variable names should be short, clear, consistent, and avoid special characters.

Uploaded by

matarabdullah61
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

LN 2

(1) There are three types of problems that can be solved on computers: computational problems involving mathematical processing, logical problems involving relational or logical processing, and repetitive problems involving repeating instructions. (2) Computers use constants and variables to solve problems. Constants never change value during program execution while variables may change. Variables are given names and assigned memory locations to store their values. (3) Proper naming conventions for variables increase readability and maintainability of code. Variable names should be short, clear, consistent, and avoid special characters.

Uploaded by

matarabdullah61
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

LECTURE NOTES 2

PROBLEM SOLVING CONCEPTS FOR THE COMPUTER

Problems that can be solved on computers generally consist of only


three:
(1) computational, problems involving some kind of mathematical
processing;
(2) logical, problems involving relational or logical processing, the kinds
of processing used in decision making on the
computer; and
(3) repetitive, problems involving repeating a set of mathematical
and/or logical instructions.
• The computer uses constants and variables to solve problems.
A constant
• never changes during the processing of all the instructions in a solution.
• Constants can be
any type of data—numeric, alphabetical, or special symbols.
• The constant is given a location in memory and a name
• Once the constant is given a value, it cannot be changed during the
execution of the program.

A variable
• may change during processing.
• A programmer must give a name to each variable value used in a solution.
• The computer sets up a specific memory location to hold the value of each
variable name found in a program.
• Variables can be any data type.
Naming Convention
• Firstly, it allows several programmers to work on the same project without
the problem of conflicting variable names.

• Secondly, it allows programs to be easily read because there is only one


consistent name for a variable. It also increases the readability between
applications because the form of the variable name is consistent within a
company.
• Thirdly, naming conventions allow the code to be easily maintained.
Programmers spend most of their time on software maintenance, not
development. Software is maintained by many different people
• And last, naming conventions should produce a clean, well-written
program.
Rules for Naming and Using Variables
• Name a variable according to what it represents, that is, Hours for hours
worked, PayRate for rate of pay, and so on.
• Create short name that clearly represents the variable.
• Do not use spaces in a variable name; for example, use HoursWorked.
• Start a variable name with a letter.
• Do not use a dash (or any other symbol that is used as a mathematical
operator) in a variable name.
• If the data item hours worked has the variable name of Hours, Hours must
be used consistently. You should not use Hrs or HoursWorked to represent
the same data item.
• Be consistent when using upper- and lower-case characters. In some
languages HOURS is a different variable name than Hours.
DATA TYPES
Numeric Data
• Numeric data include all types of numbers Numeric is the only data
type that can be used in numeric calculations. The subtypes of
numeric data include integers and real numbers. Integers are whole
numbers, such as 5,297 or -376.
• Real numbers, or floating point numbers, are whole numbers plus
decimal parts.
• Numeric data are used for values, such as rate of pay, salary, angles,
distance, or
radius, that have calculations performed on them.
• Numbers such as an account number or a zip code, which would not
have calculations performed on them, would not be designated by
the programmer as numeric data.
• The character data set, sometimes called alphanumeric data set,
consists of all single digit numbers, letters, and special characters
available to the computer—a, A, Z, 3, #, &, and so forth—placed
within quotation marks. An upper case letter is considered a different
character from a lower case letter.
• The ASCII (American Standard Code for Information Interchange)
character set contains 256 characters.
• When more than one character are put together, the computer
considers this item a string.
• Most data in business—names, account numbers, addresses, cities,
states, telephone numbers, zip codes—are string data. As a rule,
items that would not have mathematical computations performed on
them should be designated string data types.
Logical Data

• Logical data consist of two values in the data set—the words True and
False.
• These are used in making yes-or-no decisions.
• For example, logical data type might be used to check someone’s
credit record; True would mean her credit is okay, and
False would mean it’s not okay.
HOW THE COMPUTER STORES THE DATA

• The computer stores data internally in memory locations.


• Each variable name is given a memory location, and each memory location can
hold one and only one value at a time.
• When a user enters a new value into the variable location, the previous value is
destroyed.
• These memory locations are temporary, as the internal memory is a volatile
memory. When a program completes its instructions, and/or when the computer
is turned off, the values stored in the internal memory are destroyed.
• When data, information, or programs have to be kept for future use, they are
stored externally on an external storage medium such as a hard disk drive
in storage areas called files. There are basically two types of files: program files
and
data files.
• Program files contain the instructions to tell the computer what to do.
• Data files contain the data required to execute the
program files.
Functions are small sets of instructions that perform specific tasks and
return values.
• They are usually built into a computer language.
• Most languages allow programmers to write their own functions.
• Libraries of functions can be added to many
languages.
• 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 function is returned.
• Functions use data. The data is listed as part of the function and are
called parameters.
• Sqrt(N): This function will calculate the square root of N.
• Sqrt is the name of the function.
• N is the data needed to calculate the square root and, therefore, it is the
parameter.
• Parameters are surrounded by parentheses.
• The maximum function, Max(N1, N2,N3), will find which of three
numbers is the largest. Max is the name of the function. N1, N2 and
N3 are parameters.
OPERATORS
• Operators are the data connectors within expressions and equations.
They tell the computer how to process the data.
• They also tell the computer what type of processing (mathematical, logical,
or whatever) needs to be done.
• The types of operators used in calculations and problem solving include
mathematical, relational, and logical operators.
• Mathematical operators include addition, subtraction, multiplication,
division,Binteger division, modulo division, powers, and functions. The
computer has a symbol for each of them
RELATIONAL OPERATORS:
LOGICAL OPERATORS:
OR EXAMPLE:
For example, when a store might require a driver’s license or a check-cashing card on file for a
customer to cash a check.
• When the customer has a driver’s license, the check can be cashed. When she has a check-
cashing card, the check can be cashed.
• The expression is written as License OR Card.

AND EXAMPLE:
• when a store requires a customer to have both a driver’s license and a check-cashing card to cash
a check.
• The expression is written as License AND Card.

NOT EXAMPLE:
• The resultant of the NOT operator changes an operand from True to False, or from False to True.
• A programmer uses the NOT operator to change an operand to the opposite value.
• The processing of the operands
(as directed by the operators) always starts
with the innermost parentheses and works
outward, and processes from left to right.
• An equation stores the resultant of an
expression in a memory location in the
computer through the equal sign.

• The resultant of the expression


Length * Width would then be stored in a
memory location called Area.
• Equations are often called assignment
statements because the variable on the
lefthand side of the equal sign is assigned
the value of the expression on the right-
hand side.

Area=Length*Width
Example:
Evaluating a Mathematical Expression
EXAMPLE: Assume the programmer has written the expression

• Use the following values to evaluate the expression:


• The operations within the parentheses
would be completed first, moving from
left to right so X + Y would be completed
first, and Z + 6 second.
• According to the hierarchy, the
programmer would then complete the
multiplications and the divisions from
left to right. The division would be
completed after the multiplications in
this expression only because it is to the
right.
• The final operation to be completed
would be the subtraction.
Evaluating a Relational Expression
Example:
Evaluating a Logical Expression
Example:
Example:

References: PROBLEM SOLVING & PROGRAMMING CONCEPTS, Maureen Sprankle, Jim Hubbard, 9th Ed., Pearson Education Limited, 2012

You might also like