CS 115 Lecture 2

CS 115 Lecture 2

CS 115 Introduction Fundamentals of computer science, computers and programming Taken from notes by Dr. Neil Moore What is programming? CS 115 is titled Introduction to Programming. What is that? Telling a computer what to do? But every time I click on a button or press a key, I am telling the computer what to do. Thats not quite what we mean by programming. We mean writing computer programs. Back up a second what is a program outside of computing? a TV show is called a program a concert has a program what is going to happen when, in what order

A program is a sequence of instructions telling a computer how to do something. You plan out the steps in advance for how to solve a kind of problem Then we have the computer execute (follow) the steps the program. What is computer science? So what is computer science, and how does it differ from programming? the study of computers? Computer science is no more about computers than astronomy is about telescopes. attributed to Edsgar Dijkstra Questions about computation came up long before computers. It used to be people following the step-by-step instructions. They used tools like an abacus, a slide rule, pencil and paper,

What did we call those people? Computers ! When you do long division or sort a list of items, you are computing. Computers! A very early computer network around 1890. This is from Wikipedia.org, Harvard computers, E.C. Pickerings astronomy lab at Harvard. What is computer science?

Computer science is the study of: What can be computed using step-by-step procedures. How best to specify these procedures. How to tell if a procedure is correct, efficient, optimal, etc. How to design procedures to solve real-world problems. Algorithms = Designs Step-by-step procedure is a mouthful. We have names for that: an algorithm or a design. These are the steps to solve a problem.

Steps can be described in English or other natural languages, or flowcharts or maps or drawings or programming languages In programming, a design is written in pseudocode. ordered (sometimes numbered) steps in English not the same as coding; instead, an abstract view of what needs to be done Figure out what youre going to do before you start doing it! Well start with a non-computer example. Design: building a dog house Design: building a dog house Lets say we want to build a dog house. What steps do we need to take? 1. Decide on a location and a size for the doghouse. 2. Get materials for the house.

3. Cut a piece for the floor. 4. Cut shapes for the four walls. 5. Cut a door into one wall. 6. Attach walls to the floor. 7. Make roof. 8. Attach roof to walls. 9. Paint the outside. Notes on the Design Steps are numbered in the order they should be performed. If I try cutting the door after attaching the walls to the floor, it will be difficult. We ask you to number your steps for the first few designs in this class. Some steps could be further divided: Get materials: what materials? Where? Do we need a budget?

Make roof: cut some wood at an angle, nail together, add shingles Cut shapes for four walls: a step that is repeated 4 times Could go into more detail: how big is a wall? The door? What units? Designs can go through several versions, each more refined than the last Design for dog house, refined 1. Decide on a location and size for the doghouse. 2. Get materials for the house. 1. Get lumber. 2. Get paint. 3. Get nails. 3. Cut a piece of wood for the floor.

4. Repeat four times: 1. Cut a piece of wood for a wall. 5. Cut a door into one wall. 6. Attach walls to the floor. 7. Make roof. 1. Cut two pieces of wood. 2. Join the pieces at a 90 degree angle. 3. Nail the pieces together. 8. Attach roof to walls. 9. Paint the outside of the walls. Basic Hardware Hardware vs. Software

Hardware includes CPU = central processing unit Memory = RAM (random access memory) Input = Keyboard, mouse, microphone Output = Screen, speaker, printer Storage = Hard drive, DVD, Solid State devices Volatile vs. non-volatile Memory and the CPU are volatile they lose their contents when the power cuts

off Storage is NON-volatile they maintain their contents until you erase them Parts of a computer RAM: the computers working memory Random Access Memory Made up of circuits (each one a word) that each hold one number Numbers are represented in binary Volatile: information is lost when the power goes off.

Fast devices to access (retrieve or write to), in nanoseconds Relatively expensive part of the computer Von Neumann architecture: CPU reads instructions from RAM. Secondary storage: hard drives, flash drives, DVDs, Persistent: data can be stored for years or decades Slow (microseconds or milliseconds: less than 1/1000th the speed of RAM) Relatively cheap part of the computer Data and instructions must be transferred from secondary storage to RAM before the CPU can use

Parts of a modern computer CPU: Central Processing Unit. Reads instructions from RAM Executes (carries them out) in order Instructions are simple: add numbers, is-equal, skip to another instruction Works with speeds as fast as RAM (nanoseconds) Relative expensive component of a computer Peripherals

