Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

[Week 2] Intro to problem solving

The document is a lecture note on problem-solving concepts in computing, focusing on variables, constants, data types, functions, and operators. It covers the definitions and examples of constants and variables, naming conventions, data types (numeric, character, logical), and the importance of functions and operators in programming. Additionally, it includes exercises and questions to reinforce learning and understanding of the material presented.

Uploaded by

giyun.2200434
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

[Week 2] Intro to problem solving

The document is a lecture note on problem-solving concepts in computing, focusing on variables, constants, data types, functions, and operators. It covers the definitions and examples of constants and variables, naming conventions, data types (numeric, character, logical), and the importance of functions and operators in programming. Additionally, it includes exercises and questions to reinforce learning and understanding of the material presented.

Uploaded by

giyun.2200434
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 63

www.covenantuniversity.edu.

ng

Introduction to Problem
Solving Concepts in
Computing

Lecture Note (Week 2)


The CSC121 Team

1
Learning Objectives
 Understand variables and constants.
 Know the data types and when to use
them.
 Know how to create a function.
 Learn the operator precedence rule.
 How to write and evaluate expressions
and equations.

2
Introduction
 Computers can solve mainly three types
of problems:
– Computational (requires mathematical
processing)
– Logical (requires decision making i.e.
choosing the best from several available
options)
– Repetitive (reoccurring steps).

3
Introduction
 Our focus in this class is to have a deep
understanding of the basic elements
used to construct simple statements
that can be used to solve these
problems.
 The elements that add up to form
statements include: variables, constants,
data types etc,
 Each one will be discussed briefly.
4
Constants
 A constant is a value that can either be
an alphabet, numeric value, combination
of both(called alphanumeric) or special
characters.

 Note: a Constant not does change during


its use within a program.

5
 Example of a constant is PI.
 Conventionally constants are written in
CAPITAL LETTERS.
 In C, they begin with const.
 So, we write:
const PI;
Note: Read Chapter 2 of Programming with C
for more details

6
Example on Constants
 const int Book = 50
 A memory location is allocated to hold
50 for book.
 Book can call for it at any time and
memory must always return 50.

7
Variable
 A variable value unlike a constant can
change during processing of a set of
instructions.
 The variable name act as a reference to
the value stored in the memory.

8
Example on Variable
int book = 50
 a variable called book is allocated a
memory space to hold the value 50 for
the duration of the program.
 It is a temporary memory allocation.

9
Naming Conventions for
Constants and Variable
1. Name a variable according to what it
represents.

Example
1. Hours for hours worked,
2. StudentName for name of student.

 Ensure the name is short but clear.


10
Naming Conventions for
Constants and Variable Contd
2. Avoid using spaces in a variable name.

Example:

HoursWorked not Hours <space> Worked.

11
Naming Conventions for
Constants and Variable Contd
3. Start a variable name with a letter.

Example:
StudentName not 01StudentName

12
Naming Conventions for
Constants and Variable Contd
4. Avoid using mathematical operators
(example -,+, …) in variable name.

Example:
Student_Name not Student/Name

13
Naming Conventions for
Constants and Variable Contd
5. Stick to only one variable name.
Example:
StudentName
studentName
studentname

They are not the same.

14
Naming Conventions for
Constants and Variable Contd
6. Be consistent when using upper and
lower-case characters.
Example:

A is not a.

15
Naming Conventions for
Constants and Variable Contd
7. Familiarize yourself with the naming
convention of the language being
taught and use just that.

16
Naming Convention Examples

17
Data Types
 Data is raw fact.
 Unprocessed details that goes into the
system as input, is processed and
returned to the user as information.

18
Data Types Contd
Example:
 To predict your grade for CSC 121.
– data includes what you hope to score in
• Test1= ?, …
• Examscores = ?
– Processing includes:
• Total_testScore= Test1 + …+ Examscores
– Output/information will be the grade:
• A,B,… , F
19
Data Types Cont’d
 You have to specify the data type for
each variable or constant.
 Common data types includes: numeric,
character, logical, date.

20
Data Type: Numeric Data
 Include any data in number format.
 Two subtypes
