Você está na página 1de 44

ECE 160 – Comp Programming I

Data Structures with C


Prof. P. H. Viall Office: Research 217
Tel: (508) 999-8240

Web: http://portal.ece.umassd.edu/~pviall/ece160

Book: Computer Science: A Structured Programming Approach Using C


B. A. Forouzan and Richard F. Gilberg
Second Edition (or latest); Brooks/Cole, 2001.
Tentative Grading
• Exams (75%)
Best 3 of 4 exams (no makeups)
Exams are 1 hour in class exams
Exams MAY be open book/note
• Homework/Programs/Labs (25%)

Note – scheme is tentative – grading may


be adjusted ±2%
Working Together (programs)
• is OK - TO A POINT
• Working together appropriately means…
– Talking about a problem in general terms
– One person helping another to fix a particular error
• Working together INAPPROPRIATLY means…
– One person working, and one copying.
– Person A does program 1 and person B copies, then
person B does program 2 and A copies.
– One person using the mouse and one using the keyboard
to input a program, and then submit two copies of the
same program (with just the persons name altered).
Working Together (programs)
• If you do work together, please indicate
who you were working with
• Penalty for INAPPROPRIATLY working
together
– first offense: written warning; both on
homework and email to UMD account
– second offense: grade of homework halved
– third offense: grade of zero on homework
– further offenses will result in lowering of
course grade, possibly to 'F'
UMD Policy on Academic Dishonesty
Taken from UMD Catalog 2003-2004, page 51

• All UMass Dartmouth students are expected to maintain high standards of academic integrity and
scholarly practice. The University does not tolerate academic dishonesty of any variety, whether as a
result of a failure to understand proper academic and scholarly procedure or as an act of intentional
dishonesty.
• A student found guilty of academic dishonesty is subject to severe disciplinary action which may
include expulsion from the University. Refer to the Student Handbook and Student Judicial Code for
due process.
• A high standard of academic integrity promotes the pursuit of truth and learning and respect for the
intellectual accomplishments of others. These are values that are fundamental to the mission of this
University. Such values are undermined by academic dishonesty.
• Academic dishonesty is defined as attempting to obtain academic credit for work that is not one’s
own. Examples include:
1. copying another student’s answers on an examination;
2. obtaining, or attempting to obtain, the answers to an examination in advance;
3. submitting a paper that was written by someone else;
4. submitting a paper that includes phrases, sentences and paragraphs that were copied verbatim, or almost
verbatim, from a work written by someone else, without making this clear without indicating that these words
were someone else’s through the use of quotation marks or other appropriate citation conventions;
5. collaborating on a homework assignment when this has been expressly forbidden by the professor;
6. using unauthorized materials in completing assignments or examinations;
7. submitting the same paper for more than one class without the express permission of the instructors involved.
This list of examples should not be considered exhaustive.
UMD Policy on Academic Dishonesty
• This definition of academic dishonesty applies to information submitted in other forms besides
paper. Submitting a project of a musical or artistic nature where all or part of the project is
someone else’s work, without acknowledging this fact, constitutes academic dishonesty.
Submitting computer files that do not represent one’s own work is also considered
to be academic dishonesty; examples of computer-based academic dishonesty
would include submitting a computer program or text file created by someone else
as one’s own, or submitting the output of a computer program written by someone
else, and claiming to have written the program that generated the output.
• For all forms of academic dishonesty, students who knowingly allow other students to use their
work are themselves considered to be academically dishonest. Examples would include students
who knowingly allow other students to copy their exam answers, and students who give papers that
they have written to other students so that the other students can submit them for credit.
• A faculty member is appointed by the Faculty Senate to act as an Academic Ethical Matters
Facilitator. This individual will offer advice to both students and faculty about the issues involved
in penalizing academic dishonesty, and the process of appealing such penalties.
• Penalties assessed by faculty members for academic dishonesty generally consist of a reprimand, a
requirement to resubmit the work in a more acceptable form, a lowering of a grade, failure in the
course in which the alleged infraction took place, or a combination of these.
• Instead of (or in addition to) assessing such penalties, a faculty member may refer the matter to the
UMass Dartmouth Student Judiciary. Decisions made by the Student Judiciary may be appealed to
the University Appellate Board.
• A form acknowledging that each student has read, understands, and will abide by these standards
will be signed by the student and placed into the student’s permanent file.
Working Together (exams)
• Exams are definitely an individual endeavor.
• You may not work together during exams.
• You may not share any materials during an
exam.
• In general the penalty for cheating on an exam
or otherwise covertly attempting to raise your
grade on an exam shall be an ‘F’ for the course.
Canceled Classes and Tests
• If class is cancelled the day an exam is
scheduled, we will have the exam the next
time the class meets
• If class is cancelled the session prior to the
exam (the day you have to ask questions),
then the next class meeting will be the
“question time”, and the class meeting after
that will be the exam
Course Objectives
1. To learn how a computer stores data, and conversion between
various number bases
2. To learn the fundamentals of using the c compiler and c
preprocessor
3. To learn how to use a modern environment to create, compile,
execute and debug C programs
4. To learn how to use procedures to modularize a program, and
how to pass parameters by value and by address
5. To learn the syntax of the c language, including arrays,
structures, dynamic allocation, and file access
6. To learn how to use and manipulate strings using the C language
7. To learn to use system libraries within a program
8. To learn how to program, and how to design well written,
maintainable programs
Base Conversions
Convert 123|10 to Base 10 (gotta start someplace)

