IT112 Mod 1.pdf
Document Details
Uploaded by SpellboundRational
Saint Louis University
Tags
Full Transcript
Computer Programming 1 School of Accountancy, Management, Computing and Information Studies Saint Louis University Module 1. Preliminary Concepts 1. Hardware 1.1. The Central Processing Unit The central processing unit (CPU) or the processor is said to be the “brain” of the computer. It is...
Computer Programming 1 School of Accountancy, Management, Computing and Information Studies Saint Louis University Module 1. Preliminary Concepts 1. Hardware 1.1. The Central Processing Unit The central processing unit (CPU) or the processor is said to be the “brain” of the computer. It is the unit which performs most of the processing inside a computer. It receives input data, executes instructions, and processes information. It communicates with input/output (I/O) devices. The CPU contains millions of tiny electrical parts. It is placed into a specific square-shaped socket found on all motherboards by inserting its metallic connectors or pins found on the underside. Each socket is built with a specific pin layout to support only a specific type of processor. There are different kinds of processors. Since modern CPUs produce a lot of heat and are prone to overheating, they are kept cool using appropriate fans or ventilation systems, and they are covered with heat sinks and thermal paste. To control instructions and the flow of data to and from other parts of the computer, the CPU relies on a chipset A chipset is a group of microchips located on the motherboard. The Control Unit and the Arithmetic Logic Unit (ALU) are the two components of the CPU. The control unit extracts instructions from memory and decodes and executes them. The control unit acts as an intermediary that decodes the instructions sent to the processor. The control unit, then, tells the other units such as the Arithmetic Logic Unit what to do by providing control signals. Furthermore, the control unit sends back the processed data back to memory. An arithmetic logic unit (ALU) is a digital circuit inside the processor that handles arithmetic and logical operations by loading data from input registers. After the control unit provides the ALU with the instruction on the operations that must be performed, the ALU completes them by connecting multiple transistors, and then stores the results in an output register. The control unit will then move this data to memory. To function properly, the CPU relies on the system clock, memory, secondary storage, and data and address buses. The CPU contains internal memory units, which are called registers. These registers contain data, instructions, counters and addresses used in the processing by the ALU. Some computers utilize two or more processors. These consist of separate physical microprocessors located side by side on the same board or on separate boards. Each CPU has an independent interface, separate cache, and individual paths to the system front-side bus. Multiple processors are ideal for intensive parallel tasks requiring multitasking. Multicore CPUs are also common, in which a single chip contains multiple CPUs. Since the first microprocessor was released by Intel in November 1971, CPUs have increased their computing power severalfold. The oldest Intel 4004 processor only performed 60,000 operations per second, while a modern Intel Pentium processor can perform about 188,000,000 instructions per second (https://www.techopedia.com/definition/2851/central-processing-unit-cpu). 1.2. Memory The memory stores the data and instructions needed by the CPU to do its appointed tasks. It is divided into several storage locations which have corresponding addresses. The CPU accesses the memory with the use of the addresses. Computer Programming 1 SLU-SAMCIS 1|Page 1.2.1. Main Memory The main memory is very closely connected to the processor. It is used to hold programs and data that the processor is actively working with. It is not used for long-term storage. It is sometimes called the RAM (Random Access Memory). The computer's main memory is considered as volatile storage. This means that once the computer is turned off, all information residing in the main memory is erased. 1.2.2. The Secondary Memory The secondary memory is connected to main memory. It is used to hold programs and data for long term use. Currently, the secondary memory in used are the solid state drive (SSD), hard disks, flash drives and optical discs (CDs, DVDs, etc.). Secondary memory is considered as non-volatile storage. This means that information residing in secondary memory is not erased after the computer is turned off. A rough comparison of the main memory and the secondary memory is presented in the following table. Property Main Memory Secondary Memory Speed Fast Slow Price Expensive Cheap Capacity Low High Volatile Yes No Table 1: Comparison between main memory and secondary memory 1.3. Input and Output Devices Input and output devices allow a computer system to interact with the outside world by moving data into and out of the system. Examples of input devices are keyboards, mice and microphones. Examples of output devices are monitors, printers and speakers. 2. Software A software is the program that a computer uses in order to function. It is kept on some hardware device like a hard disk, but it is intangible. The data that the computer uses can be anything that a program needs. Programs act like instructions for the processor. Some Types of Computer Software: a. Systems Programs Programs that are needed to keep all the hardware and software systems running together smoothly Examples: o Operating Systems like Linux, Windows, Unix, Solaris, MacOS b. Application Programs Programs that people use to get their work done Examples: o Word Processor o Game programs o Spreadsheets Computer Programming 1 SLU-SAMCIS 2|Page c. Programming Languages A programming language is a computer software which is used by the programmers to write their programs, scripts, and instructions which can be executed by a computer. Programming languages can be used to construct computer programs and operating system. JAVA, C, C++ and some others are programming languages. The computer understands only one language which is called the machine language. Machine language is in the form of ones and zeros. 3. Overview of Programming Languages A programming language is a standardized communication technique for expressing instructions to a computer. Like human languages, each language has its own grammar. Syntax refers to the grammar of a programming language. Syntax refers to the rules that must be followed in order to construct valid program statements. Programming languages enable a programmer to precisely specify what data a computer will act upon, how these data will be stored/transmitted, and precisely what actions to take under various circumstances. There are different types of programming languages that can be used to create programs, but regardless of what language you use, these instructions are translated into machine language that can be understood by computers. There are categories of programming languages. a. High-level Programming Languages A high-level programming language is a programming language that is more user-friendly, that is, the forms of statements can easily be understood by a human being. Examples are Java, C, C++, Basic, Fortran b. Low-level Assembly Language Assembly languages are similar to machine languages, but they are easier to program in because they allow a programmer to substitute names for numbers. Assembly languages are available for each CPU family, and each assembly instruction is translated into one machine instruction by an assembler program. Note: The terms "high-level" and "low-level" are inherently relative. Originally, assembly language was considered low-level and COBOL, C, etc. were considered high-level. Many programmers today might refer to the latter languages as low-level. High Level Language Low Level Language (Pascal) (Assembly Language) var CODE SEGMENT x, y, z : integer; ASSUME CS:CODE, DS:CODE begin ORG 100h x := 10; y := 3; START: if y 0 then MOV WORD PTR DS:[011Fh],000Ah z := x div y; MOV WORD PTR DS:[0121h],0003h end. CMP WORD PTR DS:[0121h],+00h JZ CS:011Dh MOV AX,DS:[011Fh] Machine Language DIV WORD PTR DS:[0121h] MOV DS:[0123h],AX (Hex/Binary) INT 20h 11000111 00001010 X DW 0 00000110 10100001 Y DW 0 00011111 00011111 Z DW 0 00000001 00000001 00001010 11110111 CODE ENDS 00000000 00110110 END START 11000111 00100001 00000110 00000001 00100001 10100011 Computer Programming 1 SLU-SAMCIS 3|Page Since it is highly impractical for people to create programs out of zeros and ones, there must be a way of translating or converting a language which human beings can understand into a machine language. Program Translation Program translation is the conversion of a program (called the “source” code) written in some programming language to its equivalent program (called “executable”, or “object” code) in the machine language of a target computer. Categories of Program Translators: a. Compilers It converts an entire source code to the equivalent object or executable code in one session. No object code is produced (hence, no output is executed) if an error is detected during the process. b. Interpreters It translates the source code into object/executable code “on the fly”, usually one program statement at a time. Since the conversion is done one statement at a time, execution of the program stops until the end of the program is read or an error has been detected. c. Assemblers It converts assembly language to the machine language equivalent. This can be likened to a compiler for assembly language. 4. The Program Development Life Cycle Programmers do not sit down and start writing code right away when trying to make a computer program. Instead, they follow an organized plan or methodology, that breaks the process into a sequence of tasks. These tasks are Problem Definition, Problem Analysis, Algorithm Design and representation, Coding and Debugging. a. Problem Definition A programmer is usually given a task in the form of a problem. Before a program can be designed to solve a particular problem, the problem must be well and clearly defined in terms of its input and output requirements. It may be said that a clearly defined problem is half the solution. Hence, computer programming entails defining the problem first before creating a solution. b. Problem Analysis After the problem has been well-defined, the simplest and the most efficient and effective approach to solve the problem must be formulated. Usually, this step involves breaking up the problem into smaller and simpler sub-problems. Example Problem: Determine the number of times a name occurs in a list. Input to the program: list of names, name to look for. Output of the program: the number of times the name occurs in a list. Smaller problems. 1. Get the list of names 2. Get the name to be searched 3. Determine the number of times that the name to be searched is found in the list of names. 4. Display the result c. Algorithm design and representation Once our problem is clearly defined, we can now set to finding a solution. In computer programming, it is normally required to express our solution in a step-by-step manner. Computer Programming 1 SLU-SAMCIS 4|Page An algorithm is a clear and unambiguous specification of the steps needed to solve a problem. It may be expressed in either human language (English, Tagalog), through a graphical representation like a flowchart or through a pseudocode, which is a cross between human language and a programming language. For the problem on determining the number of occurrences of a certain name in a given list of names, the solution may be expressed as follows. Expressing our solution through Human language: 1. Get the list of names and place the list into a list called nameList 2. Get the name to look for, let's call this the keyName 3. Let count represent the number of occurrences of keyName in the list and let count be initialised to 0. 4. Starting from the first name in the list to the last name in the list, compare the keyName to each of the names in the list If the keyName is the same with a name in the list, add 1 to the count 5. Show the result The solution can be expressed using a flowchart: A flowchart is a picture of the separate steps of a process in sequential order. Although the flowchart is introduced here as a way to represent the design of a solution to a problem, the flowchart is a generic tool for a variety of purposes. A flowchart may be used to describe various processes, such as a service process, or a manufacturing process. In a flowchart, special shapes (i.e. flowchart symbols) are used to represent different types of actions or steps in a process. In addition, lines and arrows are used to show the sequence of the steps. The fundamental flowchart symbols are described in the following figure. Symbol Name Function Start/End An oval represents a start or end point A line is a connector that shows relationships between the Arrows representative shapes Input/Output A parallelogram represents input or output Process A rectangle represents a process Decision A diamond indicates a decision The solution can be expressed using a pseudocode: Let nameList = List of Names Let keyName = the name to be sought Let count = 0 For each name in nameList do the following if name == keyName count = count + 1 Display count Computer Programming 1 SLU-SAMCIS 5|Page The solution can be expressed using a flowchart. d. Coding and Debugging After constructing the algorithm, it is now possible to create the source code. Using the algorithm as basis, the source code can now be written using the chosen programming language. Most of the time, after the programmer has written the program, the program isn't 100% working right away. The programmer has to add some fixes to the program in case of errors (also called bugs) that occur in the program. This process is called debugging. There are two types of errors that a programmer will encounter along the way. The first one is compile-time error, and the other is runtime error. Compile-Time Errors occur if there is a syntax error in the code. The compiler will detect the error and the program won't even compile. At this point, the programmer is unable to form an executable file that a user can run until the error is fixed. Forgetting a semi-colon at the end of a statement or misspelling a certain command, for example, is a compile- time error. It's something the compiler can detect as an error. Compilers aren't perfect and so can't catch all errors at compile time. This is especially true for logic errors such as a wrong condition that causes a nonterminating loop. This type of error is called runtime error. For example, the actual syntax of a code can be correct but the code's logic results to an infinite repetition of a loop. In such a case, the compiler does not detect the error at compile-time. Therefore, the program compiles fine into an executable file. Unfortunately, when the user runs the program, the program causes the computer to freeze due to an infinite loop. Usually, logical errors that are run-time errors are incorrect computed values because of wrong expressions. Computer Programming 1 SLU-SAMCIS 6|Page Shown below are additional examples of a flowchart and a pseudocode. Flowchart: Pseudocode: 1. Count money 2. If money > 100.00 Buy an item Go to step 1 else Go home Review Exercise (May not be submitted) Using Pseudocodes, present an algorithm to accomplish each of the following tasks. 1. Baking your favorite homemade cake 2. Accessing a course material from a Google classroom 3. Getting the average of three numbers Upload a file in pdf that contains your result for one of the tasks (ONE only) in the Submission Box for Formative Exercise 1 in the Google classroom. 5. Programming Environment/Platform The process of programming involves the use of set of tools/programs. Listed below are some of the common tools that are used. editors, translators (compilers/interpreters/assemblers), linkers, loaders, debuggers, built-in libraries, profilers, documenters, etc. Computer Programming 1 SLU-SAMCIS 7|Page Programming platform – tools Editor Object Code Compiler Source Code Linker Loader 010001 100010 010001 Executable Compiled 010101 Memory 000010 Image Libraries 001001 110010 Debugger Runtime Status and Debug Info Other Tools Documentation, Program Profiles, etc. Categories of programming environments: Command line interface vs graphical interface Stand-alone vs. integrated development environments (IDEs) Computer Programming 1 SLU-SAMCIS 8|Page Programming Platform – Command Line Tools [Windows PowerShell] D:\> nano HelloWorld.java ┌────────── File: HelloWorld.java ──────────┐ │import java.lang.*; │ │ │ │public class HelloWorld { │ │ public static void main(String[] args) { │ │ System.out.println("Hello, World!"); │ │ } │ │} │ └───────────────────────────────────────────┘ D:\> dir/b HelloWorld.java D:\> javac HelloWorld.java D:\> dir/b HelloWorld.java HelloWorld.class D:\> java HelloWorld Hello, World! Integrated Development Environments (IDE) Tools [Editor with Compiler + Linker + Debugger + …] Examples: JCreator, Visual Studio, Netbeans, IntelliJ IDEA, etc. JCreator Computer Programming 1 SLU-SAMCIS 9|Page Visual Studio NetBeans Computer Programming 1 SLU-SAMCIS 10 | P a g e IntelliJ IDEA Visual Studio Code is not an IDE but a code editor which supports other basic development operations such as task running, debugging, and version control. It provides the tools that a developer needs for a quick code-build-debug cycle. Visual Studio Code Computer Programming 1 SLU-SAMCIS 11 | P a g e