– Integers (known as int in C )
• They are whole numbers e.g 3.
• It includes all whole (negative and positive) numbers
within the limitations of the computer or the programming
language.
– Real numbers (called float in C)
• Numbers with decimal parts e.g 3.00.
• It includes all whole (negative and positive) with decimal
parts within the limitations of the computer or the
programming language
21
Data Type: Numeric Data
Contd
 They are used for calculations
Note:
Zip codes, account numbers are usually
not classified as numeric, even when they
are just numbers, because sometimes
they may have characters in them.

22
Question

Are integers subset of real numbers?

23
Data Type: Character Data
 Also known as alphanumeric dataset.
 It is made up of numbers (0,…,9),
letters(a,…,z, A,…, Z) and special
characters.
 Characters are usually placed between
quotation marks “ ”.
 Using ASCII, there are 256 characters,
with 128 standardised characters, so will
be the same on different systems.
24
Data Type: Character Data
Contd
 It cannot be used for calculations.
 A string is a collection of two or more
characters.
 Character data is a subset of string data.
 Example of character = “A”
 Example of string = “Apple”

25
Data Type: Character & String
Data
 Note that since the computer only
understands numbers, every character
is assigned a number by the computer.
 Example A is assigned …

26
Assignment (In your note)

Write out the first 128 standardized


characters and their corresponding
computer assigned numbers.
Do the computation for the following
strings:
1. apple
2. APPLE
3. Your first and last name(with no space
in between).
27
Data Type: Character & String
Data Contd
 You can join character/string data together
by concatenation.
 Concatenation is the joining of two or more
characters/Strings using the + operator.
 What happens is that the second item is
placed directly at the end of the first item.
 Remember character or string data must
be in a “”.
 Example “Ade” + “Dele”= “AdeDele”
28
Data Type: Logical Data
 It has just two values:
– True/T/Y/yes
– False/F/N/no
 It can be used in decision making
statements that has a yes or no answer.
 Example: Are you a student of Covenant
University?
– Yes means you are with matric No as a proof.
– No means you are not.
29
Data Types contd
 Other data type includes: date data type
 It is possible for a user to define a data
type.
 The user will have to list out the items in
the data set unless it will not be
recognised as an item in that set.

30
Examples of Data Type

31
Data Storage
 Memory location is used by the
computer to store data internally.
 You can access the data stored in the
memory using the variable name.
 Internal memory is volatile (temporary).

32
Data Storage Contd
 Data and instructions stored on it are
lost as soon as the computer is switched
off or at the completion of the program
execution.
 To avoid loss, data, information,
programs (set of instructions) that needs
to be stored for future use, must be put
on external storage devices such as hard
drive in the file storage area.
33
Data Storage Contd
 There are two types of files:
– Program files holds the instructions for the
computer.
– Data files holds the data that will be used by
the program files during execution.

34
Functions
 These are sets of instructions used to
perform specific tasks and will return a
value.
 Functions are usually used when a given
task will have to be repeated severally.
 Function will hereby shorten the
processing time and also helps
readability.

35
Function Contd
 It takes the format:

FunctionName(data)
• FunctionName = name given to the function
• Data = are parameters used by the function to
perform the given task.

36
Function Contd
Example.
Mean(x,y,z)
is a function that will return the mean of
the three numbers x,y,z.

– Function Name is Mean


– Function Parameters are x,y,z.

37
Function Examples
 Mathematical functions:
– can be used primarily for business purposes. E.g
square root.
 String Functions:
– are used on string variables. Operations include
finding string length.
 Others include
– Conversion(converts data from one type to another),
– statistical (used for calculations),
– utility(can access information outside the program)
e.t.c
38
Examples of Mathematical &
String Functions

39
Examples of Conversion, Statistical
and Utility Functions

40
Function Contd
 Please note that there are more
functions than those stated above.

41
Operators
 They are symbols that take one or more
operands or expressions and perform
computations on them.
 Can be used to connect data within an
expression or equation.
 Operands are the data which are to be
processed.
 Resultant is the output(answer)
generated.
42
Operators Contd
 Example
14+2=16
• Operators are + and =
• Operands are 14 and 2
• Resultant is 16
 The data type of the operands and
