Last Class We Covered File I/O How to open a file

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 01 / 20 / 2009 Instructor: Michael Eckmann.
Advertisements

Unit 6 File processing Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
REPRESENTING INFORMATION: BINARY, HEX, ASCII C ORRESPONDING R EADING : UDC C HAPTER 2 CMSC 150: Lecture 2.
Computer Systems 1 Fundamentals of Computing
Lecture for Week Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday.
IT-101 Section 001 Lecture #3 Introduction to Information Technology.
Data Representation S2. This unit covers how the computer represents- Numbers Text Graphics Control.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
CMSC 104, Lecture 051 Binary / Hex Binary and Hex The number systems of Computer Science.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Unit 6 File processing Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CDA 3100 Spring Special Thanks Thanks to Dr. Xiuwen Liu for letting me use his class slides and other materials as a base for this course.
Week 7 : String and photo processing. Today’s Tasks  Practice list and string  Convert Decimal to any radix base number  Between Binary and Hexadecimal.
Number Systems. ASCII – American Standard Code for Information Interchange – Standard encoding scheme used to represent characters in binary format on.
CMSC201 Computer Science I for Majors Lecture 23 – Algorithms and Analysis Prof. Katherine Gibson Based on slides from previous iterations.
All materials copyright UMBC unless otherwise noted CMSC201 Computer Science I for Majors Lecture 02 – Algorithmic Thinking.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion (Continued) Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from UPenn’s.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Compsci 210 Tutorial Two CompSci Semester Two 2016.
Math operations 9/19/16.
CMSC201 Computer Science I for Majors Lecture 10 – File I/O
Week 2 - Wednesday CS 121.
CMSC201 Computer Science I for Majors Lecture 02 – Algorithmic Thinking Prof. Katherine Gibson Based on slides by Shawn Lupoli and Max Morawski at UMBC.
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
Binary, Denary, Hexadecimal Conversion Binary Addition
CMSC201 Computer Science I for Majors Lecture 02 – Intro to Python
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
CSC 131: Introduction to Computer Science
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
The Binary Number System
…to GCSE Level with Python Sue Sentance
Tools of Web Development 1: Module A: Numbering Systems
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Last Class We Covered ASCII values Short circuit evaluation Project 3
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
Each column represents another power of the base
Binary Files.
CMSC201 Computer Science I for Majors Lecture 20 – Dictionaries
while loops; file I/O; introduction to lists
CDA 3100 Summer 2013.
CS101 Learning Binary Learning Binary.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Numbers and their bases
Fundamentals of Data Representation
Last Class We Covered ASCII values Short circuit evaluation Project 3
CMSC201 Computer Science I for Majors Lecture 17 – Recursion (cont)
CMSC201 Computer Science I for Majors Lecture 25 – Final Exam Review
Reading and Writing Files
CMSC201 Computer Science I for Majors Lecture 18 – String Formatting
Starter Using the fingers on only one hand, what is the highest number you can count to? Rules: You must start at 1 You must count sequentially (i.e.
CMSC201 Computer Science I for Majors Lecture 13 – File I/O
Word of the Week November 2
Last Class We Covered File I/O How to open a file
CMSC201 Computer Science I for Majors Final Exam Information
Summary of what we learned yesterday
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
EECE.2160 ECE Application Programming
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Last Class We Covered Recursion Stacks Parts of a recursive function:
UNIT – 3 & 4. Data Representation and Internal
CMSC201 Computer Science I for Majors Lecture 12 – Midterm Review
Last Class We Covered Dictionaries Hashing Dictionaries vs Lists
Week 1 - Friday COMP 1600.
Dr. Clincy Professor of CS
Presentation transcript:

CMSC201 Computer Science I for Majors Lecture 19 – Binary (and File I/O)

Last Class We Covered File I/O How to open a file For reading or writing How to read from a file How to write to a file How to close a file

Any Questions from Last Time?

Today’s Objectives To understand how data is represented and stored in memory Binary numbers Converting Binary to Decimal Decimal to Binary To review what we learned about file I/O

“Bridge Course” We are offering CMSC 291 again this winter Course for CMSC/CMPE students who earn a “C” instead of the “B” required for their major Grade must be result of a single poor score (e.g., did badly on the midterm or a project) Won’t change CMSC 201 grade, but will allow eligible students to take CMSC 202 in Spring 18 An announcement will be made on Blackboard containing all of the details later this week

Binary Numbers

Binary Numbers Computers store all information (code, text, images, sound,) as a binary representation “Binary” means only two parts: 0 and 1 Specific formats for each file help the computer know what type of item/object it is But why use binary?

Decimal vs Binary Why do we use decimal numbers? Ones, tens, hundreds, thousands, etc. But computers don’t have fingers… What do they have instead? They only have two states: “on” and “off”

Decimal Example How do we represent a number like 50,932? 2 x 100 = 2 3 x 101 = 30 9 x 102 = 900 0 x 103 = 0000 5 x 104 = 50000 ------ Total: 50932 ten thousands thousands hundreds tens ones 5 9 3 2 104 103 102 101 100 Decimal uses 10 digits, so…

Another Decimal Example 6 7 4 9 3 104 103 102 101 100 10000 1000 10 1 60000 7000 400 90 60000+7000+400+90+3 = 67493

Binary Example Let’s do the same with 10110 in binary 0 x 20 = 0 1 x 21 = 2 1 x 22 = 4 0 x 23 = 0 1 x 24 = 16 -- Total: 22 sixteens eights fours twos ones 1 1 1 24 23 22 21 20 Binary uses 2 digits, so our base isn’t 10, but…

Binary to Decimal Conversion Step 1: Draw Conversion Box Step 2: Enter Binary Number Step 3: Multiply Step 4: Add 1 29 28 27 26 25 24 23 22 21 20 512 256 128 64 32 16 8 4 2 1 512 128 8 4 1 128 + 0 + 0 + 0 + 8 + 4 + 0 + 1 = 141

Exercise: Converting From Binary What are the decimals equivalents of… 101 1111 100000 101010 0010 1010 1000 0000 Longer binary numbers are often broken into blocks of four digits for the sake of readability

Exercise: Converting From Binary What are the decimals equivalents of… 101 = 4+0+1 = 5 1111 = 8+4+2+1 = 15 100000 = 32+0+0+0+0+0 = 32 101010 = 32+0+8+0+2+0 = 42 0010 1010 = 32+0+8+0+2+0 = 42 1000 0000 = 128+...+0+0 = 128

Decimal to Binary Conversion Step 1: Draw Conversion Box Step 2: Compare decimal to highest binary value Step 3: If binary value is smaller, put a 1 there and subtract the value from the decimal number Step 4: Repeat until 0 Convert 163 to binary 29 28 27 26 25 24 23 22 21 20 512 256 128 64 32 16 8 4 2 1 1 1 1 1 163-128 = 35 35-32 = 3 3-2=1 1-1=0

Converting to Binary What are the binary equivalents of… 9 27 68 216 255

Converting to Binary What are the binary equivalents of… 9 = 1001 (or 8+1) 27 = 0001 1011 (or 16+8+2+1) 68 = 0100 0100 (or 64+4) 216 = 1101 1000 (or 128+64+16+8) 255 = 1111 1111 (or 128+64+32+16+8+4+2+1)

Binary Tips and Tricks Some “sanity checking” rules for conversions: Binary can only be 1 or 0 If you get “2” of something, it’s wrong Odd numbers must have a 1 in the ones column Why? (And what’s the rule for even numbers?) Each column’s value is the sum of all of the previous columns (to the right) plus one In decimal, what column comes after 999?

File Input and Output (Review)

Quick Review Write the lines of code for the tasks below Open the file “goodDogs.txt” Read the file in (however you want), and print out each dog’s name in the sentence “X is a good dog” Finish using the file (what do you need to do?) goodDogs.txt Thor,Corgi Coco,Chocolate Lab Beethoven,St. Bernard

Exercise: Jabberwocky Write a program that goes through a file and reports the longest line in the file Example Input File: carroll.txt Beware the Jabberwock, my son, the jaws that bite, the claws that catch, Beware the JubJub bird and shun the frumious bandersnatch. Example Output: >>> longest.py longest line = 42 characters the jaws that bite, the claws that catch,

Jabberwocky Solution Pseudocode inside main: open the file "carroll.txt" (for reading) create a variable to store the "longest" line # we'll refer to this variable as "record" # what should this variable be initialized to? for each line of the input if the current line is longer than the record update the record to the current line print the length of the longest line print the longest line call main

Jabberwocky Solution Code def main(): inputFile = open("carroll.txt") longest = "" lines = inputFile.readlines() for i in range(len(lines)): if len(lines[i]) > len(longest): longest = lines[i] print("Longest line =", len(longest)) print(longest) main()

Jabberwocky Solution Walkthrough line = Beware the Jabberwock, my son, the jaws that bite, the claws that catch, Beware the JubJub bird and shun the frumious bandersnatch. line = "Beware the Jabberwock, my son," longest = "" longest = "Beware the Jabberwock, my son," len(line) > len(longest) for i in range(len(lines)): if len(lines[i]) > len(longest) longest = lines[i] 31 > 0 

Jabberwocky Solution Walkthrough Beware the Jabberwock, my son, the jaws that bite, the claws that catch, Beware the JubJub bird and shun the frumious bandersnatch. line = line = "the jaws that bite, the claws that catch," longest = "" longest = "Beware the Jabberwock, my son," longest = "the jaws that bite, the claws that catch," len(line) > len(longest) for i in range(len(lines)): if len(lines[i]) > len(longest) longest = lines[i] 42 > 31 

Jabberwocky Solution Walkthrough Beware the Jabberwock, my son, the jaws that bite, the claws that catch, Beware the JubJub bird and shun the frumious bandersnatch. line = line = "Beware the JubJub bird and shun" longest = "" longest = "Beware the Jabberwock, my son," longest = "the jaws that bite, the claws that catch," len(line) > len(longest) for i in range(len(lines)): if len(lines[i]) > len(longest) longest = lines[i] 32 > 42 

Jabberwocky Solution Walkthrough Beware the Jabberwock, my son, the jaws that bite, the claws that catch, Beware the JubJub bird and shun the frumious bandersnatch. line = line = "the frumious bandersnatch." longest = "" longest = "Beware the Jabberwock, my son," longest = "the jaws that bite, the claws that catch," len(line) > len(longest) for i in range(len(lines)): if len(lines[i]) > len(longest) longest = lines[i] 27 > 42 

Daily CS History Katherine Johnson Worked as a NASA “computer” Calculated trajectories, launch windows, and return paths for flights in Project Mercury Plotted Alan Shepard’s 1961 journey to space (first American) Also examined black box data from crashed air planes Graduated college at age 18

Announcements Homework 6 is available now Recommended that you do the parts in order Homework is due on Tuesday, November 21st No discussion next week (Thanksgiving) No lecture on Wednesday Final exam is when? Friday, December 15th from 6 to 8 PM

Image Sources Katherine Johnson: https://commons.wikimedia.org/wiki/File:Katherine_Johnson_at_NASA,_in_1966.jpg