Topic 1: Programming concepts Learning Outcomes Upon successful completion of this topic you will be able to: identify stages of a program development implement algorithm design techniques break down a problem into logical blocks describe different types of programming languages use pseudocode and flowchart to represent a complete solution describe and use a C++ IDE describe basic blocks of a C++ program. Introduction to the Topic Computers are instruments used by people to make their work performance faster and more accurate. Computers do not have an internal intelligence. Computers need programs, written by people, to tell them what to do and how to do it. In this topic you will learn how to analyse a program and write instructions (programs) that a computer understands. You will be introduced to C++, one of the most popular programming languages, and an Integrated Development Environment (Microsoft Visual Studio). Background Skills and Knowledge We assume that you understand basic computer operations, can use text editors and a file system. 1.1 Programming concepts and algorithm design techniques A computer program is a set of instructions that directs the CPU (Central Processing Unit) to perform some operations, like EEET 2280 Computing Engineering 27
adding two numbers, displaying a pop-up window, or playing a DVD. Program development always starts with analysing a problem and creating an algorithm of the solution. The algorithm describes all steps of the program s execution and their order. You can represent these steps using simple words or special symbols. The former presentation is called a pseudocode, and the latter a flowchart. You can write an algorithm for any task or procedure. There are four steps you need to follow when designing an algorithm: 1. Define the expected output 2. Work out the logic to get the output 3. Develop a pseudocode or flowchart of the algorithm. 4. Test the algorithm A programmer who uses structured programming divides a problem into smaller sub - problems or modules, and develops an algorithm of the solution for each of these modules. The complete solution is then created by combining individual algorithms. This approach is also called top-down design, or modular programming. You will find examples of structured algorithms designed using pseudocode and/or flowcharts, in the lecture notes: Reading 1 Read Herbert (2004), C++ A Beginner s Guide, pp 2 12 Deitel (2001), C++ How to Program, pp 2 15 Etter (2008), Engineering Problem Solving with C++, pp 3-30 EEET 2280 Computing Engineering 28
Activity 1.1 A Discussion/Problem solving In groups of 3 to 4 students, create an algorithm for the following tasks / procedures: 1. Making a cup of tea 2. Sending an SMS from your mobile 3. Calculating the average age of the students in your group 4. Finding the total resistance of three resistors connected in parallel. a. The program should ask a user to enter values for the resistors from the keyboard, and then display the result. b. To enhance your algorithm, continue requesting the values until the user terminates the program is a pre-defined way. Figure 1: Parallel connection of three resistors RMIT University, 2008 EEET 2280 Computing Engineering 29
1.2 Procedural and object-oriented programming C and C++ programming languages Information is stored and processed inside a computer as sequences of binary digits. These sequences consist of 1 s and 0 s, called bits, which form binary codes. A sequence of 8 binary digits makes a byte. A machine language provides instructions in bits. Assembly languages use mnemonics instructions that written in ordinary words like LOAD, ADD, etc., and a special program called assembler that translates them into binary codes. High-level programming languages use instructions that look more like natural language. Basic, FORTRAN, COBOL, Pascal, C, C++, and Java are high-level languages. C++ and Java originate from C. A C program has all the benefits provided by any high-level language, but can be executed much faster. In the 1980s C became the most popular structured programming language. C++ adds to the C language capabilities of objectoriented programming. Object-oriented design is based on identifying a problem s components (objects) and describing how these objects interact with each other. In this course you will initially use structured programming techniques to develop a C++ program. C++ programs written in this way can be run as C programs with a few modifications of the source code. From Topic 10 you will be introduced to the object-oriented features of this programming language. A program written in C++ is called a source code and, in a Windows environment is usually stored as.cpp file. You can use an ordinary text editor to write a C++ program. In this case you also need on your computer a special program called a compiler to translate the program into machine codes. The compiler program automatically runs a pre-processor program that performs pre-processor directives. In the final step you will build the program which includes calling a linker that connects the program code with C++ or other required libraries, and produces the executable file. The executable file usually has the same name as the source file and extension.exe. A set of programs required for the whole process depends on the platform, or operating system. EEET 2280 Computing Engineering 30
Activity 1.2 A Discussion/Research Using reference books or the Internet, answer the following questions: 1 What are the advantages of object oriented design compared to structured programming? 2 Why are C and C++ considered better programming languages to use in processing of large amounts of data than, for example, Java or C#? 3 Create a flowchart that illustrates the process of problem solving using a program written in C++. Which part of this flowchart will look different when the program is written in Java? 1.3 Integrated Development Environment (IDE) A C++ Integrated Development Environment includes all programs described above in a single software package. There are several popular C++ IDEs; some of them are freeware that you can download from the Internet (Dev C++), and some are powerful and quite expensive software applications (MS Visual Studio). In this course we have provided you with instructions of how to use both, Dev C++ (Using Dev-C++.pdf), and MS Visual Studio C++ (Using MS Visual Studio.pdf) to create a C++ project. Your teacher/instructor will specify which IDE is preferable to use in your course. ( Using Dev-C++.pdf and Using MS Visual Studio.pdf are located within Course Documents in Blackboard) EEET 2280 Computing Engineering 31
Every C++ program will usually include the following: comments to declare the purpose of the program, function or line, headers to provide necessary information about functions or constants (for example, call the pre-processor directive #include) a using statement(s) to notify the compiler that items from a particular namespace will be used. (For example, the statement using namespace std; makes all items from this namespace available without additional referencing) a main() function where the program execution starts and ends. Each line of the program that ends with a semicolon is called a statement. Each statement is meant to perform some task. Syntax rules define which statements are legal to use. Semantic rules determine the meaning of a statement. Program documentation is a significant part of any computer program. In complex C++ applications comments, and also programming style and naming conventions, play an important role in the program documentation and maintenance. Activity 1.3 A Discussion/Research Using the file hello.cpp look at the program Hello C++ and read the explanation of what each line of the program means. hello.cpp is within Course Documents in Blackboard EEET 2280 Computing Engineering 32
Activity 1 B Laboratory experiment Create a new folder on your Home network drive called C++, and a sub-folder C++\Topic 1. Follow instructions in the Lecture notes 1_3 to create a new project using a preferred IDE and save it in this sub-folder Open a source file and type in the program Hello C++. Note: copying text from MS Word will cause compiling errors, because of various extra characters used in Word (like, new line, etc.). Compile the program. Correct any syntax/linking errors. Run the program and observe the output. Make one change in the code (create an error by, for instance, removing a semicolon, or misspelling a keyword, or deleting a reference to namespace etc.). Repeat Step 4 and document the compiler s message. Correct errors and compile it again, and run. Repeat the last step several times. Add comments to the line which starts with cout to indicate what this line does. Describe two ways in which you can add a comment. Additional exercises: Write a C++ program that displays on the screen in three lines the program code and title, your name, and your student ID. Follow instructions in the textbook [1] complete Project 1-1 page 24. EEET 2280 Computing Engineering 33
Summary and Outcome Checklist In this topic you have learnt algorithm development techniques, discovered differences between programming languages, familiarised yourself with C++ IDE and you have written your first C++ program. Tick the box for each statement with which you agree: I can identify stages of a program development I can implement algorithm design techniques I can break down a problem into logical blocks I can describe different types of programming languages I can use pseudocode and flowchart to represent a complete solution I am familiar with C++ IDE I can describe basic blocks of a C++ program Assessment This topic will be assessed as part of the Major Assessment task 1 (see Assessment for more detail). This aims to ensure understanding of key concepts prior to undertaking the end of the semester examination. EEET 2280 Computing Engineering 34