0% found this document useful (0 votes)
7 views40 pages

Intro to Computer Science Concepts

Uploaded by

samehabdelhaleem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views40 pages

Intro to Computer Science Concepts

Uploaded by

samehabdelhaleem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CSCI 101: Introduction to Computer Science

Lecture 01: First Program


Agenda
▪ Computer hardware and software
▪ How to write a program?
▪ Substitution in mathematics & programming
▪ Building our first program
▪ Memory
▪ Getting input / Displaying output
▪ Arithmetic expressions
▪ Sample programs
Computer Hardware

Processor
Computer Hardware
▪ Input Unit: accepts inputs from users, translates it, and transmits it to the processor (e.g.
keyboard, joystick, microphone and mouse)

▪ Output Unit: display processed information to the outside world (e.g. monitors, printers (2D
and 3D), speakers, and projectors)

▪ Arithmetic and Logic Unit: calculates arithmetic and logical expressions. It reads the
operands from memory, perform the operation and store the resultant value in memory.

▪ Memory Unit: stores programs and data (two classes primary and secondary)

▪ Control unit : coordinates the operation of all other units. It is the nerve center that sends
control signals to all units and senses their status .
Software Program
▪ A computer can perform a specific set of instructions, one at a time.
▪ Get input (from keyboard to memory)
▪ Display output (from memory to screen)
▪ Compute an arithmetic expression (*, /, +, -)
▪ Compute a logical expression and branch selection
▪ Repeat instructions

▪ A software program is a sequence of such instructions


Cooking
Introduction to Computer Science
Introduction to Cooking Science
▪ Cooking recipes can be thought of as simple algorithms
Omelette recipe:
1. Break 2 eggs in the bowl
2. Put grains of salt in the bowl
3. Mix together in the bowl
▪ Common between recipes and algorithms: 4. Pour in a hot pan

▪ Sequence of steps
▪ Performing step 3 means steps 1 and 2 are complete
▪ Has order, changing order MAY ruin it:
▪ Examples:
▪ Swap steps 1 and 2?
▪ Swap steps 2 and 3?
▪ Swap steps 3 and 4?
▪ Dependencies (building on previous steps) is the key to determine the order
Introduction to Cooking Science
Omelette recipe:
1. Break 2 eggs in the bowl
2. Put grains of salt in the bowl
3. Mix together in the bowl
4. Pour in a hot pan

▪ Add more complexity: keep mixing together till the mixture is


homogeneous.
▪ Use what is called while loop that repeats as long as a condition is
valid (True) Omelette recipe:
1. Break 2 eggs in the bowl
2. Put grains of salt in the bowl The condition that must remain valid for a
3. While not well mixed: while loop to keep iterating / repeating
i. Mix together in the bowl
4. Pour in a hot pan
Introduction to Cooking Science
▪ What if we want to make 5 Omelette plates?
▪ Repeat the recipe 5 times

Omelette recipe:
1. Repeat 5 times: Count (number of iterations)
i. Break 2 eggs in the bowl
ii. Put grains of salt in the bowl
iii. While not well mixed:
a. Mix together in the bowl
iv. Pour in a hot pan

▪ Called for loop and must repeat for a known number of times
But…
▪ The previous recipes must be taken with
grains of salt!! Omelette recipe:
1. Repeat 5 times:
▪ These steps are not accurate enough, the i. Break 2 eggs in the bowl
output can be different based on the ii. Put grains of salt in the bowl
iii. While not well mixed:
executer: a. Mix together in the bowl
iv. Pour in a hot pan
▪ How much salt?!!
▪ How well mixed is enough?!!
▪ How hot is the pan?!
▪ An algorithm must obtain the same
expected correct output every run.
How to write a software program?
1. Read the problem statement, and identify
▪ The input and its range
▪ The output
▪ The relationship between the input and the output (how to compute the
output) [Comprehend or understood]
2. Write your thoughts as a sequence of steps. [Algorithm]
3. Convert these steps to Code. [Program]
4. Test your code and compare your program result against a human
result. [Testing]
Writing a Computer Program: The Role of Algorithms
▪ An algorithm is a set of steps that describes how a task can be
performed (informal definition).
▪ Fundamental concept of computer science
▪ The word is derived from the famous mathematician Muḥammad ibn
Musa al-Khwarizmi, one of the first Directors of the House of Wisdom (a
major public academy and intellectual center), in Bagdad (9th century).
▪ Began as a subject in mathematics. The goal was to find a single set of
directions that describes how all problems of a particular type could be
solved.
▪ “If no algorithm exists for solving a problem, then the solution of
that problem lies beyond the capabilities of machines.”
1. From the book “Computer Science: an overview” by J. Glenn Brookshear
Algorithms: Formal Definition
▪ An algorithm is an ordered set of unambiguous, executable steps that defines a
terminating process.
▪ Ordered: Steps are executed in a sequence. (In parallel algorithms, we may have more than one
execution path)
▪ Unambiguous: A single and unique interpretation. The information must be sufficient to determine
uniquely and completely the required actions.
▪ Branch if the value of x is large enough. Is it unambiguous?
▪ Some algorithms (nondeterministic algorithms) do not conform to this restriction.