1 2 3 |10 = ? |10
| | |
| | `--- 3 x 100 = 3 x 1 = 3
| `----- 2 x 101 = 2 x 10 = 20
`------- 1 x 102 = 1 x 100 = 100
123
Therefore, 123|10 = 123|10
Base Conversions
Convert 123|8 to Base 10

1 2 3 |8 = ? |10
| | |
| | `--- 3 x 80 = 3 x 1 = 3
| `----- 2 x 81 = 2 x 8 = 16
`------- 1 x 82 = 1 x 64 = 64
83
Therefore, 123|8 = 83|10
Base Conversions
Convert 2BAD|16 to Base 10

2 B A D |16 = ? |10
| | | |
| | | `--- D x 160 = 13 x 1 = 13
| | `----- A x 161 = 10 x 16 = 160
| `------- B x 162 = 11 x 256 = 2816
`--------- 2 x 163 = 2 x 4096 = 8192

11181
Therefore, 2BAD|16 = 11181|10
Base Conversions
Convert 20DD|16 to Base 10

??
Base Conversions
Convert 20DD|16 to Base 10

2 0 D D |16 = ? |10
| | | |
| | | `--- D x 160 = 13 x 1 = 13
| | `----- D x 161 = 13 x 16 = 208
| `------- 0 x 162 = 0 x 256 = 0
`--------- 2 x 163 = 2 x 4096 = 8192

8413
Therefore, 20DD|16 = 8413|10
Base Conversions
Convert FACE|16 to Base 10

??
Base Conversions
Convert FACE|16 to Base 10

F A C E |16 = ? |10
| | | |
| | | `--- E x 160 = 14 x 1 = 14
| | `----- C x 161 = 12 x 16 = 192
| `------- A x 162 = 10 x 256 = 2560
`--------- F x 163 = 15 x 4096 = 61440

64206
Therefore, FACE|16 = 64206|10
Base Conversions
Convert 123|10 to Base 8
Base you are converting to
8 )123
8 )15 R 3
8 )1 R 7 Read Up!
0 R 1

Therefore, 123|10 = 173|8


Base Conversions
Convert 64206|10 to Base 16
Base you are converting to
16 )64206
16 )4012 R 14 (E)
16 )250 R 12 (C)
16 )15 R 10 (A) Read Up!
16 )0 R 15 (F)

