460 likes | 715 Vues
Programming Fundamentals. Topic 1 Introduction to Programming. Objectives. Define the terminology used in programming Explain the tasks performed by a programmer Understand the employment opportunities for programmers and software engineers Become aware of structured design
E N D
Programming Fundamentals Topic 1 Introduction to Programming
Objectives • Define the terminology used in programming • Explain the tasks performed by a programmer • Understand the employment opportunities for programmers and software engineers • Become aware of structured design • A typical C++ program-development environment. • Study variable and • 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
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
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 engineer: 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
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 design
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 can 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 (note that C is in uppercase) which indicate that a file contains C++ source code.
Typical C++ Development Environment (Cont.) • Linux editors: vi and emacs. • C++ software packages for Microsoft Windows such as Microsoft Visual C++ (microsoft.com/express) have editors integrated into the programming environment. • You can also use a simple text editor, such as Notepad in Windows, to write your C++ code. • integrated development environment (IDE) • 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 2010 Express Edition • Dev C++ • NetBeans • Eclipse • CodeLite
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 preprocessor directives, which indicate that certain manipulations are to be performed on the program before compilation. • These manipulations usually include other text files to be compiled, and perform various text replacements. • Adetailed discussion of preprocessor features appears in Appendix E, Preprocessor.
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 can 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 another devices, such as disks and hardcopy printers. • 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 = initializer_expression;
Variables • Variables Declaration • Can be either initialized or uninitialized… • If variable is uninitialized Contents must be considered “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, 342222 • short, int, long • represented internally in binary • predefined constants 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, -.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 • values: 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 but not built-in, but come from library • Classes extended 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 • Consist 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 (continued) • The following are legal identifiers in C++: • first • conversion • payRate
Summary • Programs 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 consists of letters, digits, and underscores, and begins with letter or underscore
Bibliography • Zak, D. (2013). An Introduction to Programming with C++. 7th ed. Course Technology, Massachusetts. • Bronson, G.J. (2006). A First Book of C++: From Here To There, 3rd ed. Course Technology, Massachusetts. • Malik, D.S. (2009). Introduction to C++ Programming, Brief International ed. Course Technology, Massachusetts. • Friedman, F.L. & Koffman, E.B. (2011). Problem Solving, Abstraction, and Design Using C++. 6th ed. Pearson Education, Massachusetts. • Adams, J. & Nyhoff, L. (2003). C++ An Introduction to Computing. 3rd ed. Pearson Education, New Jersey. • Deitel, H.M. & Deitel, A.S. (2012). C++ How To Program, 8th ed. Pearson Education, London.