▪ Executable: A step that can be done.


▪ Make a list of all the positive integers. Is it executable

▪ Terminating: the execution must lead to an end.


▪ There are, however, some meaningful non-terminating processes such as monitoring the vital signs of a patient.

1. From the book “Computer Science: an overview” by J. Glenn Brookshear


Algorithm Representation
▪ Pseudocode:
▪ An intuitive notational system in which ideas can be expressed informally.
▪ Less formal than target programming language.
▪ Flexible, less complex, and easy to understand compared to formal
programming languages.
▪ Flowchart:
▪ A graphical representation of an algorithm.
▪ Provides a simple way to visualize transitions and analyze the execution
paths.
Pseudocode: Examples
▪ Write an algorithm to calculate the area of a circle
▪ Determine inputs, outputs, and relation

▪ Step 1: Get Radius from user


▪ Step 2: Set Pi = 3.14
▪ Step 3: Calculate Area = Pi * Radius * Radius
▪ Step 4: Print Area
Pseudocode: Examples
▪ Write an algorithm to convert temperature from Fahrenheit to
Celsius
▪ Determine inputs, outputs, and relation

T(°C) = (T(°F) - 32) × 5/9

▪ Step 1: Get Temp in Fahrenheit (F) from user


▪ Step 2: Calculate C = 5/9 * (F-32)
▪ Step 3: Print C
Exercise
▪ Find 𝑿𝟐 and 𝑿𝟑 of any given number X
▪ Determine inputs, outputs, and relation

▪ Step 1: Get X from user


▪ Step 2: Set XSquare = X * X
▪ Step 3: Set XCube = XSquare * X
▪ Step 4: Print XSquare, XCube
Substitution in Mathematics & Programming
▪ Software evolved first to serve solving mathematical problems.

▪ Therefore, it follows the main concepts of evaluating expressions

▪ Substitution in mathematical expressions works the same way in


code
Substitution in Mathematics & Programming
𝑥=4
𝑥
𝑦=𝑒
▪ Solving the above mathematically by substituting manually step by
step:
4
𝑦=𝑒
𝑦 = 𝑒2
𝑦 = 2.7182
𝑦 = 7.38
Substitution in Mathematics & Programming
import the library called
▪ In Python, it is written as: import math math which contains
x = 4 mathematical functions
▪ By substituting step by step: y = [Link]([Link](x))
Refer to the library
1. First 4 is assigned to x before calling any
function from inside it
2. Then x is substituted for, y = [Link]([Link](4))
3. Function [Link](4) is called which takes 4 as an input, gets evaluated
and returns back 2, now y = [Link](2)
4. Function [Link](2) is called which takes 2 as an input, gets evaluated
and returns back 7.38 which gets assigned to y
Exercise:
x = 4
y = [Link]([Link](x/2-2)*[Link])/[Link]([Link]/2)
Memory
▪ Memory is divided into bytes
x 15
y 12
▪ Each byte has its own address represented
by binary numbers sum 45
A 2
A1 -3
▪ 4 or 8 bytes form a memory location to store
integer or floating (Ex 10.2345) numbers
numOfBus 4

▪ Software programs use letters and numbers


to name the memory location.
Memory Location Name (Variables)
▪ Variable: a named space for storing a value radius
▪ Valid names:
▪ Must start with a letter or ‘_’ ,
▪ Can contain any combination of letters, digits, or ‘_’.
▪ Use meaningful variable names (avoid reserved words).
▪ Python (as well as most others) is case sensitive (area and Area are
different)
▪ Exercise
Xyz 3xyz x-y ab2cd a12345 myvar AbCdE1234
Variable Types
▪ Integer:
▪ x = 4
▪ y = x * 2
▪ Using substitution, y = ?
▪ type(x) Built in function to returns the type of
the object
▪ type(y)
▪ type(x * 2)
▪ Again follow substitution
Variable Types
▪ Float/decimal:
▪ x = 4.0
▪ y = x * 2
▪ Using substitution, y = ?
▪ type(x)
▪ type(y)
▪ type(x * 2)
Variable Types
▪ String:
▪ x = ‘A’
▪ x = ‘abc’
▪ x = ‘4’
▪ x = ‘4.0’
▪ x*2
▪ x + ‘3’
▪ type(x + ‘3’)
▪ x + 3
Variable Types
▪ Conversions:
▪ int(‘4’)
▪ int(4.5)
▪ float(‘4.2’)
▪ float(‘4’)
▪ str(4.2)
▪ ‘Area=’ + str(4.2)
Get Input from User
h ‘5’
h=input(‘Enter height: ’);
Memory location
to store the input Message to the user

