Download
introduction to programming using c n.
Skip this Video
Loading SlideShow in 5 Seconds..
Introduction to programming using C++ PowerPoint Presentation
Download Presentation
Introduction to programming using C++

Introduction to programming using C++

302 Views Download Presentation
Download Presentation

Introduction to programming using C++

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Introduction to programming using C++ Dr. Mohamed Khafagy

  2. Introduction to C++ • C is a programming language developed in the 1970's alongside the UNIX operating system. • C provides a comprehensive set of features for handling a wide variety of applications, such as systems development and scientific computation. • C++ is an “extension” of the C language, in that most C programs are also C++ programs. • C++, as opposed to C, supports “object-oriented programming.”

  3. Work Worker Product Instructions Work Computer Product Program What is C++? • C++ is a programming language. • A computer program performs a specific task, and may interact with the user and the computer hardware. • Human work model: • Computer work model:

  4. Why C++? • Bad News: • C++ is not easy to learn • Good News: • Lots of good-paying jobs for programmers because C++ is not easy to learn! • Java uses C++ syntax, it is easy to learn Java if you know C++. • Though C++ is not the easiest language (Basic and Pascal are easier), it is not the hardest either (Ada, Prolog and Assembly languages are really difficult!)

  5. Who Uses C++? • Computer makers such as Sun, SGI, IBM, and HP • Airport • Computer chip manufacturers like Motorola & Intel • Software companies • Banks • Hong Kong Government • Hospital Authority • Telecommunications • Universities

  6. What Can We Do By the End of the Course? • Program the computer in applications such as the following: • Program a simple calculator • Program simple computer games • Program a small inventory system for a small company

  7. Programming and Problem Solving • Algorithm • A sequence of precise instructions which leads to a solution • Program • An algorithm expressed in a language the computer can understand

  8. Software Life Cycle • Analysis and specification of the task (problem definition) • Design of the software (object and algorithm design) • Implementation (coding) • Maintenance and evolution of the system • Obsolescence

  9. Questions • Can you… • Describe the first step to take when creating a program? • List the two main phases of the program design process? • Explain the importance of the problem-solving phase? • List the steps in the software life cycle?

  10. Specification of a problem • A precise statement/description of the problem. • It involves describing the input, the expected output, and the relationship between the input and output. • This is often done through preconditions and postconditions.

  11. Design • Formulation of a method, that is, of a sequence of steps, to solve the problem. • The design “language” can be pseudo-code, flowcharts, natural language, any combinations of those, etc. • A design so expressed is called an algorithm(s). • A good design approach is a top-down design where the problem is decomposed into smaller, simpler pieces, where each piece is designed into a module.

  12. Implementation • Development of actual C++ code that will carry out the design and solve the problem. • The design and implementation of data structures, abstract data types, and classes, are often a major part of design implementation.

  13. Analysis of the Solution • Estimation of how much time and memory an algorithm takes. • The purpose is twofold: • to get a ballpark figure of the speed and memory requirements to see if they meet the target • to compare competing designs and thus choose the best before any further investment in the application (implementation, testing, etc.)

  14. Testing and Debugging • Testing a program for syntactical correctness (no compiler errors) • Testing a program for semantic correctness, that is, checking if the program gives the correct output. • This is done by • having sample input data and corresponding, known output data • running the programs against the sample input • comparing the program output to the known output • in case there is no match, modify the code to achieve a perfect match. • One important tip for thorough testing: Fully exercise the code, that is, make sure each line of your code is executed.

  15. Maintenance and Evolution of a System • Ongoing, on-the-job modifications and updates of the programs.

  16. 1.4 Testing and Debugging • Bug • A mistake in a program • Debugging • Eliminating mistakes in programs • Term used when a moth caused a failed relayon the Harvard Mark 1 computer. Grace Hopper and other programmers taped the moth in logbook stating: “First actual case of a bug being found.”

  17. Program Errors • Syntax errors • Violation of the grammar rules of the language • Discovered by the compiler • Error messages may not always show correct location of errors • Run-time errors • Error conditions detected by the computer at run-time • Logic errors • Errors in the program’s algorithm • Most difficult to diagnose • Computer does not recognize an error

  18. Questions • Can you… • Describe the three kinds of program errors? • Tell what kind of errors the compiler catches? • What kind of error is produced if you forget a punctuation symbol such as a semi-colon? • Tell what type of error is produced when a program runs but produces incorrect results?

  19. Programming as a Problem Solving Process • Define and analyze the problem. • What is the input & output? • What other information is necessary? • Develop an algorithm. • What steps must be done? • Implement a program. • Compile, test, and debug the program. • Document and maintain the program.

  20. Algorithms • Sequential steps for solving a problem or task • Language independent • Written in plain English • Allows programmers to concentrate on the solution • without worrying about the implementation details

  21. Cake Algorithm • Stir into a large mixing bowl • 2 eggs • 4cups of water • Cake mix • Once all the lumps are gone • Preheat oven to 400 degrees • Place cake mix in a 4X7 greased cake pan • Bake for 35 minutes • Cool for 15 minutes and serve

  22. Simple Sort Algorithm • 1.Get a list of unsorted numbers • 2.Repeat steps 3 through 6 until the unsorted list is empty • 3.Compare the unsorted numbers • 4.Select the smallest unsorted number • 5.Move this number to the sorted list • 6.Remove the selected smallest number from the unsorted list • 7.Stop

  23. Simple Sort Algorithm

  24. There are two commonly used tools to help to document program logic (the algorithm). • These are flowcharts and Pseudocode. • in flowcharts are shown below:

  25. With flowcharting, essential steps of an algorithm are shown using the shapes above. The flow of data between steps is indicated by arrows, or flowlines. • For example, a flowchart (and equivalent • Pseudocode) to compute the interest on a loan is shown below:

  26. Flowchart

  27. Pseudocode • Read NAME, BALANCE, RATE • Compute INTEREST as BALANCE x RATE • Write (Display) NAME and INTEREST

  28. Read X, Y, Z • Compute Sum (S) as X + Y + Z • Compute Average (A) as S / 3 • Compute Product (P) as X x Y x Z • Write (Display) the Sum, Average and Product

  29. The example below shows the flowchart for a program that reads two numbers and displays the numbers read in decreasing order

  30. Read A, B • If A is less than B • BIG = B • SMALL = A • else • BIG = A • SMALL = B • Write (Display) BIG, SMALL

  31. Example • Problem Statement • Given a collection of nickels (US 5-cent coins) and pennies (US 1-cent coins), find the equivalent number of Hong Kong dollars • Problem Analysis • Input: • nickels (integer) - number of US nickels • pennies (integer) - number of US pennies • Output: • dollars (integer) - number of HK dollar coins to return

  32. Example: Initial Algorithm 1. Read in the numbers of nickels and pennies. 2. Compute the total value in US dollars. 3. Compute the corresponding total value in HK dollars. 4. Find the number of HK dollar coins 5. Display the number of HK dollar coins

  33. Example: Refined Algorithm • Read in the number of nickels and pennies and US2HK . 2. Compute the total value in US dollars. 2.1 total_USD = (5 * nickel + penny)/100 3. Compute the corresponding total in HK dollars. 3.1. total_HKD = total_USD * US2HK 4. Find the number of HK dollar coins. 4.1. total_HK_cent = total_HKD * 100 4.2. dollar = total_HK_cent / 100 5. Display the number of HK dollar.

  34. General form of a C++ program // Program description #include directives int main(){ constant declarations variable declarations executable statements return 0; }

  35. Declarations • Constants and variables must be declared before they can be used. • A constant declaration specifies the type, the name and the value of the constant. • A variable declaration specifies the type, the name and possibly the initial value of the variable. • When you declare a constant or a variable, the compiler: • Reserves a memory location in which to store the value of the constant or variable. • Associates the name of the constant or variable with the memory location. (You will use this name for referring to the constant or variable.) • For more on declarations, see www.courseware.ust.hk and choose English--> C++ --> Declarations.

  36. Identifier Names (i.e., Symbols) • Variables, functions, structures, classes, etc. • Prules • Are case sensitive • Must begin with a letter, which includes an underscore (_) • Subsequent characters may be letters, digits, and underscores • Cannot be a keyword • May only be defined once in a scope • Should avoid library names • Must be declared before use

  37. C++ keywords • Keywords appear in blue in Visual C++. • Each keyword has a predefined purpose in the language. • Do not use keywords as variable and constant names!! • The complete list of keywords is on page 673 of the textbook. • We shall cover the following keywords in this class: bool, break, case, char, const, continue, do, default, double, else, extern, false, float, for, if, int, long, namespace, return, short, static, struct, switch, typedef, true, unsigned, void, while

  38. Variables • Name a region of memory where data is stored • Must be defined before use: includes data type and name • Names are case sensitive • Examples (definition and initialization) • int dollars; • intquarters, dimes, nickels, pennies; • int money = 217; • double x, pi = 3.14159; • char begin = 'A', end = 'Z', newLine = '\n‘;

  39. Variable declarations • Variables are used to store values that can be changed during the program execution. • A variable is best thought of as a container for a value. 3445 y Syntax: < type >< identifier >; < type >< identifier >=< expression >; Examples: intsum; int total = 3445; char answer = 'y'; double temperature = -3.14;

  40. Variable declarations • A variable has a type and it can contain only values of that type. For example, a variable of the type int can only hold integer values. • Variables are not automatically initialized. For example, after declaration intsum; the value of the variable sumcan be anything (garbage). • Thus, it is good practice to initialize variables when they are declared. • Once a value has been placed in a variable it stays there until the program deliberately alters it.

  41. Character data • A variable or a constant of char type can hold an ASCII character • When initializing a constant or a variable of char type, or when changing the value of a variable of char type, the value is enclosed in single quotation marks. Examples: constchar star = '*'; char letter, one = '1';

  42. Constant declarations • Constants are used to store values that never change during the program execution. • Using constants makes programs more readable and maintainable. Syntax: const <type> <identifier> = <expression>; Examples: const double US2HK = 7.8; //Exchange rate of US$ to HK$ const double HK2TW = 3.98; //Exchange rate of HK$ to TW$ const double US2TW = US2HK * HK2TW; //Exchange rate of US$ to TW$

  43. C++ Data Type A type defines a set of values and a set of operations that can be applied on those values. The set of values for each type is known as the domain for the type. C++ contains 5 standard types:

  44. void The void type has no values and no operations. In other words, both the set of values and the set of operations are empty. Although this might seem unusual, we will see later that it is a very useful data type.