Programming Fundamentals 4-Jan-24 Chapter 1: Introduction to Computers, Programs, and C++ Sections 1.1 - 1.3, 1.6 - 1.9 Textbooks: Y. Daniel Liang, Introduction to Programming with C++, 3rd Edition © Copyright 2016 by Pearson Education, Inc. All Rights Reserved. Faculty of Computer Science and Engineering 1 Programming Fundamentals 4-Jan-24 Outline • • • • • • Introduction and Computers (§§1.1–1.2) Programming languages (§§1.3) A simple C++ program for console output (§1.6) C++ program-development cycle (§1.7) Programming style and documentation (§1.8) Programming errors (§1.9) Faculty of Computer Science and Engineering 2 Programming Fundamentals 4-Jan-24 What is a Computer? A computer consists of a CPU, memory, hard disk, monitor, and communication devices. Bus Storage Devices Memory e.g., Disk, CD, and Tape Faculty of Computer Science and Engineering CPU Communication Devices Input Devices Output Devices e.g., Modem, and NIC e.g., Keyboard, Mouse e.g., Monitor, Printer 3 Programming Fundamentals 4-Jan-24 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 Core i7 Processor at 3 gigahertz (1 gigahertz is 1000 megahertz). Bus Storage Devices Memory e.g., Disk, CD, and Tape Faculty of Computer Science and Engineering CPU Communication Devices Input Devices Output Devices e.g., Modem, and NIC e.g., Keyboard, Mouse e.g., Monitor, Printer 4 Programming Fundamentals 4-Jan-24 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. Bus Storage Devices Memory e.g., Disk, CD, and Tape Faculty of Computer Science and Engineering CPU Communication Devices Input Devices Output Devices e.g., Modem, and NIC e.g., Keyboard, Mouse e.g., Monitor, Printer 5 Programming Fundamentals 4-Jan-24 How Data is Stored? • Data of various kinds are encoded as a series of bits (zeros and ones). • The encoding scheme varies. For example, character ‘J’ is represented by 01001010 in one byte. • A small number such as 3 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. • A byte is the minimum storage unit. Faculty of Computer Science and Engineering 6 Programming Fundamentals 4-Jan-24 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 four main types of storage devices: Disk drives (hard disks), Solid-state devices (SSD, Flash), CD drives (CD-R and CD-RW), and Tape drives. Bus Storage Devices Memory e.g., Disk, CD, and Tape Faculty of Computer Science and Engineering CPU Communication Devices Input Devices Output Devices e.g., Modem, and NIC e.g., Keyboard, Mouse e.g., Monitor, Printer 7 Programming Fundamentals 4-Jan-24 Outline • • • • • • Introduction and Computers (§§1.1–1.2) Programming languages (§§1.3) A simple C++ program for console output (§1.6) C++ program-development cycle (§1.7) Programming style and documentation (§1.8) Programming errors (§1.9) Faculty of Computer Science and Engineering 8 Programming Fundamentals 4-Jan-24 Programs Computer programs, known as software, are instructions to the computer. You tell a computer what to do through programs. Without programs, a computer is an empty machine. Computers do not understand human languages, so you need to use computer languages to communicate with them. Programs are written using programming languages. Faculty of Computer Science and Engineering 9 Programming Fundamentals 4-Jan-24 Programming Languages Machine Language Assembly Language Machine language is a set of primitive instructions built into every computer. The instructions are in the form of binary code, so you have to enter binary codes for various instructions. Program with native machine language is a tedious process. Moreover, the programs are highly difficult to read and modify. For example, to add two numbers, you might write an instruction in binary like this: 1101101010011010 ADD 42 Faculty of Computer Science and Engineering 26 High-Level Language Early computer systems used to punch the 0’s and 1’s on special cardboard paper (punch cards) Imagine making a single mistake and you have to do it all again! Impractical e.g., computer designers will agree what operation the first 4 bits indicate 10 Programming Fundamentals 4-Jan-24 Programming Languages Machine Language Assembly Language Assembly languages were developed to make programming a bit easier. Instead of writing programs in 0’s and 1’s, we write the program in terms of the desired operation (ADD, SUB, MOV), the numerical numbers, memory locations, etc. High-Level Language For example, to add two numbers, you might write an instruction in assembly code like this: add 2, 3, result Since the computer cannot understand assembly language, a program called assembler is used to convert assembly language programs back into machine code. Faculty of Computer Science and Engineering 11 Programming Fundamentals 4-Jan-24 Programming Languages Machine Language Assembly Language ▪ ▪ ▪ ▪ ▪ High-Level Language There are dozens of computer processor types: INTEL/AMD (found mostly in PCs, laptops, servers), ARM (found mostly in tablets, phones, some laptops), MIPS, RISC-V, etc. Each has a different design→ different machine code → different assembly language. Imagine learning different assembly languages to develop a program that runs on all these different computers. IMPRACTICAL! Solution: high-level languages written once, and translated to different assembly languages as needed (one program → many machines) The high-level languages are English-like and easy to learn and program. For example, the following is a high-level language statement that computes the area of a circle with radius 5: area = 5 * 5 * 3.1416; Faculty of Computer Science and Engineering 12 Programming Fundamentals 4-Jan-24 High-Level Languages Old High-Level Languages: COBOL (COmmon Business Oriented Language), FORTRAN (FORmula TRANslation), BASIC (Beginner All-purpose Symbolic Instructional Code), Pascal, Ada, Delphi Popular High-Level Languages: • C (used a lot in hardware programming / similar to C++) • Visual Basic (Basic-like visual language developed by Microsoft) • C++ (an object-oriented language, based on C) • Java (a popular object-oriented language, similar to C++) • C# (a Java-like developed my Microsoft) • MATLAB (oriented for engineering and scientific applications) • Python (used widely in data analysis and artificial intelligence ) • JavaScript (front-end/back-end, and mobile development) • Also: Go, Ruby, Swift, Objective-C, Rust, Scala, Perl Faculty of Computer Science and Engineering 13 Programming Fundamentals 4-Jan-24 From High-Level to Machine Code: Compiling versus Interpretation • Some programming languages like Python/MATLAB have interpreters that translate and execute a program line by line as long as they are correct as seen in Fig. (a). Think of it as being in a conference room, and someone is translating the speech from English to Arabic as the person speaks. • C++ needs a compiler that translates the entire source program into a machinelanguage file for execution Fig. (b). All the program must be in machine code before execution. Think of it as giving an English document to a translator and they hand you the Arabic version. Faculty of Computer Science and Engineering 14 Programming Fundamentals 4-Jan-24 Hierarchy of Programming Paradigms https://github.com/chrislgarry/Apollo-11/ Faculty of Computer Science and Engineering 15 Programming Fundamentals 4-Jan-24 POP vs OOP Procedural Oriented Programming Faculty of Computer Science and Engineering Object Oriented Programming 16 Programming Fundamentals 4-Jan-24 Outline • • • • • • Introduction and Computers (§§1.1–1.2) Programming languages (§§1.3) A simple C++ program for console output (§1.6) C++ program-development cycle (§1.7) Programming style and documentation (§1.8) Programming errors (§1.9) Faculty of Computer Science and Engineering 17 Programming Fundamentals 4-Jan-24 Very brief history of C++ Quote: “C++ was designed to provide Simula’s facilities for program organization together with C’s efficiency and flexibility for systems programming. It was intended to deliver that to real projects within half a year of the idea. It succeeded.” For details more check out A History of C++: 1979−1991 Faculty of Computer Science and Engineering 18 Programming Fundamentals 4-Jan-24 Features of C++ Faculty of Computer Science and Engineering 19 19 Programming Fundamentals 4-Jan-24 A Simple C++ Program Let us begin with a simple C++ program that displays the message “Welcome to C++!” on the console. #include <iostream> using namespace std; int main() { // Display Welcome to C++ to the console cout << "Welcome to C++!" << endl; return 0; } Note: Clicking the green button displays the source code with interactive animation and live run. Internet connection is needed for this button. Welcome Run Note: Clicking the blue button runs the code from Windows. To enable the buttons, you must download the entire slide file slide.zip and unzip the files into a directory (e.g., c:\slide). If you are using Office 2010 or higher, check PowerPoint2010.doc located in the same folder with this ppt file. Faculty of Computer Science and Engineering 20 Programming Fundamentals 4-Jan-24 Special Characters in C++ Faculty of Computer Science and Engineering 21 Programming Fundamentals 4-Jan-24 Comments in C++ // This is a comment cout << "Hello World!"; cout << "Hello World!"; // This is a comment /* The code below will print the words Hello World! to the screen, and it is amazing */ cout << "Hello World!"; Faculty of Computer Science and Engineering 22 Programming Fundamentals 4-Jan-24 Extending the Simple C++ Program Once you understand the program, it is easy to extend it to display more messages. For example, you can rewrite the program to display three messages. #include <iostream> using namespace std; int main() { cout << "Programming is fun!" << endl; cout << "Fundamentals First" << endl; cout << "Problem Driven" << endl; return 0; } Run WelcomeWithThreeMessages Faculty of Computer Science and Engineering 23 Programming Fundamentals 4-Jan-24 Computing with Numbers Further, you can perform mathematical computations and displays the result to the console. Listing 1.3 gives such an example. #include <iostream> using namespace std; int main() { cout << "(10.5 + 2 * 3) / (45 - 3.5) = "; cout << (10.5 + 2 * 3) / (45 - 3.5) << endl; return 0; } Faculty of Computer Science and Engineering ComputeExpression Run 24 Programming Fundamentals 4-Jan-24 Outline • • • • • • Introduction and Computers (§§1.1–1.2) Programming languages (§§1.3) A simple C++ program for console output (§1.6) C++ program-development cycle (§1.7) Programming style and documentation (§1.8) Programming errors (§1.9) Faculty of Computer Science and Engineering 25 Programming Fundamentals 4-Jan-24 The C++ Toolchain • From the moment you write your C++ program to the moment it executes; your program passes through many steps. • At each step, a certain tool does something towards the process of converting your program from a high-level language to an executable program. • This sequence of tools is called a toolchain. • Most of the time, the toolchain comes with your IDE (e.g., Microsoft Visual Studio), or needs to be downloaded separately and configured to work with your IDE. • Expert Programmers can configure the fine details of the tool chain (e.g., choose each tool, which version, other options). • In any C++ IDE, pressing the build button starts the toolchain. Faculty of Computer Science and Engineering 26 Programming Fundamentals 4-Jan-24 The C++ Toolchain Main Programs A program written in a high-level language is called a source program. It has #include statements and may contain comments. • The preprocessor strips out user comments and does replacements for the include statements. • A program called a compiler is used to translate the preprocessed source program into the assembly code of the target machine. • A program called the assembler then translates the assembly code to a machine language program called an object program. • The object program is often then linked using a linker with other supporting library code before the object can be executed on the machine. Faculty of Computer Science and Engineering 27 Programming Fundamentals 4-Jan-24 The Compilation Process Faculty of Computer Science and Engineering 28 Programming Fundamentals 4-Jan-24 Detailed Tool Chain Steps (1) Assembly Code Faculty of Computer Science and Engineering 29 Programming Fundamentals 4-Jan-24 Detailed Toolchain Steps (2) Assembly Code Assembler Faculty of Computer Science and Engineering 30 Programming Fundamentals 4-Jan-24 C++ IDE Tutorial You can develop a C++ program from a command window or from an IDE. An IDE is software that provides an integrated development environment (IDE) for rapidly developing C++ programs. Editing, compiling, building, debugging, and online help are integrated in one graphical user interface. Just enter source code or open an existing file in a window, then click a button, menu item, or function key to compile and run the program. Examples of popular IDEs are Microsoft Visual Studio, Dev-C++, Eclipse, CodeBlocks, CLion, and NetBeans. All these IDEs can be downloaded for free or with a free student license. IDEs might use different toolchains. For example, Microsoft Visual Studio has their own compiler. CodeBlocks/CLion/Our server use GCC. GCC/G++ is one of the world’s most famous compilers. Faculty of Computer Science and Engineering 31 Programming Fundamentals 4-Jan-24 Flowchart symbols Terminal Input/output Process Flowlines Decision Connector Predefined process Faculty of Computer Science and Engineering 32 Programming Fundamentals 4-Jan-24 Algorithms in pseudo-code You also can use English-like phases to describe an algorithm. In this case, the description is called pseudocode. Start Input Name, Hours, Rate Calculate Pay Hours Rate Example: • Input the three values into the variables Name, Hours, Rate • Calculate Pay = Hours * Rate • Display Name and Pay Faculty of Computer Science and Engineering Dislay Name, Pay End 33 Programming Fundamentals 4-Jan-24 Outline • • • • • • Introduction and Computers (§§1.1–1.2) Programming languages (§§1.3) A simple C++ program for console output (§1.6) C++ program-development cycle (§1.7) Programming style and documentation (§1.8) Programming errors (§1.9) Faculty of Computer Science and Engineering 34 Programming Fundamentals 4-Jan-24 Programming Style and Documentation • Appropriate Comments • Proper Indentation and Spacing Lines • Block Styles #include <iostream> using namespace std; int main() { cout << "(10.5 + 2 * 3) / (45 - 3.5) = "; cout << (10.5 + 2 * 3) / (45 - 3.5) << endl; return 0; } Faculty of Computer Science and Engineering 35 Programming Fundamentals 4-Jan-24 Outline • • • • • • Introduction and Computers (§§1.1–1.2) Programming languages (§§1.3) A simple C++ program for console output (§1.6) C++ program-development cycle (§1.7) Programming style and documentation (§1.8) Programming errors (§1.9) Faculty of Computer Science and Engineering 36 Programming Fundamentals 4-Jan-24 Programming Errors 1. Syntax Errors 2. Runtime Errors 3. Logic Errors Faculty of Computer Science and Engineering 37 Programming Fundamentals 4-Jan-24 Syntax Errors ShowSyntaxErrors Faculty of Computer Science and Engineering 38 Programming Fundamentals 4-Jan-24 Runtime Errors ShowRuntimeErrors Faculty of Computer Science and Engineering Run 39 Programming Fundamentals 4-Jan-24 Logic Errors ShowLogicErrors Faculty of Computer Science and Engineering Run 40 Programming Fundamentals 4-Jan-24 Common Errors 1. 2. 3. 4. Missing Braces Missing Semicolons Missing Quotation Marks Misspelling Names Faculty of Computer Science and Engineering 41 Programming Fundamentals 4-Jan-24 Outline • • • • • • Introduction and Computers (§§1.1–1.2) Programming languages (§§1.3) A simple C++ program for console output (§1.6) C++ program-development cycle (§1.7) Programming style and documentation (§1.8) Programming errors (§1.9) Faculty of Computer Science and Engineering 42