Click here to load reader

APS105 - Electrical & Computer Engineering

  • View
    0

  • Download
    0

Embed Size (px)

Text of APS105 - Electrical & Computer Engineering

Slide 1– BASc/MASc, ECE, UofToronto, 1995/1997
• Contact:
[email protected]
3
– Learn to command them to do your bidding!
• For love of design
– “only slightly removed from pure thought-stuff”
• Entrepreneurial opportunities
• Many engineering jobs involve programming
– Simulations, databases, websites
– Java, Python, Ruby, javascript
• Easier to understand what is happening
• Important building-block for later computing courses
– C/C++ still very common
6
– Different than simply seeing the solution
• Learn “why”, not just “how”
– how course material fits in with “big picture”
• Will provide proper emphasis and pacing
– Highlight key material
– Overview-only sundry material
– Eg., order that you need it for labs
• Ideally more entertaining than reading manual
– Else Prof-fail.
• Midterm: 25%
11
– Form for regrade request for misgraded midterm
• Discussion Board
12
• First lab (lab1) available asap
– Can submit to test, but not graded
– But do submit to test/practice!!
• Must run properly on ECF systems
– Even if it runs fine elsewhere
• Must output exactly as prescribed
– Close isn’t good enough
• Marks for functionality and style
– Proper style will be defined throughout term
13
– Start at 10mins after the hour
• Tutorial Periods:
– 4 tutorials have quizzes during last 20mins (see schedule)
– Attendance optional, unless there is a quiz
• Weekly Optional Lecture:
– attendance recommended for non-experts
• Lab Periods:
– Attendance "mandatory" 14
Using C”, 2nd Edition
– UofT bookstore
– Amazon etc.
• Pptx/pdf available on www prior to lecture
– Please bring to class (printed, on tablet,etc)
• The filled-in “blanks” are only during class
– Filled-in slides will not be published
– I will try to publish med-large code examples
• The symbol that indicates a blank to fill
16
Cheating/Copying
– “cheater-beater” software
• You only cheat yourself
– Labs worth 1.6% each
– The consequences for being caught are harsh
• See Engineering Calendar for details
17
– Collaborating vs cheating
• Get help quickly if you get into trouble
– First-year office (1st floor Galbraith Building)
– www.calss.utoronto.ca
• Quiet please!
• Pay attention
• Ask questions and point out errors!
– Raise your hand
19
• Attend lectures
– Avoid last-minute cramming
• Prepare for midterm/final
– Practice by filling lecture slides on your own
• Both available on www 20
How to Succeed at Skule (computing)
• Learn to keyboard properly!
• has details on how to log in from home
• Consider learning some javascript
– Has C-like syntax in many aspects
• Write a simple app for your own use
– Eg., for your laptop or iphone
21
C (and others) APS105
Programmer Machine Instructions
Memory
(RAM)
CPU
– control circuitry
– by Dennis Ritchie
int main (void)
Comments
Main should return 0 if program finished normally
‘\n’ means carriage return
int main (void){printf("Hello, world\n");return 0;}
Note: this works too; whitespace mostly doesn’t matter in C.
but it is very ugly and hard to read
and doesn’t execute any faster than the nicer formatting
so don’t write programs this way!
GCC
• gcc
– Means “GNU C compiler”
– A program that converts C to 0’s & 1’s • Stored in a file called an “executable”
• Type this command to compile hello.c gcc hello.c
• Generates the executable file a.out
• Insteady you could type: gcc –o hello hello.c
• Generates the executable file hello
Value Representation
– A bit can be 0 or 1
• Why do computers use bits?
– Composed of transistors, which are switches,
either on/off, hence zero/one
– 1 byte can be written as 1B
RECALL: BASE 10 (decimal) • 956
= 9 * 10^2 + 5 * 10^1 + 6 * 10^0
= 9*100 + 5 * 10 + 6 * 1
= 956
• NOTE: not all languages/cultures use base10 – Ndom, Frederik Hendrik Island: base6
– nimbia: base12
– Tzotzil, Mexico: base20
• Converting from binary to decimal:
• 0b1011
= 1*2^3 + 0 *2^2 + 1 *2^1 + 1 *2^0
= 1*8 + 0*4 + 1*2 + 1*1
= 8 + 2 + 1
represent with N bits?
• NOTE:
Byte Size
– 28 =256 possible values
– Example: 0,1,2,3..255 = 0.. 28-1
• floating point value
Types
39
Types
– Without a type they are just bits!
• C has types:
– User-defined types: name your own! (later)
• Basic data types:
– Example: a number or a character
40
– We will almost always use int in this class
• Other integer types:
– short: at least 16bits
– long: at least 32bits
• the actual size can depend on the system!!!!!
41
– Eg., 1.3333333333333….
• Floating-point: a common approximation
• 1234e-3
• Eg., 2.1313*1020 or 2.1313*10-20
– We don’t need to know the details for this class
42
• We will almost always use double in this class
• float: at least 32 bits
43
• char: 8 bits, typically ASCII-encoded
• Note1: char holds only a single character
– How can we represent a “string” of characters?
• Note2: recall “escapes”
44
– The old way: use int values 0 and 1
– The new way: use the bool type
• bool type
– Note: you have to include the bool library
• in program (at top): #include <stdbool.h>
45
– Choosing good names is a key skill!
• Rules: identifiers can be
– upper & lower case letters, digits, ‘_’
– First character cannot be a digit
– Cannot match a C “reserved” word (like “int”)
47
– myExampleIdentifier
– my_example_identifier
Choosing Identifiers: Good Style
– Too short: eg., “x”:
• Leads to hard-to-understand code
– Too long: eg., “thisIsMyExampleAPS105Identifier”
• World’s worst identifier:
– data2
Declaring Variables • Variable: storage of a value of a certain type
• Variable declaration:
– and you would like it to be a certain type
– Note: the C-compiler creates storage for it
• General form:
double length, width; // sides of a rectangle
char term; // ‘F’ means fall, ‘S’ means spring
• Question: what value does height have?
– garbage: we did not “initialize” it to a certain value
– compiler should warn if use it without initializing
Location Value
• Example assignment at declaration:
bool weLoveAPS105 = true;
int height; // in centimeters
Motherboard
1) Each char is sent to the OS
2) OS converts char into a pixelmap
3) Pixelmap is sent to graphics card
4) Graphics card adds pixelmap to the frame buffer
5) Frame buffer sent to monitor (eg., VGA encoded)
Use printf()
Printing Values
printf(“hi mom\n”);
• printf can also print variable values
int age = 39;
// prints: “I am 39 years old\n”
printf(“I am %d years old\n”,age);
conversion specification variable to print
Printing Integers
• decimal (base10)
– %4d: print the decimal using at least 4 spaces
• Example:
// prints: “I’m 39\n”
Printing chars
• Example:
// prints “A is for apple”
Printing doubles
– by default prints 6 digits right of the decimal
– cheat: can use %f instead, does the same thing
• %.2lf
• %6.2lf
• %e
58
– Just use %d (decimal)
• Example:
– Answer: use ‘%%’
// prints: “My grade is 82.5%\n”
• How do you print ‘\’?
– Answer: use ‘\\’
1) Program “sleeps” awaiting a keypress
2) Keypress sends key value to CPU via USB
3) USB port “interrupts” CPU
4) OS is invoked on CPU, handles interrupt
5) CPU copies value from OS (recoding it?)
6) Sleeping program is eventually awoken
Use scanf()
• the scanf function inputs from the keyboard
• example1:
• example2:
control string says:
input an int
the reason why we need this is complex---more later
Inputting Multiple Values
– works for this input:
22
33
44
– eg., won’t work for input: 22.0 33.0 44.0
Consuming Whitespace
• The next scanf call would start at this point:
– “ 22 33 44”
– but does not consume “trailing” whitespace
Skipping Characters
– eg., non-whitespace
• Example:
Location Value
courseCode 105
• Note: %c does not skip whitespace!
• Example input: 2011F
• Example2 input: 2011 F
gives year=2011, term=‘ ’
Constants
69
Constants
– possibly used in multiple places in your program
• Literal constant:
– example: int age = 2;
– then use the identifier in your code
– Named constants make for easier-to-read code!
70
• With named constant:
“magic number”;
bad style!!
More readable;
• Using the const attribute
of PI is not allowed to change
72
• avoid magic numbers in your code
– numbers which have no meaning to others
– use of magic numbers will cost you points in labs!
• avoid multiple copies of the same value
– if you must update the value, only have to do it in
one place in the code
• Convention/Good-Style:
– eg., PI and MAX_ATTEMPTS