COMPUTER PROGRAMMING COURSE 66111 DR. EMAD NATSHEH Adapted from the slides originally prepared by Dr. Luai Malhis, Eng. Muhannad Al-Jabi, Eng. Haya Samma’aneh, & Dr. Samer Arandi Part 1 Introduction Numbering Systems First C++ Program What is the computer? The computer is a programmable machine that receives input, manipulates and stores data, and produces output in a useful format. How does the computer work? The computer works as a result of the interaction of two categories of components: Software Components: programs which contain Instructions that command the computer to perform actions. Hardware components: Physical components of the computer. Computer Organization (Hardware) Most computers have six basic components: Input devices Output devices Memory unit Arithmetic Logic Unit Control Unit Secondary storage unit Computer Organization (Cont.) Input unit: Obtains information from input devices Keyboard, mouse, microphone, scanner, networks, etc Output unit Places information processed by computer on output devices Screen, printer, etc. Computer Organization (Cont.) Memory unit (Primary) Internal data storage in a computer An ordered sequence of storage cells Each memory cell has a distinct location Rapid access, relatively low capacity Holds data (input data or the product of computation), and instructions (programs) Secondary Storage Slower to access than primary memory High-capacity (warehouse) section Stores inactive programs or data Less expensive per unit than primary memory Secondary storage devices Hard drives, CDs, DVDs Computer Organization (Cont.) Central processing unit (CPU) Part of computer that executes instructions Consists of: Arithmetic and Logical Unit (ALU) Performs arithmetic calculations (addition, subtraction, etc) and logic decisions (determining if a number is greater, less, or equal to other number, etc) Control Unit (CU): manages the action of the other components so that program instructions execute in the correct order. A set of registers The CPU has its own small workspace, consisting of several registers, each of which can hold a number. More on the CPU The CPU fetches the program instructions from memory and executes them. The memory also holds the input/output data of the program. A specific register holds the memory address of the program instructions. The CPU uses this address to fetch the program instructions for execution, consecutively. A group of registers holds the input and output data values (operands) during the execution of the instruction by the ALU Input operands are typically fetched from memory before execution and stored back to memory after execution Computer Software Software: programs that enable the hardware to perform different tasks Types of software: System software: essential for computer operation and support (e.g. operating systems: Windows, Linux, Mac) Application software Tools used to accomplish specific tasks. (e.g. image viewers, word processors, games, scientific applications, etc) Programming languages Machine language (Low Level Language):The language that the computer directly understand. Generally consists of 0s and 1s. Almost impossible to understand by humans. Assembly language (Low-level Language): cannot be understood by the computer, it must be converted to machine language by translator programs called assemblers Programming languages High level languages: C++, Java, C#, FORTRAN, etc. Similar to everyday English. Uses common mathematical notations. Single statements to do multiple tasks Converted to machine language by translator programs also called Compilers Example code of high level language. z=a+b Bits and Bytes: Data representation Bit (binary digit): is the basic unit of information in computing. It is the smallest unit of storage that can be either 0 or 1 Byte : 8 bits. Amount of data Numbering systems Decimal --10 symbols (0,1,2,3,4,5,6,7,8,9) Binary --2 symbols (0,1) Octal --8 symbols (0,1,2,3,4,5,6,7) Hexadecimal --16 symbols (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F) A = 10 , B = 11 ….F = 15 Decimal numbering system Decimal numbers are made of decimal digits: (0,1,2,3,4,5,6,7,8,9) But how many items does a decimal (base 10) number represent? 115 = 1x102 + 1x101 + 5x100 What about fractions? 97654.35 = 9x104 + 7x103 + 6x102 + 5x101 + 4x100 + 3x10-1 + 5x10-2 In formal notation (97654.35)10 Binary numbering system Binary numbering system, or base-2 number system, represents numeric values using two symbols, 0 and 1. Binary system is used internally to represent data by all computers. Converting from binary to decimal Convert (11001)2 to the decimal system (11001)2= 1x24+1x23+ 0x22 + 0x21 +1x20 = 16 + 8 + 0+ 0 +1 = (25)10 The sum of the last row = (25)10 Converting from decimal to binary Divide the decimal number by 2. Put the division result in the next row under the number. Put the division remainder next to the number in the same row. Repeat the 3 steps for the division result until the division result is zero. Octal numbering System Octal numbers are made of octal digits: (0,1,2,3,4,5,6,7) Octal numbers don’t use digits 8 or 9 Note that each octal digit can be represented with three bits. Converting from octal to decimal Convert (357)8 to the decimal system (357)8 = 3x82 + 5x81 +7x80 = 192 + 40 +7 = (239)10 The sum of the last row = (239)10 Converting from decimal to octal Divide the decimal number by 8. Put the division result in the next row under the number. Put the division remainder next to the number in the same row. Repeat the 3 steps for the division result until the division result is zero. (239)10 = (357)8 Converting from octal to binary Each digit in the octal number is represented by 3 digits in binary. Example : (63)8 = (110011)2 Converting from binary to octal Start from right to left, create groups of digits, such that, each group contains three digits only. If the last group contains less than 3 digits, add zeros to the left. Write down the equivalent symbol for each group. Example : (010 101 110)2 =(256)8 Hexadecimal numbering system Each digit in hexadecimal number contains one of the following symbols: (0,1,2,3,4,5,6,7,8,9,A, B, C, D, E, F) Note that each hexadecimal digit can be represented with four bits. (1110) 2 = (E)16 Converting from hexadecimal to decimal Convert (3A9F)16 to the decimal system (3A9F)16= 3x163 + 10x162 +9x161 +15x160 = 12288 + 2560+144+15 = (15007)10 The sum of the last raw = (15007)10 Converting from decimal to hexadecimal Divide the decimal number by 16. Put the division result in the next row under the number. Put the division remainder next to the number in the same row represented in hexadecimal. Repeat the 3 steps for the division result until the division result is zero. (171)10 = (AB)16 Converting from hexadecimal to binary Each digit in the hexadecimal number is represented by 4 digits in binary. Example : (63)16= (01100011)2 Converting from binary to hexadecimal Start from right to left, create groups of digits, such that, each group contains four digits only. If the last group contains les than 4 digits, add zeros to the left. Write down the equivalent symbol for each group (you can use the table on the previous slide) Example : (000101011101)2=(15D)16 Converting between octal and hexadecimal There is no direct way to convert between the two systems. So we can either: Convert the number to decimal then to the desired system Convert the number to binary then to the desired system The second method is much easier because it avoids the mathematical operations needed by the first method. What do we mean by a program? It is a set of ordered instructions that performs a certain task. Instruction A is executed before instruction B, as long as instruction A is located before instruction B. For example, some instructions of your daily program:Wake up Dressing Go to school Stages of program development Program execution and testing Program loading Program linking Program compilation Program writing Program design Problem statement Problem Statement What is the problem? Why the program is needed? What is the scope and limitations (in time, and accuracy) that can be used to solve the problem? Program Design Find a suitable algorithm to solve the problem. One way to represent the algorithm is by drawing a Flowchart of that algorithm. What does it mean? Flowchart example 1 C/C++ Programming Language History of C Evolved from two other programming languages BCPL and B “Typeless” languages Dennis Ritchie (Bell Laboratories) Added data types and other features Hardware independent Portable programs 1989: ANSI standard The C Language was extended to contain classes and other Object Oriented features and named as C++. Many Other Languages currently developed uses syntax and symantics similar to C. C/C++ is traditionally the first language a programmer learns. Program writing When you write a C++ program you store what you write in a text file called a source code file or simply a source file. The name of the file ends in .cpp (example: program.cpp) The part of the name before the period is called the base name, and the part after the period is called the extension. Program writing We will use Microsoft Visual Studio 6.0. It can be used for : Program writing Program compilation & linking Program execution and testing. How to start writing a program? After installing Microsoft Visual Studio C++ 6.0 Go to all programs menu from desktop. Select Microsoft Visual Studio 6.0. Select Microsoft Visual C++ 6.0 How to start writing a program? From file menu select new. Select the projects tab. Select the win32 console application Select Empty Project How to start writing a program? How to start writing a program? From the file menu select new. Select the file tab. Select the C++ source file option. How to start writing a program? Example #1 Write a C++ program that prints hello on the screen. Example #1 … Hello World Compile a C++ program To compile a C++ program, press ctrl+F7 If there are no errors, press ctrl+F5 to run the program. How to find compilation errors In the window below the source code editing page. Find the error(s) using the right slide bar When you double click on an error, a notification arrow will point to the source code line that contains the error. Solve the first error first, then the second and so on. Programming errors Syntax errors: violation of the syntax ( grammar rules ) of the programming language Run-time errors: errors detected when the program is run errors that prevent your program from running The compiler gives an error message if the program contains a syntax error Most compiler errors are caused by mistakes that you make when typing code. For example, you might misspell a keyword, leave out some necessary punctuation, or try to use an End If statement without first using an If statement. The system usually gives an error message during execution An example of this is division by zero Logic errors: program compiles and runs normally, but does not perform properly. Caused by an error in the logic of the program Comments Important part of the program Non-executable (not compiled) statements Describe the purpose of the program or parts of the program Indicated by: the double slash // everything to the right of the double slash until the end of the line is ignored the slash asterisk combination /* everything between the enclosing and ending symbols is ignored, typically used for comments spanning multiple lines */ Provide documentation More Special Characters