Therefore, 64206|10 = FACE|16


Base Conversions
123|7 = ?|9 (Can’t do directly)

Step 1: 123|7=?|10 Step 2: 66|10 =?|9


1 2 3 9 )66
| | |
9 ) 7 R 3
| | `-3x70= 3
0 R 7
| `--2x71=14
∴ 66|10 =73|9
`-----1x72=49
66
∴ 123|7=66|10
∴ 123|7=73|9
Base Conversions
Dec Bin Base 4 Octal Hex
• Converting between 0 0000 00 00 0
bases which are 1 0001 01 01 1
2 0010 02 02 2
powers of 2 (base 2, 3 0011 03 03 3
4, 8, and 16) 4 0100 10 04 4
5 0101 11 05 5
• Each Digit in base 4, 6 0110 12 06 6
8 or 16 represents 2, 7 0111 13 07 7
8 1000 20 10 8
3 or 4 bits 9 1001 21 11 9
10 1010 22 12 A
11 1011 23 13 B
12 1100 30 14 C
13 1101 31 15 D
14 1110 32 16 E
15 1111 33 17 F
Base Conversions
Dec Bin Bs 4 Oct Hex
0 0000 00 00 0
10101110001101|2=?|8 1 0001 01 01 1
2 0010 02 02 2
Work from right to left 3 0011 03 03 3
Divide into 3 bit groups 4 0100 10 04 4
5 0101 11 05 5
6 0110 12 06 6
#10 101 110 001 101 7 0111 13 07 7
8 1000 20 10 8
9 1001 21 11 9

2 5 6 1 5 10
11
1010
1011
22
23
12
13
A
B
12 1100 30 14 C

∴ 10101110001101|2=25615|8 13 1101 31 15 D
14 1110 32 16 E
NOTE: # is a place holder for zero 15 1111 33 17 F
Base Conversions
Dec Bin Bs 4 Oct Hex
0 0000 00 00 0
10101110001101|2=?|16 1 0001 01 01 1
2 0010 02 02 2
Work from right to left 3 0011 03 03 3
Divide into 4 bit groups 4 0100 10 04 4
5 0101 11 05 5
6 0110 12 06 6
##10 1011 1000 1101 7 0111 13 07 7
8 1000 20 10 8
9 1001 21 11 9

2 B 8 D 10
11
1010
1011
22
23
12
13
A
B
12 1100 30 14 C

∴ 10101110001101|2=2B8D|16 13 1101 31 15 D
14 1110 32 16 E
NOTE: # is a place holder for zero 15 1111 33 17 F
Base Conversions
Dec Bin Bs 4 Oct Hex
0 0000 00 00 0
1357|8=?|2 1 0001 01 01 1
2 0010 02 02 2
1 3 5 7 3 0011 03 03 3
4 0100 10 04 4
5 0101 11 05 5
6 0110 12 06 6
001 011 101 111 7 0111 13 07 7
8 1000 20 10 8
9 1001 21 11 9
∴ 1357|8=001011101111|2 10 1010 22 12 A
11 1011 23 13 B
12 1100 30 14 C
Note: one need not
13 1101 31 15 D
write the leading zeros 14 1110 32 16 E
15 1111 33 17 F
Base Conversions
Dec Bin Bs 4 Oct Hex
0 0000 00 00 0
FACE|16=?|2 1 0001 01 01 1
2 0010 02 02 2
F A C E 3 0011 03 03 3
4 0100 10 04 4
5 0101 11 05 5
6 0110 12 06 6
1111 1010 1100 1110 7 0111 13 07 7
8 1000 20 10 8
9 1001 21 11 9
∴ FACE|16=1111101011001110|2 10 1010 22 12 A
11 1011 23 13 B
12 1100 30 14 C
13 1101 31 15 D
14 1110 32 16 E
15 1111 33 17 F
Base Conversions
Dec Bin Bs 4 Oct Hex
A1CE|16 =?|8 0 0000 00 00 0
1 0001 01 01 1

2 1 C E 2
3
0010
0011
02
03
02
03
2
3

1010000111001110 4
5
0100
0101
10
11
04
05
4
5
6 0110 12 06 6
7 0111 13 07 7

1 2 0 7 1 6 8
9
1000
1001
20
21
10
11
8
9

∴ A1CE|16 =120716|8
10 1010 22 12 A
11 1011 23 13 B
12 1100 30 14 C
13 1101 31 15 D
14 1110 32 16 E
15 1111 33 17 F
Terminology
• Bit – a 0 or a 1
• Nibble – 4 bits
• Byte – 8 bits
• Word – Machine dependent
(8086/88 = 16 bits; 386/486/Pent = 32 bits)
• Doubleword – Machine dependent
(8086/88 = 32 bits; 386/486/Pent = 64 bits)
Terminology
• K – Kilo; 210 = 1,024
• M – Mega; 220 = 1,048,576
• G – Giga; 230 = 1,073,741,824
• T – Tera; 240 = 1,099,511,627,776
• P – Penta; 250 = 1,125,899,906,842,624
• E – Exa; 260 = 1,152,921,504,606,846,976
Rough Cost for RAM/Disk
R A M
1956 1978 1985 2000 2004 2005
K $19.00 $ 0.16

M $19000. $160.00 $ .50 $ .20

G $500.00 $200.00 80.00

D i s k
K
M $9999.99 $250 $7.00

G $7000.00 $10.00 $ .83 .10

T $9999.99 $830.00 100.00

See http://www.littletechshoppe.com/ns1625/winchest.html for more info


ASCII-American Standard Code
for Information Interchange
• Used to represent characters
• Seven bit code used to represent character
• ‘0’ through ‘9’ are characters
• IBM used 8 bits in PC and added 128
additional characters (line drawing, Greek)
• Problem with ASCII – the A
• Unicode addresses this problem
0 00 C-@ 32 20 64 40 @ 96 60 `
1 01 C-A 33 21 ! 65 41 A 97 61 a