Input devices bring data in to RAM from the outside world Output devices take data out from RAM to the outside world Computer numbers RAM consists of circuits that each can store a single binary digit = 1 bit, usually written as 0 or 1 Computers use binary numbers: the place values are powers of two Place values: 1, 2, 4, 8 , 16, 32, 64, 128, 256, 512, 1024, 2048, So what would the binary number 0 0 1 0 1 0 1 1 be in decimal? = 0 + 0

+ 32 + 0 + 8 + 0

+ (More about this in chapter 3) Bits are combined into bytes, in modern usage 8 bits. One byte can represent a number from 0 to 255 Or a single character in ASCII code. 2 + 1 = 43 Computer capacity or speed units Its pretty inconvenient to throw around very large numbers when talking about the capacity of a hard drive or SD card or the speed of a download

(numbers of bytes), so larger units were created Kilobyte (kB): 210 = 1024 bytes (roughly a page of text) Megabyte (MB): 220 = 1024 kB or 1024 * 1024 bytes, which is approximately one million bytes (like a 1000-page book) A song in MP3 format might take 3 or 4 MB. Gigabyte (GB): 230 = 1024 MB = 1024 * 1024 * 1024 bytes, which is approximately 1 billion bytes (like 1000 of those books, a library!) A DVD is about 4.7 GB in capacity, a Blu-Ray might be 17 GB. A modern personal computer might have 16 GB of RAM. Computer units Terabyte (TB): 240 = 1024 * 1024 * 1024 * 1024 bytes (1024 GB, a large library) A modern hard drive might contain 1 to 2 TB of storage.

Calculating with computer units: Suppose you had a 16 GB USB stick and a bunch of 256 MB video files. How many videos could you put on the stick? 256 = 1024 / 4 or 1024 = 256 * 4 so you can get 4 videos in one GB 16 GB (the whole stick) can hold 4 * 16 videos = 64 videos on the stick You should NOT need a calculator for these. If you know the powers of 2 up to 1024, you can do simple fractions in your head. Suppose you had 13 files, each one 512 MB in size. How much total storage do they need? 512 is half of 1024, so 2 of them is 1024 MB = 1 GB. So 13 files = 6.5 GB

Useful fractions to know 1024 = 512 * 2 so 512 is 1024/2 one half 1024 = 256 * 4 so 256 is 1024/4 one quarter 1024 = 128 * 8 so 128 is 1024/8 one eighth Easy to remember 1024 = 32 * 32 so 32 is 1024 / 32 one thirty-second Programming languages Computer programming is the process of translating an algorithm into instructions that a computer can understand. A programming language is a formal constructed language designed to communicate instructions to a computer. There are thousands of programming languages in existence, dozens or hundreds of which are still in regular use.

A professional programmer usually knows several. They can choose the right tool (language) for each job. In CS 115 well learn to write programs in Python, a high-level interpreted programming language. Python was created by Guido van Rossum. Guido van Rossum, creator of Python Born in the Netherlands Began to develop Python in 1989 Has worked for Google, DARPA, Dropbox, retired at present (picture from Wikipedia, 2014) Named Python after Monty Pythons Flying Circus

Released Python 1.0 in 1994 Latest version of Python is 3.8 Python is used for things like websites, scripts, games, graphic interfaces. It is used by Pinterest, Instagram, Yahoo, Google, Dropbox, Netflix, etc. Programming languages have syntax and semantics In a given programming language: Syntax are the rules that say what programs look like, how the statements in the language are formed Spelling of keywords Punctuation Order and combination of words (grammar)

Semantics are the rules that say what the programs mean: What does the computer do when it executes this statement? When you combine these statements, what happens inside the computer? Low-level languages Machine code: numbers treated as instructions by the CPU. 05 01 23 Assembly code: humanly readable way of writing machine code add EAX, 1 mov [ESP+4], EAX One instruction does very little, takes huge number of steps to accomplish anything Every different processor type has their own machine and assembly languages: Intel (32 and 64 bit), ARM, PowerPC,

For many years, these were the only ways to write programs Difficult to learn, verbose, error-prone and machine-specific Does anyone still write in assembly? Yes! Small devices where size of code is important, hardware specific code like device drivers, where performance is critical Analogy to cars: why do we need sports cars when we have sedans and trucks? High-level languages Low-level languages have very simple instructions so you need lots of them to do anything useful. High-level languages like Python and C++ make things easier for the humans. One of their statements can stand for hundreds of machine code instructions. Assembly language: mov EAX, EBP[-2] mov EBX, EBP[-4]