resultant depends on the operator.

43
Types of Operators
 There are mainly 3 types for calculations:
– Mathematical operator (such as +)
• for calculations and formulas.
– Relational operator (such as =)
• for decisions and loop controls in cases of repetition.
• The resultant is usually a logical operator TRUE or
FALSE.
• The operands must be of the same datatype.
– Logical operator (such as AND)
• to connect relational operands/expressions.
44
Types of Operators Contd

45
Hierarchy of Operations

46
Question (1 minute)
 What is the difference between / and \
operators?
– / is used for fraction division.
• Example: 9.0/2.0 = 4.5.
– \ is used for integer division.
• Example: 9\2 = 4.

47
Expressions and Equations
 An expression is a combination of one
or more operands, operators to produce
a value(resultant).
 The resultant produced is not stored in
the memory so cannot be reused at a
later time.
 Equations on the other hand is an
expression whose resultant is allocated a
memory space using the “=“ operator
sign. 48
Expressions and Equations
Contd
 “=” is also known as assignment
operator.
 Usually of the form
– Variable = value1 [operator] value2
– The = sign means “is assigned the value of”
– The combination of value1, operator and
value2 is called an expression.

49
Expressions and Equations
Contd

50
Question 1: Fill in the table with an
appropriate variable name and data type.

51
Question 2: Name the data type
of the following
a. 5.38
b. “87654”
c. True
d. “A”
e. “707-434-5555”
f. “New York”
g. -389
h. 2.45E6
i. 48976.0
j. False
52
Question 3: Set up an equation to calculate the
following (create your own variable names):

a. The area of a room.


b. The sale price of an item given an
original price and a percentage discount.

53
Question 4: Find the result of the
following operations
1. 5+4
2. 10/2
3. True OR False
4. 20 MOD 3
5. “A” > “H”
6. NOT True
7. False AND True
8. 20 * 0.5
9. 35 <= 35
54
Question 5: list the order in which the following
operations
would be processed.

1. +, -, *
2. /, \, =
3. OR, *, <
4. NOT, AND, *
5. NOT, >, +
6. AND, OR, NOT
7. <, AND, >, +
8. *, ^, +
9. NOT, +, \
10. MOD, \, <
55
Question 6: Evaluate the following equations,
given the values A = 12, B = 3, C = 6, D = 2:

1. F = A + B/C - D ^2
2. F = (A + B)/ C - D ^2
3. F = A + B/(C - D ^ 2)
4. F = (A + B)\ D ^ 2

56
Question 7: Write the following
equations in computer form:
X = Y + 3Z -
X = 5Y +
X = (X – Y)^2

57
Question 8:
 Roger would like to know the average of
his test scores. Write an equation that
would calculate the average given five
test scores. Write the equation with and
without using a function.

58
Question 9: Evaluate the following equations,
given A = False, B = True, C = False, D = True.

a. R = A AND B OR C AND D
b. R = NOT (A AND B) OR NOT (D AND C)
c. R = (A OR B) AND (D OR C)
d. R = NOT (A AND B OR C) AND (A OR B
AND D)
e. R = C OR NOT (A AND D) AND (A OR B)
OR NOT (A OR C)

59
Question 10: Set up a logical
expression for the following conditions.

A company gives a bonus at the end of


each fiscal year. For an employee to get a
bonus, the following must be true:
a. The employee has been working at the
company for more than six months with
no negative reports.
b. The employee has earned more than
$5,000 during the fiscal year.

60
Question 11:
What is wrong with these variable names? Can
you correct them?
a. City Name referencing the name of a city.
b. Client-name referencing a client name.
c. City/State referencing a city and state.
d. LN referencing a last name.
e. Street address
f. Q for a quantity of books
g.Street_Address_for_Joe’s_Hardware_Supply_Inco
rporated_Client
61
Assignment
 Write short note on Memory Allocation
as it concerns Variables and Constants.
– Maximum of 2 pages
– Submission via Moodle
– Lifeline: Friday Jan 29, 2016.

62
www.covenantuniversity.edu.ng

End OF Class

63

You might also like