APSC 142 INTRODUCTION TO COMPUTER PROGRAMMING FOR
ENGINEERSWINTER 2017
Instructor
Tashfeen Karamat, Ph.D.
Department of Electrical and Computer Engineering
Lecture 1
About Me
• Tashfeen Karamat
• Assistant Professor (Adjunct)• Royal Military College of Canada (RMC)
• Navigation and Instrumentation Research group at RMC
• Education• PhD Queen’s 2014
• MASc RMC 2008
• M.Eng Queen’s 2006
• Publications• Book on GPS/INS- Oct, 2012
• Several papers on GPS/INS
• Interests• Photography
• Etymology
• Astronomy
© Tashfeen Karamat 2017 2
Course notes
• No audio/video recording of the lectures (it’s a copyright
material)
• Course official page is not yet functional
• As an interim measure to get the notes, go to following
link:
• http://karamat.segfaults.net/
• Click APSC142
• Then click PDF of week 1 to download the lecture
• This is only for week 1. For rest of the lectures, go to onQ
• Make sure you use onQ regularly
• Enable notifications etc. to get the info quickly
3© Tashfeen Karamat 2017
Course Organization & People
Lectures1 hour/wk
Studios1.5 hours/wk
Labs2 hours/wk
• Brian Lynch (Sec. 100)
• Tashfeen Karamat (Sec. 101)
• Malek Karaim (Sec. 102)
• Course Instructor/Graduate studio TA
• Undergraduate studio TA
• Graduate TA
• Undergraduate TA’s
4© Tashfeen Karamat 2017
Course Activities• Lecture:
• 1 hour/week
• Studio (Practice programming):
• 1.5 hours/week – beginning in Week 2
• Studio exercises – weeks 2-11
• Quizzes: weeks 3, 6, 9 and 12
• Lab (Robots):
• 2 hours/week – beginning in Week 2
• Lab exercises – required programming tasks
• Project – demonstrated in week 11 or week 12
• Office hours
• Before and after the lectures
• Setup a time through email ([email protected])
5© Tashfeen Karamat 2017
Course Schedule (Lectures)
6
Lecture 100 – Ellis Hall Auditorium
Lecture 101 – Ellis Hall Auditorium
Lecture 102 – Stirling Hall Auditorium
© Tashfeen Karamat 2017
Course Schedule (Studios)
7
Studios – Beamish-Munro Hall 213
© Tashfeen Karamat 2017
Course Schedule (Labs)
8
Studios – Beamish-Munro Hall 212/214
© Tashfeen Karamat 2017
Marking scheme
• Lab exercises – 10%• best 7 out of 9 will be counted
• Studio exercises – 10%• best 7 out of 10 will be counted
• Quizzes – 10%• best 3 out of 4 will be counted
• Project – 25%
• 3% for project proposal (week 7/8)
• 10% for project demonstration (week 11/12)
• 12% for project report and code (week 11/12)
• Final Exam – 45%
9© Tashfeen Karamat 2017
Studios
• Short video lecture summarizing weekly material and demonstrating examples
• Studio exercise to be demonstrated to TA (assigned one week ahead, may be completed at home or during studio)
• Studio exercises are marked out of 3:• 3 marks if exercise program completed successfully
• 2 marks if exercise program partially complete
• 1 mark if exercise program did not work, but you made some effort
• Changing studio sections is NOT permitted!
10© Tashfeen Karamat 2017
Studios
• Studios run from week 2 to 12 in BMH 213:• Weeks 2-11: studio exercises
• Week 12: review for final exam
• There will be 4 quizzes written during studio time:• Weeks 3, 6, 9 and 12
• The best 7 out of 10 studio marks and the best 3 out of 4 quiz marks will count towards your final grade
11© Tashfeen Karamat 2017
Labs
• You will work in pairs to program a Lego EV3 robot using RobotC• Lab exercises will be posted on course
website prior to each week’s lab
• Can be tested at home with simulator
• Marking: labs are marked out of 3• Lab exercises (weeks 2-7)
• 3 marks for successful completion
• 2 marks for partially complete exercise
• 1 mark for attendance
• Project labs (weeks 8-10)• 3 marks for attending the full lab session
12© Tashfeen Karamat 2017
Labs
• Labs run from week 2 to 12 in BMH 212/214:• Weeks 2-7: lab exercises with specific tasks
• Weeks 8-10: project labs; work on your robot project
• Weeks 11-12: project demonstrations
• The best 7 out of 9 lab marks will count towards your final grade
• Changing lab sections is NOT permitted!
13© Tashfeen Karamat 2017
Lab Procedures
• At the beginning of each lab, you will receive an attendance/sign-out slip
• Write both partners’ names and student numbers on the slip
• The slip is used to sign out a robot to use in the lab and for TAs to record the lab marks
• You must return all equipment and turn in your slip at the end of the lab
• Preparation• Read each week’s lab material before going into the lab! You can
even begin to prepare some of your programs using the simulator.
14© Tashfeen Karamat 2017
Lab Project Details
• You and your lab partner will define a project by Week 7
• The project involves designing software to make the EV3 robot do something fun and fancy
• A short project proposal is due in Week 8
• You will work on your project during the lab sessions in Weeks 8-10
• Project demonstrations will be held in week 11 or week 12
• After demonstrating your project, hand in a project report, including a print-out of your project code (in the same lab period as your demonstration).
• There will be a bonus of 2 marks if you demonstrate your project and submit your report in week 11
• Information and rules for the project, project proposal and project report will be posted on the course web page and on course website
15© Tashfeen Karamat 2017
Resources
The primary resource for APSC 142 is the course website page. You will find:
• general information on course organization & policies • lecture & studio slides (for all sections) • lab exercises• weekly practice exercises and solutions• Robot simulator (for use at home)• marks• information and links to additional resources on C programming
• Notes: • answers (where appropriate) to studio exercises will be posted, but
full solutions will not be provided; if you miss a studio, you should do the programming exercise and check your answer
• partial solutions to lab exercises will be provided
16© Tashfeen Karamat 2017
Computer Organization and Architecture
• A computer is a machine that can solve problems by carrying out a
series of instructions
• The machine – or physical device – is the hardware; the set of
instructions – or program – is the software
17
task main()
{
float x1, y1, x2, y2, x3, y3;
float slope, yint;
x1 = 15;
x2 = 75;
y1 = 20;
y2 = 90;
x3 = 60;
// compute slope
slope = (y2-y1)/(x2-x1);
nxtDisplayTextLine(1,"Slope is:");
nxtDisplayTextLine(2,"%0.3f", slope);
// compute intercept
yint = y1-x1*slope;
nxtDisplayTextLine(3,"Intercept is:");
nxtDisplayTextLine(4,"%0.3f", yint);
// Find y3
y3 = slope*x3+yint;
nxtDisplayTextLine(5, "y3 is: %0.2f", y3);
}
2000 BCE
2000 CE
© Tashfeen Karamat 2017
Input / output control
Hard drive Keyboard Monitor Modem Robot hardware
USB drive Scanner Printer WiFi
DVD Mouse
Computer Organization and Architecture
18
I/O and storage
devices
ALU
Control
Regis
ters
Central processing unit (CPU)
RAM RAM
Memory
Four components are necessary for computing:input – get information (data) into the computer
storage (memory) – store data and instructions
processing – manipulate the data
output – get answers out of the computer
© Tashfeen Karamat 2017
Computer Applications
• Today, you can find computers everywhere.
19
Type Price ($) Example
Disposable computer 1 – 5 Greeting card
Embedded computer 10 – 100 Watch, appliances, cell phones, cars,
robots
Game computer <300 Xbox, Wii, Playstation
Personal computer 1000 Desktop, laptop, tablet
Server computer 4000 Network servers
Cluster computer 20K – 200K Low-end to high-end applications
Massively parallel
computer
10M Grand challenge applications
© Tashfeen Karamat 2017
Computer Applications
• Today, you can find computers of different shapes and sizes.
20
Flexible smart phone
Xbox gaming system
IBM Blue Gene Super
computer
© Tashfeen Karamat 2017
Programming as a Tool
• Engineers and scientists use programming as a tool for problem solving.
21
Analyzing air flow around a
race car Simulating dispersion of Chlorine
after a spill
Solving for protein structures that fight
diseases
© Tashfeen Karamat 2017
C programming language• C is a classic (procedural) programming language
• It is one of the most popular languages of all time• Used by engineers in industry
• Low run-time and access to hardware devices
• C has been used to develop:
22
Operating
Systems
Embedded
Systems
Computational
Software
Android
Mac OS
Unix
Windows
Automobiles
Robots
Biomedical devices
(e.g. pacemakers)
Maple
MATLAB
Mathematica
© Tashfeen Karamat 2017
RobotC versus Standard C
• RobotC is a subset of standard C developed for
specific robotic systems
• some commands are named differently
• some commands are specific to the robot function, e.g.
setMotorSpeed
• RobotC displays output on the LCD display of the Lego
robot, not the computer screen
• RobotC has no keyboard input
• We will use RobotC for the Lego EV3 robot
23© Tashfeen Karamat 2017
RobotC
• RobotC is a licensed product, available to
students in BMH 212/214
24
http://i.imgur.com/rsJYvMg.png
© Tashfeen Karamat 2017
RobotC• QEV3 Simulator is developed at Queen’s for students to use in the labs, studios, and at home• For use with studio exercises, lab exercise preparation, and project
25© Tashfeen Karamat 2017
What We Will Cover
• Syntax: rules for writing programs that a computer can
read
• Programming structures:
• Conditional statements [if ... else]
• Repetition [while and for loops]
• Techniques for storing and manipulating large amounts of data
[arrays]
• Engineering Applications
• Moving and manipulating robots
• Simulating physical problems
• Style: making programs readable to others, easier to
debug
26© Tashfeen Karamat 2017
Topics
• C program structure
• Variable types and declaration [int age]
• Expressions [area= PI * radius * radius]
• Screen Output [displayTextLine]
• Conditionals [if …else]
• Loops [while loops, for loops]
• Arrays
• Functions
• Screen pixel drawing
• Sorting
• Gaussian Elimination
27© Tashfeen Karamat 2017
Programs: From Ideas to Computer ‘Bits’
28
1. High-level
description (Idea)
Make Robot
Follow Black
Line
100110101110010001010001100011
101010000001100
(Binary
Code in
memory)
(Binary Data)
3b. Assembler Program
AB95FC2E637BC
2000
(HEX
Machine
Code)?
© Tashfeen Karamat 2017
Programs: From Ideas to Action
29
2. Human Translation to
C-language Program
(Source Code)
task main( )
{
int black;
motor[1]=30;
motor[2]=30;
if ……
motor[1]=0;
……
}
1. High-level
description (Idea)
Make Robot
Follow Black
Line
c. Loader Program
100110101110010001010001100011
101010000001100
(Binary
Code in
memory)
(Binary Data)
3. C-Program Translation
to Machine Code and
Execution
a. Compiler Program
MOV $45B EAX
ADD 3, EAX
(Assembly
Language
Instructions)
b. Assembler Program
AB95FC2E637BC
2000
(HEX
Machine
Code)
© Tashfeen Karamat 2017
Generating a computer program
1. Write the RobotC source code
2. Compile the source code. The compiler
• Checks the syntax (or grammar) of the code
• Reserves parts of memory to store the variables
3. Run (execute) the program.
• The computer will perform each instruction in the program.
4. Debug make sure you have no errors and that program
achieves “idea”
30© Tashfeen Karamat 2017
Simple program
task main()
{
int x1, y1, x2, y2;
float xmid, ymid, side1, side2, distance;
x1 = 15;
x2 = 75;
y1 = 20;
y2 = 90;
// compute midpoint
xmid = (x1 + x2)/2;
ymid = (y1 + y2)/2;
// compute distance
side1 = x2 - x1;
side2 = y2 - y1;
distance = sqrt(side1*side1+side2*side2);
}
31
x
y
(x1,y1)
(x2,y2)
(xmid,ymid)
© Tashfeen Karamat 2017
Things to Notice
• the “main” program begins with task main()
• the program opens with { and closes with }
• other blocks of code (functions) are also delineated by curly
brackets { }
• there are semicolons at the end of most statements
• comments are defined by // at the beginning of the
comment or /* and */ around the comment and are
ignored by the compiler
• comments are important to tell other programmers what you have
done
32© Tashfeen Karamat 2017
Syntax errors
• if you have a syntax error in your program, it will not run
• instead the compiler will generate an error message
• for example, if you typed xmid = (x1 + x2)/2
• instead of xmid = (x1 + x2)/2;
• (i.e., you forgot the ; at the end of the statement
• you will get a PARSE ERROR
33© Tashfeen Karamat 2017
Programming style and debugging
• The computer will follow your recipe (program) blindly–even if your recipe is flawed
• Your syntax may be correct and the program will compile, but the program doesn’t do what you intended
• For example:// compute midpoint
xmid = (x1 + x2)/2;
ymid = (y1 - y2)/2;
// compute distance
side1 = x2 - x1;
side2 = y2 - y1;
distance = sqrt(side1*side1 + side2*side2);
34
you have a ‘-’ sign instead of ‘+’
The program will run, but the answer will be incorrect.
© Tashfeen Karamat 2017
Debugging
• Finding and correcting program errors is called debugging
• Debugging is harder if you don’t use good writing style and commenting • Poor programming style makes it hard for others to read your
program and figure out what it’s designed to do
• Good writing style• Use consistent indentation, spacing and placement of { }• Use comments tell others (and you!) what each segment of code is
supposed to do
• Team projects rely on team members working individually on different sections of code and require good programming style
35© Tashfeen Karamat 2017
References• Original slides provided by Prof. Evelyn Morin (Sec. 100)
• Slides have been adapted from previous course instructors: Dr. K. Rudie, Dr. A. McLeod and Dr. S. Simmons and co-instructors H. Abou-zeid, B. Nabulsi anb Brian Lynch
• QNXTBotSim was developed by Dr S. Simmons
• Web-sites
• Bombe (Turing) – the Turing machine: http://www.jproc.ca/crypto/bombe_turing.html
• IBM Blue Gene - http://www-03.ibm.com/ibm/history/ibm100/us/en/icons/bluegene/
• Flexible smart phone – Dr. R. Vertegaal:
• http://www.cs.queensu.ca/frameset/frameset.php?local=true&url=http://www.hml.queensu.ca/people/roelvertegaal
36© Tashfeen Karamat 2017
References (cont’d)
• Abacus: Weisstein, Eric W. "Abacus." From MathWorld--A
Wolfram Web Resource.
http://mathworld.wolfram.com/Abacus.html
• F1 race-car CFD: Advantage CFD
http://www.symscape.com/node/261
• Simulated chlorine spill: ERCIM Environmental Modelling
Working Group (Achim Sydow)
http://www.ercim.eu/publication/Ercim_News/enw39/sydow.htm
l
• Protein structure solution: Nature Vol. 466 Issue 7307 Letters
(Seth Cooper, et al.) (doi:10.1038/nature09304)
http://www.nature.com/nature/journal/v466/n7307/full/nature09
304.html
37© Tashfeen Karamat 2017