Keyword to hold the


program execution to
get input from the keyboard

Enter height: 5

Note: the default input is string data type


Get Input from User
h 5
h=int(input(‘Enter height: ‘))

Returns (substitutes in its place) the string ‘5’


int(‘5’)=5

Enter height: 5

? Write a Python statement to take the circle radius from the user and store it in ‘R’.
Display to Screen
print(h) h 5
a 102
Keyword to display
Memory location
to the screen
to display

→ 5

print(a)
→ 102
Display to Screen
print('Height = ' + str(h)) h 5
→ Height = 5
print('Height =', h) a 102
→ Height = 5
Note regarding separating by a comma:
▪ No type conversion needed
▪ Automatically inserts space between the elements
print('Height =', h, 'and area =', a)
→ Height = 5 and area = 102
Arithmetic Expressions
Operations
x 4
** Exponentiation (5**2 is 25)
*, /, // Multiplication, division, and floor division y 3.5
+, - Addition and subtraction z 2
Examples a 6.5
x=4; y=3.5; z=2
a = x + y - 5 / (3+z)
b 8.5
b = a + x / 2
(semicolon can be used to separate multiple lines)
Operations are executed only one at a time according to their
precedence (priority). We will study it further in future lectures
How to write a software program?
1. Read the problem statement, and identify
▪ The input and its range
▪ The output
▪ The relationship between the input and the output (how to compute the
output) [Comprehend or understood]
2. Write your thoughts as a sequence of steps. [Algorithm]
3. Convert these steps to Code. [Program]
4. Test your code and compare your program result against a human
result. [Testing]
Calculate Area of a Rectangle
(Problem → Algorithm)
▪ How a computer can solve this problem?
▪ I get the values of the height and the → Get height as h
width from you and I store them in my → Get width as w
memory.
▪ Then I multiply them in my brain and → Calculate area = h*w
store the result in my memory
▪ And inform you about the resultant value. → Print area
Calculate Area of a Rectangle
(Algorithm → Program)

Get height as h h = int(input(‘Enter height: ’))

Get width as w w = int(input(‘Enter width: ’))

Calculate area = h*w area = h * w

Print area print(area)


Calculate Area of a Rectangle
(Program and Testing)
h 5
h=int(input(‘Enter height: ’)) w 4
w=int(input(‘Enter width: ’)) area 20
area = h * w
print(area)

Enter height: 5
Enter width: 4
20
Calculate Area of a Circle
(Algorithm, Program, Testing)
import math

1. Get radius r r = int(input(‘Enter radius:’))

2. Calculate area = pi*r**2 area = [Link]*r**2

3. Print area print(area) radius 5


area 78.5398

Enter radius: 5
78.5398
Calculate Number of Buses to Transfer Students

(Algorithm)
[Link] number of students as numS
[Link] the bus capacity as busC
[Link] num of buses as
numB = numS/busC (round up)
4. Print numB
Calculate Number of Buses to Transfer Students
(Program)
import math
numS = int(input(‘Enter number of students:’))
busC = int(input(‘Enter bus capacity:’))
numB = numS/busC → numB = [Link](numS/busC)
print(numB)

If numS=60 and busC = 50 then numB = 60/50 = 1.2 ???


But it should be 2

[Link](1.2) = 2 ceil rounds up to nearest integer


[Link](1.2) = 1 floor rounds down to nearest integer
Calculate Number of Dozens and Remainder
(Algorithm)
[Link] a number as N
[Link] dozen = N/12 (no fraction)
[Link] the remainder (r) of dividing N by 12
[Link] dozen
[Link] r
Calculate Number of Dozens and Remainder
(Program)
import math
N = int(input(‘Enter a number’))
dozen = N/12 → dozen = N // 12
r = ?? →r = N % 12
print(dozen)
print(r)

N // 12 is the same as [Link](N / 12)

// called floor division

You might also like