690 likes | 888 Views
CSC 131 - Introduction to Computer Science I. Devon M. Simmonds Computer Science Department University of North Carolina Wilmington Wilmington, NC 28403 simmondsd@uncw.edu http://www.uncw.edu/people/simmondsd/ _____________________________________________________________. Overview.
E N D
CSC 131 - Introduction to Computer Science I Devon M. Simmonds Computer Science Department University of North Carolina Wilmington Wilmington, NC 28403 simmondsd@uncw.edu http://www.uncw.edu/people/simmondsd/ _____________________________________________________________
Overview • To understand computer basics, programs, and operating systems • Course motivation • Introduction to algorithm design • Overview of Python programming language
What is a Computer? A computer consists of a CPU, memory, hard disk, floppy disk, monitor, printer, and communication devices. M o t I v a t I o n
CPU The central processing unit (CPU) is the brain of a computer. It retrieves instructions from memory and executes them. The CPU speed is measured in megahertz (MHz), with 1 megahertz equaling 1 million pulses per second. The speed of the CPU has been improved continuously. If you buy a PC now, you can get an Intel Pentium 4 Processor at 3 gigahertz (1 gigahertz is 1000 megahertz). M o t I v a t I o n
Memory Memory is to store data and program instructions for CPU to execute. A memory unit is an ordered sequence of bytes, each holds eight bits. A program and its data must be brought to memory before they can be executed. A memory byte is never empty, but its initial content may be meaningless to your program. The current content of a memory byte is lost whenever new information is placed in it. M o t I v a t I o n
How Data is Stored? Data of various kinds, such as numbers, characters, and strings, are encoded as a series of bits (zeros and ones). Computers use zeros and ones because digital devices have two stable states, which are referred to as zero and one by convention. The programmers need not to be concerned about the encoding and decoding of data, which is performed automatically by the system based on the encoding scheme. The encoding scheme varies. For example, character ‘J’ is represented by 01001010 in one byte. A small number such as three can be stored in a single byte. If computer needs to store a large number that cannot fit into a single byte, it uses a number of adjacent bytes. No two data can share or split a same byte. A byte is the minimum storage unit. M o t I v a t I o n
Storage Devices Memory is volatile, because information is lost when the power is off. Programs and data are permanently stored on storage devices and are moved to memory when the computer actually uses them. There are three main types of storage devices:Disk drives (hard disks and floppy disks), CD drives (CD-R and CD-RW), and Tape drives. M o t I v a t I o n
Output Devices: Monitor The monitor displays information (text and graphics). The resolution and dot pitch determine the quality of the display. M o t I v a t I o n
Monitor Resolution and Dot Pitch resolution The resolution specifies the number of pixels per square inch. Pixels (short for “picture elements”) are tiny dots that form an image on the screen. The resolution can be set manually. The higher the resolution, the sharper and clearer the image is. However, the image may be very small if you set high resolution on a small screen monitor. PC monitors are usually 15-inch, 17-inch, 19-inch, or 21-inch. For a 15-inch monitor, a comfortable resolution setting would be 640480 (307,200 pixels). dot pitch The dot pitch is the amount of space between pixels. The smaller the dot pitch, the better the display. M o t I v a t I o n
Communication Devices A regular modem uses a phone line and can transfer data in a speed up to 56,000 bps (bits per second). A DSL (digital subscriber line) also uses a phone line and can transfer data in a speed 20 times faster than a regular modem. A cable modem uses the TV cable line maintained by the cable company. A cable modem is as fast as a DSL. Network interface card (NIC) is a device to connect a computer to a local area network (LAN). The LAN is commonly used in business, universities, and government organizations. A typical type of NIC, called 10BaseT, can transfer data at 10 mbps (million bits per second). M o t I v a t I o n
Motivation • Computer science, the science of solving problems with the aid of a computer • Artificial intelligence • Database management systems • Distributed systems • Computer graphics • Operating systems • Software engineering All of these areas of study revolve around one fundamental question: how can we solve problems using computers? M o t I v a t I o n
??? M o t I v a t I o n • Solving problems with the aid of a computer • instruct the computer to do what we want ? • Humans communicate through naturallanguages: English, Spanish, French, etc. • The computer has its own language! • Bits and bytes, 0’s and 1’s – machine language • So humans and computers speak different languages! • 1st Problem: • How do we give instructions to a computer to do what we want it to do if we speak different languages?
??? M o t I v a t I o n • What language should the interpreter speak? • Human language • Machine language • Interpreter translates human language into machine language • 2nd Problem: • Human language is ambiguous! Employ an Interpreter
M o t I v a t I o n Human language Programming language Machine language • 2nd Problem: Human language is ambiguous: • Create a language for writing instructionsfor a computer – programming language • Interpreter translates instructions written in a programming language into machine code. • The process of writing instructions for a computer to execute is called programming. Interpreter
M o t I v a t I o n Programming? • Programming - the process of using a programming language to write instructions for a computer. • There are two general steps involved in programming: algorithmic design, coding. • Algorithmic design: • Before you can ask a computer to solve a problem, you must figure out what the computer needs to do in order to reach a solution. • Algorithmic design is the process of describing in human terms what the computer needs to do to solve the problem. • An algorithm is a sequence of instructions to solve a problem. • Coding • After you have determined what the computer needs to do, and written it as an algorithm, you need to expressthe algorithm using a programming language (C, C++, Java, LISP, Prolog, Python, COBOL, etc.). • A sequence of instructions written in a programming language is called a program or code.
What is this course about? • This course is about solving problems • Before solving a problem, we must first understand the problem • Many solutions are possible for a given problem • Each solution should be expressed as a separate algorithm before being converted into program code. • In this course programs will be written using the Python programming language. • This course is therefore an introduction to programming in Python. • i.e., the course is an introduction to expressing solutions to programming problems as written algorithms and converting written algorithms to Python programs.
Course Details & Grading • Lectures • Labs • Homeworks • Programming Assignments • Quizzes • Tests Let’s Visit the Course Web Page
The Man without a Face, oil on panel, 11x14, 2005 – Dae-Woong Nam The Getting to Know You Roll Call • Background • Where from • Previous school • Programming experience
Your Questions? • What are your course expectations? Your Questions? • What are your course expectations? Let’s explore…. Welcome Come with me … let’s explore this wonderful world of programming
CSC 131 - Introduction to Computer Science I Algorithm Design & Introduction To Programming in Python Devon M. Simmonds Computer Science Department University of North Carolina Wilmington Wilmington, NC 28403 simmondsd@uncw.edu http://www.uncw.edu/people/simmondsd/ _____________________________________________________________ Chapters 1 & 2 of course text
Problem Solving • Understand the problem • Design a solution/algorithm • Consider alternatives • Implement the solution/algorithm • Test solution and fix problems Introduction to Algorithm Design
Algorithm Design Example Travelling from UNC Chappell Hill to UNC Wilmington • Solution 1: • Pick up travellers at UNC Chappell Hill • Take travellers to airport • Board travels on aircraft to Wilmington • (Aircraft flies to Wilmington airport) • Deboard passengers from aircraft • Take travellers to UNC Wilmington • Solution 2: • Pick up travellers at UNC Chappell Hill • Take travellers to dock by bus • Board travels on ship • (Ship travels to Wilmington dock) • Deboard passengers from ship • Take travellers by bus to UNC Wilmington Introduction to Algorithm Design
Solution 3: The language used to express our algorithm may vary Introduction to Algorithm Design
Problem Solving • Understand the problem • Design a solution/algorithm • Consider alternatives • Implement the solution/algorithm • Test solution and fix problems Introduction to Algorithm Design
CSC 121: Introduction to Computer Science I Algorithm Design Example 2
An algorithm A step Or statement Algorithm Design: A Robot that Makes Coffee • Problem: writing instructions for a robot to make coffee • Take coffee jar from shelf • Open coffee jar • Tip one spoonful of coffee into cup • Pour hot water into cup • Stir coffee An algorithm is a finite set of instructions for solving a problem. Introduction to Algorithm Design
Coffee Making Robot Algorithm Design • Algorithm for a robot to make coffee Step 1: Take coffee jar from shelf Step 2: Open coffee jar Step 3: Tip one spoonful of coffee into cup Step 4: Pour hot water into cup Step 5: Stir coffee Each step in our algorithm must be simple enough to be executed by the robot. Introduction to Algorithm Design
Coffee Making Robot Algorithm Design • Algorithm for a robot to make coffee Step 1: Take coffee jar from shelf Step 2: Open coffee jar Step 3: Tip one spoonful of coffee into cup Step 4: Pour hot water into cup Step 5: Stir coffee We need to rewtite these steps Each step in our algorithm must be simple enough to be executed by the robot. Introduction to Algorithm Design
Stepwise Refinement • A solution design technique that describes a solution to a problem at a high level, and then partitions it repeatedly into more detailed levels one level at a time until the detail is sufficient to allow execution by a processor or translation into the language of the processor. Introduction to Algorithm Design
Stepwise Refinement of Statements 3 & 4 /* Tip one spoonful of coffee into cup */ Extract one teaspoonful of coffee Tip teaspoonful of coffee into cup Close coffee jar /* Pour hot water into cup */ Pour hot water into cup until cup is full Introduction to Algorithm Design
Coffee Making Algorithm With Steps 3 and 4 refined • Algorithm for a robot to make coffee Step 1: Take coffee jar from shelf Step 2: Open coffee jar Step 3: /*Tip one spoonful of coffee into cup*/ Extract one teaspoonful of coffee Tip teaspoonful of coffee into cup Close coffee jar Step 4: /*Pour hot water into cup*/ Pour hot water into cup until cup is full Step 5: Stir coffee Introduction to Algorithm Design
Top-down Design • A problem-solving technique that repeatedly divides a complex problem into simpler and simpler parts until the parts have sufficient detail to allow execution by a processor or translation into the language of the processor. Introduction to Algorithm Design
Part A.1 Part A.2 Part A.3 Part A.4 Top-down Design: Divide & Conquer Problem Part C Part A Part D Part B Part D.1 Part B.1 Part D.2 Part B.2 Part D.3 Part B.3 Part D.4 Part B.4 Introduction to Algorithm Design
Top-down Design & Stepwise Refinement • Specify solution to a problem as an algorithm with a sequence of steps • Refine the algorithm by defining a separate algorithm for each step • Refine each new step of the algorithm by defining a separate algorithm for the new step • Stop refining when the instruction can be directly converted into code Introduction to Algorithm Design
Top-Down Design of Coffee Making Algorithm Give robot instructions to make coffee Step 3 Step 1 Step 4 Step 2 Step 5 Step 4.1 Step 3.1 Step 3.2 Step 3.3 Introduction to Algorithm Design
Algorithms • Algorithm - a set of instructions (steps) for solving a problem. • E.g., a recipe for baking
Syntax and Semantics of Algorithms • Syntax: the rules governing the use of symbols or constructs in a language. The rules of the grammar. • John are in town. • Semantics: the meaning of statements in a language. • The peanut ate the elephant.
Functions of an algorithm • Input • Getting data from a user or a file • Storing data in memory • Processing • Performing calculations, make decisions • Output • Printing messages and data
Algorithm Design: Control Structures • Algorithm for robot to make coffee Step 1: Take coffee jar from shelf Step 2: Open coffee jar Step 3: #Tip one spoonful of coffee into cup Extract one teaspoonful of coffee Tip teaspoonful of coffee into cup Close coffee jar Step 4: #Pour hot water into cup Pour hot water into cup until cup is full Step 5: Stir coffee Control structures are ways of organizing the statements in an algorithm. Introduction to Algorithm Design
Algorithm Design: Control Structures • Algorithm for robot to make coffee Step 1: Take coffee jar from shelf Step 2: Open coffee jar Step 3: #Tip one spoonful of coffee into cup Extract one teaspoonful of coffee Tip teaspoonful of coffee into cup Close coffee jar Step 4: #Pour hot water into cup Pour hot water into cup until cup is full Step 5: Stir coffee Sequence: Steps are executed one after the other in the order listed. No steps are omitted. Execution of the last step signals the end of the algorithm. Introduction to Algorithm Design
The Python Programming Language Human language Programming language Machine language Communication
The Python Programming Language • Conceived in the late 1980s • Implementation was started in December 1989 by Guido van Rossum at CWI in the Netherlands • A successor to the ABC programming language capable of exception handling and interfacing with the Amoeba operating system.VanRossum is Python's principal author
The Python Programming Language • Python 0.9 - February 1991 • Python 1.0 - January 1994 • Python 2.0 - October 16, 2000 • Python 2.7 - July 3, 2010 • Python 3.0 - December 3, 2008 • Python 3.1 - June 27, 2009 • Python 3.2 - February 20, 2011 • Python 3.3 - September 29, 2012
The Python Programming Language # Hello World program in Java public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } } • # Hello World program in Python • print "Hello, World!"
Algorithm Python (Sequence ) # Algorithm to add two numbers # and print the result. Get the first number Get the second number Add the two numbers together Print the result # Python program to add two #numbers and print the result. # Get the first number num1 = 4 # Get the second number num2 = 6 # Add the two numbers sum = num1 + num2 # Print the result print (sum)
Programming Languages • A programming language is a special language used to write computer programs. • A program is a set of instructions a computer follows in order to perform a task. • A computer needs its instructions to be written in machine language. • Machine language is written using binary numbers. • The binary numbering system (base 2) only has two digits (0 and 1).
Programming Languages • The binary numbers are encoded as a machine language. • Each CPU has its own machine language. • Motorola 68000 series processors • Intel x86 series processors • DEC Alpha processors, etc. • Example of a machine language instruction: 1011010000000101
Development of Programming Languages • In the distant past, programmers wrote programs in machine language (1st generation). • Programmers developed assembly programming languages to make things easier (2nd generation). • …but assembler was processor dependent. • Next, 3rd generation or high-level languages were developed (3GL)
Programming Languages • High level programming languages are not processor dependent. • Common high-level programming languages: • BASIC • COBOL • Pascal • C • C++ • Java • Python • Alice
Programming LanguagesCommon Language Elements • There are some concepts that are common to virtually all programming languages. • Key words (if, while, class, etc.) • Operators • Arithmetic (+, -, *, /, etc.) • Relational (>, <, etc.) • Punctuation • Programmer-defined identifiers • Strict syntactic rules.