GENG 106 Computer Programming Chapter 1 PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document is a chapter from a computer programming textbook, GENG 106, exploring fundamental concepts of computers, hardware, software, and memory. It provides an introductory overview of key components in computer systems.
Full Transcript
GENG 106: Computer Programming Chapter 1 Introduction to Computers And Programming COMPUTER SYSTEMS: HARDWARE AND SOFTWARE GENG 106 Computer Programming 2 Introduction Computers can be programmed Designed to do any job that a program tells them to Program:...
GENG 106: Computer Programming Chapter 1 Introduction to Computers And Programming COMPUTER SYSTEMS: HARDWARE AND SOFTWARE GENG 106 Computer Programming 2 Introduction Computers can be programmed Designed to do any job that a program tells them to Program: set of instructions that a computer follows to perform a task Commonly referred to as Software Programmer: person who can design, create, and test computer programs Also known as software developer Computer: a programmable machine designed to follow instructions (orders) in a program. Why Program? To solve problems (develop apps, games, website), speed up calculations etc. GENG 106 Computer Programming 3 Main Hardware Component Categories Hardware is the physical devices that make up a computer. Computer is a system composed of several components that all work together Typical major components: GENG 106 Computer Programming 4 Central Processing Unit (CPU) o The part of the computer that actually runs programs Most important component Without it, cannot run software CPU includes: Control Unit Retrieves and decodes program instructions. Coordinates computer operations. Arithmetic & Logic Unit (ALU) Performs mathematical operations. Performs logical operations (eg, comparisons). GENG 106 Computer Programming 5 Main Memory Main memory: is where computer stores a program while program is running, and data used by the program Known as Random Access Memory or RAM CPU is able to quickly access data in RAM RAM is volatile memory: Memory used for temporary storage while program is running Contents are erased when computer is off GENG 106 Computer Programming 6 Main Memory Organization Bit 8 bits Smallest piece of memory Stands for binary digit Has values 0 (off) or 1 (on) 0 1 1 0 0 1 1 1 Byte Is 8 consecutive bits Kilobyte (KB) 1 byte Is 1024 bytes Megabyte (MB) Is 1024 kilobytes (i.e. 1024x1024 = 1,048,576 bytes) Gigabyte (GB) Is 1024 megabytes (i.e. 1024x1024x1024 = 1,073,741,824 bytes) Terabyte (TB) Is 1024 gigabytes (i.e. 1024x1024x1024x1024 = 1,099,511,627,776 bytes) GENG 106 Computer Programming 7 Secondary Storage Non-volatile - data retained when program is not running or computer is turned off. Programs normally stored here and loaded to main memory when needed Comes in a variety of media Magnetic: floppy or hard disk drive, internal or external Optical: CD or DVD drive Flash: USB flash drive Secure Digital (SD): micro-SD card GENG 106 Computer Programming 8 Input Devices Input: data the computer collects from people and other devices Input device: component that collects the data. Used to send data from outside to the computer. Examples: keyboard, mouse, scanner, camera, touchscreen Disk drives can be considered input devices because they load programs into the main memory Lets identify Inputs in a modern smartphone. GENG 106 Computer Programming 9 Output Devices Output: data produced by the computer for other people or devices Can be text, image, audio, or bit stream Output device: formats and presents output Many devices can be used for output. Examples: Computer screen, printer, speakers. Disk drives and USB drives can be considered output devices because data is sent to them to be saved What are the outputs of a smartphone? GENG 106 Computer Programming 10 SOFTWARE GENG 106 Computer Programming 11 Software Everything the computer does is controlled by software General categories: Application software System software Application software: programs that make the computer useful for every day tasks Examples: word processing, email, games, and Web browsers System software: programs that control and manage basic operations of a computer Operating system: controls operations of hardware components Utility Program: performs specific task to enhance computer operation or safeguard data Software development tools: used to create, modify, and test software programs GENG 106 Computer Programming 12 How a Program Works Figure 1-16 The fetch-decode-execute cycle GENG 106 Computer Programming 13 PROGRAMS AND PROGRAMMING LANGUAGES GENG 106 Computer Programming 14 From Machine Language to Assembly Language Impractical for people to write in machine language Assembly language: uses short words (mnemonics) for instructions instead of binary numbers Easier for programmers to work with Assembler: translates assembly language to machine language for execution by CPU Sample Assembly language code: GENG 106 Computer Programming 15 Programs and Programming Languages Programming Language : a language used to write programs. Low-level languages: close in nature to machine language (the only language that the machine understands which is a sequence of bits (1, or 0)) Example: assembly language Difficult to write (and read). High-level languages: closer to human language. Allows simple creation of powerful and complex programs similar to everyday English (e.g., if, while, print,...) –No need to know how CPU works or write large number of instructions –Single statements accomplish substantial tasks. –easier to read (ex: if (a=b) then x=0 else x=1; ) and errors are easier to find. –Programs are portable from one computer to another. How can we write programs in high-level languages, and the computer can still understand them? GENG 106 Computer Programming 16 Compiler translates high-level language program into separate machine language program. Whole program is translated. Machine language program can be executed at any time – no more compilation. 0000000000010010 0010111011111101 x = 10; y=2; 0000000000010000 z = x + y; 0010111010110101 a=(z+5)/(x-y); Compiler 0000000000010010 0010111011111101 Program written in High Level Language such as C++ Machine language statements GENG 106 Computer Programming 17 Interpreter Translates and executes instructions in high-level language program Used by Python language. Interprets one instruction at a time. No separate machine language program. Source code: statements written by programmer Syntax error: prevents code from being translated GENG 106 Computer Programming 18 Discussion & Questions When a program runs on a computer, the part of the computer o that carries out the instructions is called the:……………………. A bit is: o an alternative term for byte. o an electronic device used in computers. o a binary digit, like 0 or 1. A compiler: o maintains a collection of programs. o tests a program's logic. o translates source code into executable code. o translates executable code to machine code GENG 106 Computer Programming 19 WHAT IS A PROGRAM MADE OF? GENG 106 Computer Programming 20 Statements an instruction to the computer to perform an action. may contain keywords, operators, programmer-defined identifiers, and punctuation. Statements may occupy multiple lines. x = 2 #assignment statement x = x + 2 #assignment with expression print(x) #print statement Variable Operator Constant Function GENG 106 Computer Programming 21 Keywords Also called reserved words Have a specific meaning. Cannot be used for another purpose. Written in lowercase letters. Examples: break, continue, if, while, else, …etc. GENG 106 Computer Programming 22 Programmer-Defined Identifiers Names given by the programmer to identify parts of a program Not part of the Python language. Used to represent various things, such as variables (memory locations). GENG 106 Computer Programming 23 Operators Used to perform operations on data Many types of operators Arithmetic: +, -, *, / Assignment: = GENG 106 Computer Programming 24 Variables A variable is a named location in computer memory (in RAM). holds a piece of data that has a value. num2 values 12 #71 variable #72 names 17 #73 sum addresses/ locations num1 5 #77 GENG 106 Computer Programming 25 INTRODUCTION TO PROBLEM SOLVING GENG 106 Computer Programming 26 Problem Solving Algorithm: is a set of well-defined logical steps that must be taken to perform a task An Algorithm example o This is a general definition, however 1. Start our concern here is to write algorithms 2. Print “Enter first number” 3. Read A. to solve computational problems 4. Print “Enter second number” o We want to specify the steps that 5. Read B. 6. Print “Enter third number” should be followed by the computer to 7. Read C. perform a specific task, and write them 8. S=A+B+C using a human language like English – 9. X=S/3 see the example algorithm → 10. Print X. 11. End. An algorithm can also be represented graphically (using a flowchart) GENG 106 Computer Programming 27 Program Structure Whether written as text or represented in a flowchart, an algorithm is made of the following control structures: By control structure we mean the logical design that controls the order in which a set of statements execute Sequence structure: set of statements that execute in the order they appear (See the algorithm on the previous slide) Decision structure: specific action(s) performed only if a condition exists appear (See the flowchart on the next slide) Also known as selection structure Repetition structure: set of statements that execute repeatedly a specific number of times or based on a condition. Also known as loop structure GENG 106 Computer Programming 28 Introduction to Flowcharts It is a formal diagram that is used to represent an algorithm, process, or workflow. It uses the following symbols: GENG 106 Computer Programming 29 GENG 106 Computer Programming 30 USING PYTHON GENG 106 Computer Programming 31 Using Python Python must be installed and configured prior to use One of the items installed is the Python interpreter. Python interpreter can be used in two modes: Interactive mode: enter statements on keyboard. Script mode: save statements in Python script. GENG 106 Computer Programming 32 Interactive Mode When you start Python in interactive mode, you will see a prompt. Indicates the interpreter is waiting for a Python statement to be typed. Prompt reappears after previous statement is executed. Error message displayed If you incorrectly type a statement. Good way to learn new parts of Python – suitable for programs of 3-4 lines long. GENG 106 Computer Programming 33 Writing Python Programs and Running Them in Script Mode Statements entered in interactive mode are not saved as a program. To have a program use script mode. Save a set of Python statements in a file. The filename should have the.py extension. To run the file, or script, type (python filename)at the operating system command line. Or use the Run menu. GENG 106 Computer Programming 34 The IDLE Programming Environment IDLE (Integrated Development and Learning Environment): a single program that provides tools to write, execute and test a program. Automatically installed when Python language is installed. Runs in interactive mode. Has built-in text editor with features designed to help write Python programs (scripts). Other good development environments Visual Studio Code Pycharm PyDev..more GENG 106 Computer Programming 35 Discussion & Questions o True or False: 1. A programmer writes a high-level program. 2. The programmer runs a compiler, which converts the high-level program into an executable program. 3. Users can run the executable. GENG 106 Computer Programming 36