add EBX, 100 mul EAX, EBX div EAX, 100 mov EBP[2], EAX High-level language: total = price * (tax + 100) / 100 And you can translate it into many different machine code languages for different processors if you want to. High-level languages are portable like that. Interpreters and Compilers

In All cases, the computer still only understands machine code. So if we write in a high-level language, we have to have it translated into machine code. Generally there are two ways to do this: an interpreter or a compiler. Interpreter: translates the statements and executes the statements in order +plus: Easy to change your program you edit it, then run it again. -minus: The statements must be translated each time: this makes it slow. -minus: Users of the program must have a copy of the interpreter for themselves. - Examples of interpreted languages: Python, JavaScript, Perl. Compilers Compiler: translates statements in a language into machine code. Without executing it! -minus: Changing a program requires another step after editing it, you have to

compile before you run. +plus: Compile once, execute many times, no repeated translation needed, so it runs faster +plus: The machine code is run directly by the Operating System. You do not need to have the compiler on the machine at all. Examples: C++, FORTRAN, Haskell. Some languages combine features of both: Java is compiled into an intermediate byte code and then interpreted. A Baking Analogy Suppose you wanted to make baklava (a kind of Greek dessert pastry). You have a recipe on paper but its in Greek and you dont know the language. You have a friend who speaks Greek and English but doesnt know

how to bake. A Baking Analogy What do you do? Two options: 1. Have your friend stand with you in the kitchen, reading the recipe (in Greek) and telling you the instructions one at a time in English. Your friend is acting as an interpreter. 2. Give your friend the recipe and ask them to translate it from Greek into English and write it down. Your friend is acting as a compiler. You can get started quicker with the interpreter, but you will need your friend in the kitchen every single time. It takes a bit more time at the beginning for the compiler, but once your friend has done their job, you dont need them any more you have the recipe written in English.

Programming environment and tools What do you need to write programs in Python? An interpreter to translate and execute your program A text editor for writing and changing your source code Notepad is possibly useful but not really suited to programming More advanced editors can: Automatically indent the code Color code to clarify its meaning

Jump from variable name to its definition Jump from function call to its definition Much more Programming environment and tools A debugger to help find and repair bugs Pauses execution at a given line Steps through code line by line. Inspects the values of variables These are just some of the tools used by professional programmers. When programming was a new activity, programmers used the command line a lot each tool was run by a command, in a certain order: first edit, then compile, then run, then edit, then compile When Graphical User Interfaces (GUIs) became available,

programmers started using them to hold all their tools together. Integrated development environments An IDE (integrated development environment) combines several programming tools together into one cohesive program. Some IDEs for Python: IDLE comes with Python its installed when Python is. WingIDE is recommended for this class its free, more professional looking and less likely to crash. PyScript, PyCharm are a couple other IDEs that you can find for free. Lab 1 will ask you to use WingIDE. Debugging and other topics in a few weeks.

How to do a design in CS 115 Use a plain text editor, not a word processor The editor in IDLE or WingIDE works fine Notepad works Mac TextEdit: go to the Format menu, choose Make Plain Text otherwise it saves as RTF (rich text format) NOT plain text! State the purpose of the program at the top follow by your name, section, email Write one step per line Start each step with a # symbol (well see why next time). If you break one step down into parts, indent them under the main step and number them accordingly, like step 7 is broken into 7.1, 7.2, 7.3,

How to do a Design in CS 115 Hint: wait until you are satisfied with your design to number the steps. Why? that way you dont have to renumber if you add or delete steps Give your file a name ending with .py as an extension (Python code) Why? the design will be the skeleton for your implementation (the outline) when you start writing Python code. This way its easy to make a copy of your design and start filling in Python code after each step. The design step tells you what needs to be done. Example program design # # #

# # # # Purpose: Ask for the users name and greet them. Author: J. Random Hacker, section 1, [email protected] Main Program: 1. Input the users name from the keyboard 2. Ask for their age 3. Tell them how old they will be in 10 years. Design turned into code # Purpose: Ask for the users name and greet them.

# Author: J. Random Hacker, section 1, [email protected] # Main Program: def main(): # 1. Input the users name from the keyboard name = input(Whats your name? ) # 2. Ask for their age age = int(input(How old are you (in years)? )) # 3.

Tell them how old they will be in 10 years. print(In 10 years you will be, age+10, years old) main() The first computers Charles Babbage designed the difference engine, 1823-1842, to compute values of polynomials. Was not able to finish in his lifetime. One was built from his plans in 1991, and it worked! Woman who worked with him, Lady Ada Lovelace (daughter of poet Lord Byron) first programmer she wrote an appendix to a paper on the engine which contained an algorithm designed to be carried out by the machine

