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: copying another student’s answers on an examination; obtaining, or attempting to obtain, the answers to an examination in advance; submitting a paper that was written by someone else; 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. 1. 2. 3. 4. 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 | `----- 2 x 81 = 2 x `------- 1 x 82 = 1 x 1 = 8 = 64 = Therefore, 123|8 = 83|10 3 16 64 83 Base Conversions Convert 2BAD|16 to Base 10 2 B A D |16 | | | | | | | `--| | `----| `------`--------- = ? |10 D A B 2 x x x x 160 161 162 163 = 13 x 1 = 10 x 16 = 11 x 256 = 2 x 4096 Therefore, 2BAD|16 = 11181|10 = = = = 13 160 2816 8192 11181 Base Conversions Convert 20DD|16 to Base 10 ?? Base Conversions Convert 20DD|16 to Base 10 2 0 D D |16 | | | | | | | `--| | `----| `------`--------- = ? |10 D D 0 2 x x x x 160 161 162 163 = 13 x 1 = 13 x 16 = 0 x 256 = 2 x 4096 Therefore, 20DD|16 = 8413|10 = = = = 13 208 0 8192 8413 Base Conversions Convert FACE|16 to Base 10 ?? Base Conversions Convert FACE|16 to Base 10 F A C E |16 | | | | | | | `--| | `----| `------`--------- = ? |10 E C A F x x x x 160 161 162 163 = = = = 14 12 10 15 x 1 x 16 x 256 x 4096 Therefore, FACE|16 = 64206|10 = 14 = 192 = 2560 = 61440 64206 Base Conversions Convert 123|10 to Base 8 Base you are converting to 8 )123 8 )15 R 3 8 )1 R 7 0 R 1 Read Up! Therefore, 123|10 = 173|8 Base Conversions Convert 64206|10 to Base 16 Base you are converting to 16 )64206 16 )4012 16 )250 16 )15 16 )0 R R R R 14 12 10 15 (E) (C) (A) (F) Therefore, 64206|10 = FACE|16 Read Up! Base Conversions 123|7 = ?|9 (Can’t do directly) Step 1: 123|7=?|10 1 2 3 | | | | | `-3x70= 3 | `--2x71=14 `-----1x72=49 66 \ 123|7=66|10 Step 2: 66|10=?|9 9 )66 9 ) 7 R 3 0 R 7 \ 66|10=73|9 \ 123|7=73|9 Base Conversions • Converting between bases which are powers of 2 (base 2, 4, 8, and 16) • Each Digit in base 4, 8 or 16 represents 2, 3 or 4 bits Dec Bin Base 4 Octal Hex 0 0000 00 00 0 1 0001 01 01 1 2 0010 02 02 2 3 0011 03 03 3 4 0100 10 04 4 5 0101 11 05 5 6 0110 12 06 6 7 0111 13 07 7 8 1000 20 10 8 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 1 0001 01 01 1 2 0010 02 02 2 3 0011 03 03 3 4 0100 10 04 4 5 0101 11 05 5 6 0110 12 06 6 7 0111 13 07 7 8 1000 20 10 8 9 1001 21 11 9 10 1010 22 12 A 11 1011 23 13 B \ 10101110001101|2=25615|8 12 1100 30 14 C 13 1101 31 15 D NOTE: # is a place holder for zero 14 1110 32 16 E 15 1111 33 17 F 10101110001101|2=?|8 Work from right to left Divide into 3 bit groups #10 101 110 001 101 2 5 6 1 5 Base Conversions Dec Bin Bs 4 Oct Hex 0 0000 00 00 0 1 0001 01 01 1 2 0010 02 02 2 3 0011 03 03 3 4 0100 10 04 4 5 0101 11 05 5 6 0110 12 06 6 7 0111 13 07 7 8 1000 20 10 8 9 1001 21 11 9 10 1010 22 12 A 11 1011 23 13 B \ 10101110001101|2=2B8D|16 12 1100 30 14 C 13 1101 31 15 D NOTE: # is a place holder for zero 14 1110 32 16 E 15 1111 33 17 F 10101110001101|2=?|16 Work from right to left Divide into 4 bit groups ##10 1011 1000 1101 2 B 8 D Base Conversions 1357|8=?|2 1 3 5 7 001 011 101 111 \ 1357|8=001011101111|2 Note: one need not write the leading zeros Dec Bin Bs 4 Oct Hex 0 0000 00 00 0 1 0001 01 01 1 2 0010 02 02 2 3 0011 03 03 3 4 0100 10 04 4 5 0101 11 05 5 6 0110 12 06 6 7 0111 13 07 7 8 1000 20 10 8 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 FACE|16=?|2 F A C E 1111 1010 1100 1110 \ FACE|16=1111101011001110|2 Dec Bin Bs 4 Oct Hex 0 0000 00 00 0 1 0001 01 01 1 2 0010 02 02 2 3 0011 03 03 3 4 0100 10 04 4 5 0101 11 05 5 6 0110 12 06 6 7 0111 13 07 7 8 1000 20 10 8 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 A1CE|16=?|8 2 1 C E 1010000111001110 1 2 0 7 1 6 \A1CE|16=120716|8 Dec Bin Bs 4 Oct Hex 0 0000 00 00 0 1 0001 01 01 1 2 0010 02 02 2 3 0011 03 03 3 4 0100 10 04 4 5 0101 11 05 5 6 0110 12 06 6 7 0111 13 07 7 8 1000 20 10 8 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 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; M – Mega; G – Giga; T – Tera; P – Penta; E – Exa; 210 = 220 = 230 = 240 = 250 = 260 = 1,024 1,048,576 1,073,741,824 1,099,511,627,776 1,125,899,906,842,624 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 G $ .50 $ .20 $500.00 $200.00 80.00 $10.00 $ .83 .10 $9999.99 $830.00 100.00 T D i s $250 $7.00 k K M G T $9999.99 $7000.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 ASCII-points to note •Values 30-39=Chars ‘0’-’9’ •‘a’ = ‘A’ + 32 (Bit 5) •‘A’ = Ctrl/A+64 (bit 6) •‘A’ < ‘Z’ < ‘a’ < ‘z’ •If character is represented in a byte, bit 7 is usually 0 •Chars 0-31d are not printable, but may influence how other chars are printed. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F C-@ C-A C-B C-C C-D C-E C-F C-G C-H C-I C-J C-K C-L C-M C-N C-O C-P C-Q C-R C-S C-T C-U C-V C-W C-X C-Y C-Z ESC 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F ! " # $ % & ' ( ) * + , . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G 48 H 49 I 4A J 4B K 4C L 4D M 4E N 4F O 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W 58 X 59 Y 5A Z 5B [ 5C \ 5D ] 5E ^ 5F _ 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 60 ` 61 a 62 b 63 c 64 d 65 e 66 f 67 g 68 h 69 i 6A j 6B k 6C l 6D m 6E n 6F o 70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w 78 x 79 y 7A z 7B { 7C | 7D } 7E ~ 7F DEL upper 3 bits 00000 00001 00010 00011 00100 00101 00110 00111 01000 01001 01010 01011 01100 01101 01110 01111 10000 10001 10010 10011 10100 10101 10110 10111 11000 11001 11010 11011 11100 11101 11110 11111 lower 5 bits 000 001 010 011 100 101 110 111 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 4BA978 + D2B84 58D4FC • 8+4=C • 7+8=F • 9+B=4 w/ carry • 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 1011 -5 1110 1101 -4 -3 0000 1111 -2 -1 0010 0001 0 1 0100 0011 2 3 0101 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 1110 -1 0 0001 1 -2 1101 1100 2 0010 -3 -4 4 -5 1011 0100 5 0101 -6 1010 0011 3 6 -7 1001 -8 1000 7 0110 0111 Observations (Part 1) 0110 6 + 0011 + 3 1001 9 0100 4 0010 + 2 0110 6 0110 6 1101 +-3 10011 3 1010 -6 1101 +-3 10001 9 1100 -4 1110 +-2 11010 -6 1010 -6 0011 + 3 1101 -3 Observations (Part 2) 0110 6 + 0011 + 3 1001 9 0100 4 0010 + 2 0110 6 0110 6 1101 +-3 10011 3 1010 -6 1101 +-3 10001 9 1100 -4 1110 +-2 11010 -6 1010 -6 0011 + 3 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 Data Bus Control Bus CRT Kbd