ASCII-points to note 2
3
4
02
03
04
C-B
C-C
C-D
34
35
36
22
23
24
"
#
$
66
67
68
42 B
43 C
44 D
98
99
100
62 b
63 c
64 d
5 05 C-E 37 25 % 69 45 E 101 65 e
•Values 30-39=Chars ‘0’-’9’ 6 06 C-F 38 26 & 70 46 F 102 66 f
7 07 C-G 39 27 ' 71 47 G 103 67 g
8 08 C-H 40 28 ( 72 48 H 104 68 h
•‘a’ = ‘A’ + 32 (Bit 5) 9 09 C-I 41 29 ) 73 49 I 105 69 i
10 0A C-J 42 2A * 74 4A J 106 6A j
•‘A’ = Ctrl/A+64 (bit 6) 11
12
0B
0C
C-K
C-L
43
44
2B
2C
+
,
75
76
4B K
4C L
107
108
6B k
6C l
13 0D C-M 45 2D - 77 4D M 109 6D m
•‘A’ < ‘Z’ < ‘a’ < ‘z’ 14
15
0E
0F
C-N
C-O
46
47
2E
2F
.
/
78
79
4E N
4F O
110
111
6E n
6F o
16 10 C-P 48 30 0 80 50 P 112 70 p
•If character is represented in a 17 11 C-Q 49 31 1 81 51 Q 113 71 q
18 12 C-R 50 32 2 82 52 R 114 72 r
byte, bit 7 is usually 0 19 13 C-S 51 33 3 83 53 S 115 73 s
20 14 C-T 52 34 4 84 54 T 116 74 t
21 15 C-U 53 35 5 85 55 U 117 75 u
•Chars 0-31d are not printable, 22 16 C-V 54 36 6 86 56 V 118 76 v
23 17 C-W 55 37 7 87 57 W 119 77 w
but may influence how other 24 18 C-X 56 38 8 88 58 X 120 78 x
25 19 C-Y 57 39 9 89 59 Y 121 79 y
chars are printed. 26 1A C-Z 58 3A : 90 5A Z 122 7A z
27 1B ESC 59 3B ; 91 5B [ 123 7B {
28 1C 60 3C < 92 5C \ 124 7C |
29 1D 61 3D = 93 5D ] 125 7D }
30 1E 62 3E > 94 5E ^ 126 7E ~
31 1F 63 3F ? 95 5F _ 127 7F DEL
upper 3 bits

