Programming Fundamental Introduction to Programming
Objectives Define the terminology of used in programming Explain the tasks performed by a programmer Understand the employment opportunities for programmers and software engineers Become aware of structured design methodologies
Objectives A typical C++ program development environment. Study variables How to declare How to use Become familiar with the basic components of a C++ program, including identifiers
Programming a Computer It is important to understand the relationship between the terms programs, programmers, and programming languages. Programs – The directions that humans give to computers Programmers – The people who create these directions Programming Languages – Special languages used by programmers to communicate directions to a computer Programs – Test 2013/2014
The Programmer’s Job Programmers help solve computer problems Employee or freelance Typical steps involved Meet with user to determine problem Convert the problem into a program Test the program Provide user manual help solve computer problems – Test 2012 Employee or freelance – Final 2013, 2014 Typical steps – Quiz 2013
What Traits Should a Software Developer Possess? Analytical Skills Communication Skills Creativity Customer-service skills Detail oriented Problem-solving skills Teamwork Technical skills
Employment Opportunities Computer software engineers: designs an appropriate solution to a user’s problem Computer programmer: codes a computer solution Coding is the process of translating a computer solution into a language a computer can understand Some positions call for both engineering and programming Computer software engineers – Test 2014 Computer programmer – Test 2013, 2014 Coding – Test 2014
Flowcharting
Flowcharting
Flowchart Example
Structured Programming Structured design: Dividing a problem into smaller subproblems Structured programming: Implementing a structured design The structured design approach is also called: Top-down (or bottom-up) design Stepwise refinement Modular programming
Implementation #include <iostream> using namespace std; int main() { const float KM_PER_MILE = 1.609; float miles, kms; cout << “Enter the distance in miles: “; cin >> miles; kms = KM_PER_MILE * miles; cout << “The distance in kilometers is “ << kms << endl; return 0; }
Testing, Execution, Debugging Common error sources Violations of grammar rules of the high level language Errors that occur during execution Errors in the design of the algorithm
Typical C++ Development Environment C++ systems generally consist of three parts: a program development environment, the language and the C++ Standard Library. C++ programs typically go through six phases: edit, preprocess, compile, link, load and execute.
Typical C++ Development Environment (Cont.) Phase 1 consists of editing a file with an editor program, normally known simply as an editor. Type a C++ program (source code) using the editor. Make any necessary corrections. Save the program. C++ source code filenames often end with the .cpp, .cxx, .cc or .C extensions, which indicate that a file contains C++ source code.
Typical C++ Development Environment (Cont.) Linux editors: vi and emacs. You can also use a simple text editor, such as Notepad in Windows, to write your C++ code. integrated development environments (IDEs) Provide tools that support the software-development process, including editors for writing and editing programs and debuggers for locating logic errors-errors that cause programs to execute incorrectly.
Typical C++ Development Environment (Cont.) Popular IDEs Microsoft® Visual Studio 2015 Community Edition NetBeans Eclipse Apple’s Xcode CodeLite Clion
Typical C++ Development Environment (Cont.) In phase 2, you give the command to compile the program. A preprocessor program executes automatically before the compiler’s translation phase begins (so we call preprocessing Phase 2 and compiling Phase 3). The C++ preprocessor obeys commands called preprocessing directives, which indicate that certain manipulations are to be performed on the program before compilation. These manipulations usually include (copy into the program file) other text files to be compiled, and perform various text replacements.
Typical C++ Development Environment (Cont.) In Phase 3, the compiler translates the C++ program into machine-language code-also referred to as object code.
Typical C++ Development Environment (Cont.) Phase 4 is called linking. The object code produced by the C++ compiler typically contains “holes” due to these missing parts. A linker links the object code with the code for the missing functions to produce an executable program. If the program compiles and links correctly, an executable image is produced.
Typical C++ Development Environment (Cont.) Phase 5 is called loading. Before a program can be executed, it must first be placed in memory. This is done by the loader, which takes the executable image from disk and transfers it to memory. Additional components from shared libraries that support the program are also loaded.
Typical C++ Development Environment (Cont.) Phase 6: Execution Finally, the computer, under the control of its CPU, executes the program one instruction at a time. Some modern computer architectures often execute several instructions in parallel.
Typical C++ Development Environment (Cont.) Problems That May Occur at Execution Time Programs might not work on the first try. Each of the preceding phases can fail because of various errors that we’ll discuss throughout this book. If this occurred, you’d have to return to the edit phase, make the necessary corrections and proceed through the remaining phases again to determine that the corrections fixed the problem(s). Most programs in C++ input or output data.
Typical C++ Development Environment (Cont.) Certain C++ functions take their input from cin (the standard input stream; pronounced “see-in”), which is normally the keyboard, but cin can be redirected to another device. Data is often output to cout (the standard output stream; pronounced “see-out”), which is normally the computer screen, but cout can be redirected to another device. When we say that a program prints a result, we normally mean that the result is displayed on a screen.
Typical C++ Development Environment (Cont.) Data may be output to other devices, such as disks, hardcopy printers or even transmitted over the Internet. There is also a standard error stream referred to as cerr. The cerr stream is used for displaying error messages.
Variables Give a name to a memory location Compiler accesses specific memory location when program uses a given variable Refer to objects in the program for which the value can change Declaration type variableName; // or type variableName = initializerExpression;
Variables Variables Declaration Examples: int age = 18; Can be either initialized or uninitialized… If variable is uninitialized Contents must be considered as “garbage value” Examples: int age = 18; double GPA = 3.25, credits; char letterGrade = ‘A’; bool ok, done = false;
Data Types Defines a set of values and operations that can be performed on those values integers positive and negative whole numbers, e.g. 5, -52, 343222 short, int, long represented internally in binary predefined constants in INT_MIN and INT_MAX
Data Types (con’t) Floating point (real) number has two parts, integral and fractional e.g. 2.5, 3.66666666, -0.000034, 5.0 float, double, long double stored internally in binary as mantissa and exponent 10.0 and 10 are stored differently in memory
Data Types (con’t) Boolean named for George Boole represent conditional values Return: true and false
Data Types (con’t) Characters represent individual character values E.g. ’A’ ’a’ ’2’ ’*’ ’”’ ’’ stored in 1 byte of memory special characters: escape sequences E.g. ’\n’ ’\b’ ’\r’ ’\t’ ‘\’’
string Class Strings not built-in, but come from library Classes extend C++ string literal enclosed in double quotes E.g.: “Enter speed:“ “ABC” “B” “true” “1234” #include <string> for using string identifiers, but not needed for literals
Identifiers (1 of 2) An identifier is the name of something that appears in a program Consists of letters, digits, and the underscore character (_) Must begin with a letter or underscore C++ is case sensitive NUMBER is not the same as number Two predefined identifiers are cout and cin Unlike reserved words, predefined identifiers may be redefined, but it is not a good idea
Identifiers (2 of 2) Legal identifiers in C++: first conversion payRate TABLE 1-1 Examples of Illegal Identifiers Illegal Identifier Reason A Correct Identifier employee Salary There can be no space between employee and Salary. employeeSalary Hello! The exclamation mark cannot be used in an identifier. Hello one+two The symbol + cannot be used in an identifier. onePlusTwo 2nd An identifier cannot begin with a digit. second
Summary Program are step-by-step instructions that tell a computer how to perform a task Programmers use programming languages to communicate with the computer Structured design Problem is divided into smaller subproblems Each subproblem is solved Combine solutions to all subproblems Identifiers consist of letters, digits and underscores, and begin with a letter or an underscore
Bibliography Adams, J. & Nyhoff, L. (2003). “C++ An Introduction to Computing.” 3rd ed. New Jersey. Pearson Education. Bronson, G. J. (2006). “A First Book of C++: From Here to There.” 3rd ed. Massachusetts. Course Technology. Deitel, H.M & Deitel, A.S (2017). “C++ How to Program (Early Objects Version).” Global 10th ed. London. Pearson Education. Friedman, F.L & Koffman, E.B (2011). “Problem Solving, Abstraction, and Design Using C++.” 6th International ed. Massachusetts. Pearson Education. Malik, D.S. (2018). “C++ Programming From Problem Analysis to Program Design.” 8th ed. United States of America. Cengage Learning. Zak, D. (2016). “An Introduction to Programming with C++.” 8th ed. Massachusetts. Course Technology.