Lab 01
Lab 01
Lab
Dr Asad Mansoor Khan
LE Kashaf Raheem
Instructors
• Instructor: Dr. Asad Mansoor Khan
• Email:
For Queries: [email protected]
[email protected]
For Assignments: Through LMS (In case of any issue, submission can be
done at [email protected])
Introductions
4
Learning Management System
(LMS)
• Please enroll on LMS using course code:
Lecture and Lab: 924861507 (Syn-A), 206815974 (Syn-B)
• All content including assignment, lab manual, lectures etc. will be available
there
• In case you need to contact please use the following contact details:
[email protected]
[email protected], [email protected]
5
Guidelines For Lab Work
• Individual to turn-in their own work
• Each Lab will be marked based on a specific rubric scheme (shared on next
slide)
• Please observe
Marks will be deducted for ‘plagiarized/copied’ lab reports.
20% per day penalty for late labs
all labs MUST be checked off to pass the course
lab assignments (when given) MUST be done in groups of 2 or 3
6
Marking Rubrics
Exceptional Acceptable Amateur Unsatisfactory
Trait [10-8] [7-5] [4-3] [2-0]
Functionality The program works and meets The code is fairly efficient The code is unnecessarily The code is huge and
all the specifications and is without sacrificing readability long and inefficient and does not meet any
and efficiency extremely efficient without and understanding and also meets only few of its specification.
40% sacrificing readability and meets almost all of its specifications.
understanding. specifications but not all.
Output The program produces correct The program works and produces The program produces The program is
results and display them the correct results and displays correct results but does producing incorrect
20% correctly in an organized way. them correctly not display them results.
correctly.
Input All the inputs entered by user In case of wrong input, the Some of the inputs are No input validation.
are properly validated, in case programs continue execution. validated leaving the
Validation of wrong input entered by user others.
20% the program displays to the
user to enter the correct input.
Code The code was well documented The code was sufficiently The code was documented The code was not
with proper commenting and documented with comments and with comments and with document properly
Presentation variable names. variable names (to an extent). improper variable names without comments and
(Commenting/ or vice versa. proper variable name.
Readability)
20%
7
Lab Assessment
• Lab Work + Quizzes: 40-50%
• Project: 15-20%
8
Lesson Plan
Algorithm
Designing and Logic
Processor Development
(Verilog (Python)
HDL)
Accessing
Processor using
MARS
(Assembly)
9
Lab # 1
Introduction to Computers
Algorithms and Python!!!
10
Computing Machines
• Ubiquitous ( = everywhere)
General purpose: servers, desktops, laptops, PDAs, etc.
Special purpose: cash registers, ATMs, games,
telephone switches, etc.
Embedded: cars, hotel doors, printers, VCRs, industrial
machinery, medical equipment, etc.
• Distinguishing Characteristics
Speed
Cost
Ease of use, software support & interface
Scalability
11
Two Pillars of Computing
• Universal Computational Devices
Given enough time and memory, all computers can
compute the same things (irrespective of speed, size or
cost).
Turing’s Thesis: every computation can be performed by some
“Turing Machine” - a theoretical universal computational device
• Problem Transformation
Convert a problem expressed in natural language into
electrons running around a circuit!
That’s what Computer Science and Computer Engineering
is all about: a continuum that embraces software & hardware.
12
Two recurring themes
• Abstraction
The concept that we can concentrate on one “level” of
the big picture at a time, with confidence that we can
then connect effectively with the levels above and below.
• Hardware vs. Software
Abstraction does not mean being clueless about the
neighboring levels.
Hardware and software are inseparably connected,
especially at the level we will be studying.
13
Problem Transformation - levels of
abstraction
The desired behavior: Natural Language
the application
Algorithm
Program
Machine Architecture
Micro-architecture
Logic Circuits
The building blocks: Devices
electronic devices
16
Levels of • These levels do not necessarily
Abstraction correspond to discrete components,
but to well defined standard
Natural Language interfaces.
Standard interfaces provide
Algorithm
portability
Program third party software/hardware
wider usage
Machine Architecture
• These levels are to some extent
Micro-architecture
arbitrary - there are other ways to
Logic Circuits draw the lines.
Devices
17
The Program
Level
Most computers run a Application Program
management program
called the operating
system (OS). Operating System
Application programs
interface to the machine
architecture via the OS.
Program (Software)
An example:
This lecture Data
18
Algorithms!
19
Computational problems
• A computational problem specifies an input-output
relationship
What does the input look like?
What should the output be for each input?
• Example:
Input: an integer number n
Output: Is the number prime?
• Example:
Input: A list of names of people
Output: The same list sorted alphabetically
20
Algorithms
• A tool for solving a well-specified computational problem OR Steps
used to solve a problem
• A well-defined computational procedure that takes some value, or
set of values, as input and produces some value, or set of values, as
output.
21
Problems and Algorithms
• We need to solve a computational problem
“Convert a weight in pounds to Kg”
22
From Algorithms to Programs
Program
Problem Algorithm (in our
case
Python)
Algorithm: A sequence of
instructions describing how
to do a task (or process)
23
Developing an
Algorithm
Program Development
1. Understand the problem
2. Represent your solution (your algorithm)
Pseudocode
Flowchart
3. Implement the algorithm in a program
4. Test and debug your program
25
Step 1: Understanding the Problem
• Input
What information or data are you given?
• Process
What must you do with the information/data?
• Output
What are your deliverables?
26
“Weekly Pay” Example
Problem Solution
• Create a program to • Input: pay rate and number of
calculate the weekly pay of hours
an hourly employee • Process: multiply pay rate by
What is the input, process, number of hours
and output? • Output: weekly pay
27
Step 2: Represent the Algorithm
• Can be done with flowchart or
pseudocode
• Flowchart
Symbols convey different types of
actions
• Pseudocode
A cross between code and plain English
28
Three Base constructs of An
Algorithm
• Three basic control flow elements which exist in
algorithm
Sequence of operations/expressions/statements
Decision making
Iterating an action again and again →
Repetition or looping
29
Flowchart Symbols
Start
Start Symbol Input / Output
End
End Symbol Decision Symbol
Display “Amount
End
paid per hour: ” 33
Examples
Draw Flowcharts for each problem
34
Example 1
Problem Solution
• Write an algorithm in pseudocode • AverageOfTwo
that finds the average of two numbers
• Input: Two numbers
• End
35
Example 2
Problem Solution
• Write an algorithm in the form of a • ComputeBMI
pseudocode that computes BMI using
weight in pound and height in inches. • Input: Weight (𝑤) and Height (ℎ)
𝑤𝑒𝑖𝑔ℎ𝑡
𝐵𝑀𝐼 = 703 ∗ 1. Hsquare ← ℎ2
ℎ𝑒𝑖𝑔ℎ𝑡 2
𝑤
2. wbyh2 ← Hsquare
• Output: BMI
• End
36
Algorithms and Language
• Notice that developing the algorithm doesn’t involve any
language at all
Only pseudocode or a flowchart was needed
An algorithm can be coded in any language
37
Steps 3 and 4: Implementation
and Testing/Debugging
• We’ll cover implementation in detail.
38
Programming
through Python
Install latest version of IDLE on your laptops
Link: https://www.python.org/downloads/
39
Introduction to Python
• Python is a high-level programming language – like C++/Java
• Open Source and has a large community
• Used by a lot of companies around the world
• The popularity of Python has increased significantly in the last
decade
• Interpreted Scripting Language
Good for writing “quick” code
• Python is object oriented
Everything is a Python object
• Different code blocks are grouped together by indentation
40
General Information
41
Variables
• No need to declare
• Need to assign (initialize)
use of uninitialized variable raises exception
• Not typed
if friendly:
greeting = "hello world"
else:
greeting = 12**2
print(greeting)
• Everything is a "variable":
Even functions, classes, modules
42
Indentation and Blocks
• Python uses whitespace and indents to denote blocks of
code
• Lines of code that begin a block end in a colon:
• Lines within the code block are indented at the same
level
• To end a code block, remove the indentation
• You'll want blocks of code that run only when certain
conditions are met
43
Whitespace
• Whitespace is meaningful in Python:
Especially indentation and placement of newlines.
• Use a newline to end a line of code.
Use \ when must go to next line prematurely.
• No braces { } to mark blocks of code in Python…
Use consistent indentation instead.
The first line with less indentation is outside of the block.
The first line with more indentation starts a nested block
• Often a colon appears at the start of a new block. (E.g. for function
and class definitions.)
44
Comments
• Start comments with # – the rest of line is ignored.
• Can include a “documentation string” as the first line of
any new function or class that you define.
• The development environment, debugger, and other tools
use it: it’s good style to include one.
def my_function(x, y):
“““This is the docstring. This
function does blah blah blah.”””
# The code would go here...
45
None
• “None” represents the lack of a value.
• Like “NULL” in some languages or in databases.
• For instance:
>>> if y!=0:
... fraction = x/y
... else:
... fraction = None
46
Linking this with
Algorithm-Flowchart
47
Input Input
48
Output Output
>>>print("Value of A is ", A)
Value of A is 10
49
Sequence!
Data Processing Symbol
• Mathematical expressions
• Built-in functions/libraries
50
Mathematical Functions
• 𝒂𝒃𝒔(𝒙) – absolute value
• 𝒅𝒊𝒗𝒎𝒐𝒅(𝒂, 𝒃) - divide a by b and return both the quotient and the
remainder
• 𝒉𝒆𝒙(𝒙) - Convert an integer number (of any size) to a hexadecimal
string.
• 𝒐𝒄𝒕(𝒙) - Convert an integer number (of any size) to an octal string.
• 𝒓𝒐𝒖𝒏𝒅(𝒙[, 𝒏]) - Return the floating-point value x rounded to n digits
after the decimal point.
51
Example: Weight into Kg
• End
52
Example: Compute Pay
Display “Amount
End
paid per hour: ” 53
Solution:
Problem Solution
• Write pseudocode of an algorithm • ComputerHypo
which can compute the value of
hypotenuse if user provides value of • Input: 𝑝 and 𝑏
perpendicular and base.
1. Compute 𝑝2
ℎ2 = 𝑏 2 + 𝑝2
2. Compute 𝑏 2
3. Add gets 𝑝2 + 𝑏 2
• Output: ℎ
• End
55
Program
• End
56
Tasks
Make sure to comment your code.
Use appropriate variable name
Know the location where file is being saved
Create different files for different tasks
57
Task 1
• Create
an algorithm and code
it through python for the
problem that takes two
numbers and displays the
sum, sub, divide, multiply
and square of numbers
entered, on the console
window .
58
Task 2
60
Next week: Control
Flow!
Advice:
Install latest version of IDLE on your laptops
Link: https://www.python.org/downloads/
62