111
110
101
100
011
010
001
000
00000
00001
00010
00011
00100
00101
00110
00111
01000
01001
01010
01011
01100
01101
01110
01111
10000
lower 5 bits

10001
10010
10011
10100
10101
10110
10111
11000
11001
11010
11011
11100
11101
11110
11111
Binary Addition
0+0=0
0+1=1
1+0=1
1 + 1 = 0 (with carry of 1)
1011011 + 10110 = ?

1 0 1 1 0 1 1
+ 1 0 1 1 0
----------------
1 1 1 0 0 0 1
100111001 + 11011 = ?
100111001 + 11011 = ?

1 0 0 1 1 1 0 0 1
+ 1 1 0 1 1
-----------------
100111001 + 11011 = ?

1 0 0 1 1 1 0 0 1
+ 1 1 0 1 1
-----------------
1 0 1 0 1 0 1 0 0
Hex Addition
• 8+4=C
4BA978 • 7+8=F
+ D2B84 • 9+B=4 w/ carry
58D4FC • 1+A+2=D
• B+D=8 w/ carry
• 1+4+0=5
Hex Addition Table
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10
00 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10
01 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11
02 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12
03 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13
04 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14
05 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15
06 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16
07 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17
08 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18
09 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19
0A 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A
0B 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B
0C 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C
0D 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D
0E 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E
0F 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
10 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20
Positive/Negative Numbers
1100 1110 0000 0010 0100
1011 1101 1111 0001 0011 0101
-5 -4 -3 -2 -1 0 1 2 3 4 5

Notes
• We are in “4-bit” mode
• Think odometer math (-1=1111, -2=1110,
-3=1101, -4=1100, -5=1011, -6=1010, etc.)
• At some point a big positive number+1
becomes a big negative number
• Really a number “circle”
4-bit Number Circle
0000
1111 0001
1110 -1 0
1 0010
-2 2
1101
-3 0011
3

1100 -4 4 0100

-5 5
1011 0101
-6 6
-7 7 0110
1010 -8
1001 0111
1000
Observations (Part 1)
0110 6 0100 4 0110 6
+ 0011 + 3 0010 + 2 1101 +-3
1001 9 0110 6 10011 3
  

1010 -6 1100 -4 1010 -6


1101 +-3 1110 +-2 0011 + 3
10001 9 11010 -6 1101 -3
  
Observations (Part 2)
0110 6 0100 4 0110 6
+ 0011 + 3 0010 + 2 1101 +-3
1001 9 0110 6 10011 3
  

1010 -6 1100 -4 1010 -6


1101 +-3 1110 +-2 0011 + 3
10001 9 11010 -6 1101 -3
  
Observations (Part 3) & Conclusions
•Note when a carry into the sign bit occurs
•Note when a carry out of the sign bit occurs
•If one occurs and not the other – WRONG
•If both occur – RIGHT
•If neither occur – RIGHT
•Therefore:
OF = carry into sign bit carry out of sign bit
Organization of a computer
Address Bus

CPU RAM ROM Printer Disk CRT Kbd

Data Bus
Control Bus

Você também pode gostar