C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 1: An Overview of Computers and Programming Languages
Introduction Without software, the computer is useless Software developed with programming languages –C++ is a programming language C++ suited for a wide variety of programming tasks Before programming, it is useful to understand terminology and computer components C++ Programming: From Problem Analysis to Program Design, Fifth Edition2
Elements of a Computer System Hardware CPU Main memory Secondary storage Input/Output devices Software C++ Programming: From Problem Analysis to Program Design, Fifth Edition3
Hardware CPU Main memory: RAM Input/output devices Secondary storage C++ Programming: From Problem Analysis to Program Design, Fifth Edition4
Central Processing Unit and Main Memory Central processing unit –Brain of the computer –Most expensive piece of hardware –Carries out arithmetic and logical operations C++ Programming: From Problem Analysis to Program Design, Fifth Edition5
Central Processing Unit and Main Memory (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition6
Central Processing Unit and Main Memory (cont'd.) Random access memory Directly connected to the CPU All programs must be loaded into main memory before they can be executed All data must be brought into main memory before it can be manipulated When computer power is turned off, everything in main memory is lost C++ Programming: From Problem Analysis to Program Design, Fifth Edition7
Secondary Storage Secondary storage: device that stores information permanently Examples of secondary storage: –Hard disks –Flash drives –Floppy disks –CD-ROMs –Tapes C++ Programming: From Problem Analysis to Program Design, Fifth Edition8
Input/Output Devices Input devices feed data and programs into computers –Keyboard –Mouse Output devices display results –Monitor –Printer C++ Programming: From Problem Analysis to Program Design, Fifth Edition9
Software Software: programs that do specific tasks System programs take control of the computer, such as an operating system Application programs perform a specific task C++ Programming: From Problem Analysis to Program Design, Fifth Edition10
The Language of a Computer Digital signals: sequences of 0s and 1s Machine language: language of a computer Binary digit (bit): –The digit 0 or 1 Binary code: –A sequence of 0s and 1s Byte: –A sequence of eight bits C++ Programming: From Problem Analysis to Program Design, Fifth Edition11
The Language of a Computer (cont’d.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition12
The Language of a Computer (cont'd.) ASCII (American Standard Code for Information Interchange) –128 characters –A is encoded as (66th character) –3 is encoded as C++ Programming: From Problem Analysis to Program Design, Fifth Edition13
The Language of a Computer (cont'd.) EBCDIC –Used by IBM –256 characters Unicode –65536 characters –Two bytes are needed to store a character C++ Programming: From Problem Analysis to Program Design, Fifth Edition14
The Evolution of Programming Languages Early computers were programmed in machine language To calculate wages = rates * hours in machine language: //Load //Multiply //Store C++ Programming: From Problem Analysis to Program Design, Fifth Edition15
The Evolution of Programming Languages (cont'd.) Assembly language instructions are mnemonic Assembler: translates a program written in assembly language into machine language C++ Programming: From Problem Analysis to Program Design, Fifth Edition16
Processing a C++ Program #include using namespace std; int main() { cout << "My first C++ program." << endl; return 0; } Sample Run: My first C++ program. C++ Programming: From Problem Analysis to Program Design, Fifth Edition17
Processing a C++ Program (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition18
Programming with the Problem Analysis–Coding–Execution Cycle Programming is a process of problem solving One problem-solving technique: –Analyze the problem –Outline the problem requirements –Design steps (algorithm) to solve the problem Algorithm: –Step-by-step problem-solving process –Solution achieved in finite amount of time C++ Programming: From Problem Analysis to Program Design, Fifth Edition19
The Problem Analysis–Coding– Execution Cycle (cont’d.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition20
The Problem Analysis–Coding– Execution Cycle (cont'd.) Run code through compiler If compiler generates errors –Look at code and remove errors –Run code again through compiler If there are no syntax errors –Compiler generates equivalent machine code Linker links machine code with system resources C++ Programming: From Problem Analysis to Program Design, Fifth Edition21
The Problem Analysis–Coding– Execution Cycle (cont'd.) Once compiled and linked, loader can place program into main memory for execution The final step is to execute the program Compiler guarantees that the program follows the rules of the language –Does not guarantee that the program will run correctly C++ Programming: From Problem Analysis to Program Design, Fifth Edition22
Example 1-1 Design an algorithm to find the perimeter and area of a rectangle The perimeter and area of the rectangle are given by the following formulas: perimeter = 2 * (length + width) area = length * width C++ Programming: From Problem Analysis to Program Design, Fifth Edition23
Example 1-1 (cont'd.) Algorithm: –Get length of the rectangle –Get width of the rectangle –Find the perimeter using the following equation: perimeter = 2 * (length + width) –Find the area using the following equation: area = length * width C++ Programming: From Problem Analysis to Program Design, Fifth Edition24