(Wikipedia, http://en.Wikipedia.org/wiki/Ada_Lovelace) Babbages Difference Engine, built in 1991 Image taken by Allan J. Cronin, Wikipedia, March 2009 Device is at the London Science Museum Programming early computers Early computers were designed to solve one specific problem they were built that way think of them as gigantic calculators. Some could be reprogrammed by flipping switches or plugging in cables. A flipped switch might make the machine enter a number into the store.

Cables would be connected from the store to the adder or comparator, etc. Setting up the machine to solve a problem could take days! But even so, that was faster and more accurate than humans. Stored programs British mathematician Alan Turing described in 1936 a mathematical model of how machines could compute. He is one of the founders of modern computer science He realized you could make a universal machine.

It would take as part of its input, a description of the program to be run. Programs become just another kind of data in memory! John von Neumann developed these ideas further in 1944. It meant that computers could run without modifying their hardware at all! No cords and switches! It meant that computers could run much faster no waiting for humans to do things. It meant that computers became general-purpose machines. Change the software (programs) and you had a different behavior. Thats why we can use one machine to run spreadsheets and games and music and browsers and so on. Alan Turing, British computer scientist 29th March 1951 date of image Image supplied by NPL Archive,

Science Museum, South Kensington, London, England Stored programs Turing went on to develop the bombe to break the Nazis encryption scheme. Germany used the Enigma machine to encrypt war messages. The bombe figured out which settings the Enigma used each day. 2014 film: The Imitation Game is about his life That was his name for the Turing Test: how can we tell if a computer is really intelligent? When a conversation with it cannot be distinguished from one with a human Annually the Turing Award is given for major contributions of lasting importance to computing, created in 1966. http://amturing.acm.org Came to a sad end: in 1952, convicted of being gay and committed suicide in

1954. Just a few years ago the British government pardoned him. the bombe

Recently Viewed Presentations

  • DESCRIPTION OF THE GIRO_PERT ON THE DETAILS OF

    DESCRIPTION OF THE GIRO_PERT ON THE DETAILS OF

    To obtain a blue-shift the arguments must be negative. In this case the value is parsed as a double precision floating point. In SRF_SHAPE mode this is the maximum value of the perturbation that is allowed (dimensionless). The user must...
  • The People and Religions of Asia - TypePad

    The People and Religions of Asia - TypePad

    Believed in unselfishness Did not believe in Gods or see the need for priests Felt a man could change evil to good if he followed the Middle Way Eventually one would achieve perfect peace Buddhism today 6% of the world...
  • Acid-base Disorders

    Acid-base Disorders

    Metabolic Alkalosis May be due to: 1.H Loss 2.HCO3 Retention 3.Volume Depletion (Contraction Alkalosis) Compensatory response: 1 meq/lit HCO3 0.7 mmHg PCO2 - + Quiz 2 A patient with heart failure receiving diuretics has the following ABG: pH=7.46 , HCO3=30...
  • Developing Plans to End Homelessness

    Developing Plans to End Homelessness

    A Plan to End Homelessness in Yakima County by2014. Yakima. Systems prevention activities focus around using legal services to prevent evictions. Plan calls for expanding affordable housing; no specific numbers on units are provided. Homeless Network of Yakima County. City...
  • Elementary Statistics, 13e - Sybil Nelson

    Elementary Statistics, 13e - Sybil Nelson

    Here the classical approach cannot be used because the two outcomes (dying, surviving) are not equally likely. A subjective probability can be estimated in the absence of historical data. Example: Texting and Driving(1 of 4)
  • Peitho Personified, Reified - Binghamton University

    Peitho Personified, Reified - Binghamton University

    Peitho Personified, Reified. you'll have noticed that the ancient readings for today run the gamut from inscriptions to bits of travel literature to short poems, fragments of poems, lengthy excerpts from epic poems, history, etc. etc.
  • Foster Care Education: Improving Educational Outcomes for Children

    Foster Care Education: Improving Educational Outcomes for Children

    National Facts and Figures. Less than 50% of youth in foster care finish high school before leaving care. Children and youth in out-of-home care experience 1 or 2 placement changes per year on average.
  • Policy and Implementation Plan for Public Access to

    Policy and Implementation Plan for Public Access to

    Is there a grace period for implementing the new requirements? ANSWER: No. However, ORO will give facilities a reasonable period of time to revise their relevant policies and procedures. Facilities are reminded that requirements for results access took effect on...