MELJUN CORTES C Fundamental Basics

184 views

Published on

MELJUN CORTES C Fundamental Basics

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
184
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MELJUN CORTES C Fundamental Basics

  1. 1. C++ Programming Complete 1 CHAPTER 2 PROBLEM SOLVING USING C++ MELJUN CORTES MELJUN CORTES,BSCS,ACS
  2. 2. Objectives 2 Introduction to C++ : Identifier, keyword, mnemonic Programming Style Data Types Arithmetic Operations Variables and Declaration Statements MELJUN CORTES,BSCS,ACS
  3. 3. Introduction to C++ 3 Modular Program: a program consisting of interrelated segments arranged in a logical and understandable form  Easier to develop, correct, and modify than other kinds of programs Module: a small segment which is designed to perform a specific task  A group of modules is used to construct a modular program MELJUN CORTES,BSCS,ACS
  4. 4. Introduction to C++(continued) 4 Modules in C++ can be classes or functions:  Function: accepts an input and produces an output by processing the input in some fashion   A function’s processing is encapsulated and hidden within the function Class: contains both data and functions used to manipulate the data  Function: encapsulates a set of operations, while a class encapsulates data plus one or more sets of operations MELJUN CORTES,BSCS,ACS
  5. 5. Introduction to C++: Identifier 5 Identifier: a name given to an element of the language, such as a class or function Rules for forming identifier names:     First character must be a letter or underscore Only letters, digits, or underscores may follow the initial letter (no blanks allowed) Keywords cannot be used as identifiers Max length of an identifier = 1024 characters Use underscores to separate multiple words in a name, or capitalize the first letter of each word. MELJUN CORTES,BSCS,ACS
  6. 6. Introduction to C++: Keyword 6  Keyword: a reserved name that represents a built-in object or function of the language MELJUN CORTES,BSCS,ACS
  7. 7. Introduction to C++: Identifier (continued) 7 Identify valid C++ identifiers below: valid  degToRad invalid  1AB3 begins with a number  addNums valid  while invalid this is a keyword  slope valid  findMax valid  E*6 invalid contains a special character MELJUN CORTES,BSCS,ACS
  8. 8. Introduction to C++: Mnemonic 8 Function names  Require a set of parentheses at the end  Can use mixed upper and lower case  Should be meaningful, or be a mnemonic Mnemonic: a word designed as a memory aid Examples of function names: areaCircle() calAverage() main() Note that C++ is a case-sensitive language! MELJUN CORTES,BSCS,ACS
  9. 9. Introduction to C++: The main() Function 9 Overall structure of a C++ program contains one function named main(), called the driver function All other functions are invoked from main() Each statement inside the function must be terminated with a semicolon return: a keyword causing the appropriate value to be returned from the function return 0 in the main() function causes the program to end MELJUN CORTES,BSCS,ACS
  10. 10. Introduction to C++: The main() Function (continued) 10 The structure of a main() function. MELJUN CORTES,BSCS,ACS
  11. 11. Introduction to C++: The cout Object 11 cout object: an output object that sends data to a standard output display device USEG20/Saidatul Rahah
  12. 12. Objectives 12 Introduction to C++ : Identifier, keyword, mnemonic Programming Style Data Types Arithmetic Operations Variables and Declaration Statements MELJUN CORTES,BSCS,ACS
  13. 13. Programming Style 13 • Although more than one C++ statement can be on a single line, good style calls for one statement per line • Opening and closing braces {} for the function body should each be on separate lines • Statements in the function body should be indented MELJUN CORTES,BSCS,ACS
  14. 14. Programming Style: Comments 14  Comments: explanatory remarks in the source code added by the programmer  Line comment: begins with // and continues to the end of the line  Line comment can be on a line by itself, or at the end of a line of code  Line comment cannot be longer than one line  Block Comment: a comment that spans across two or more lines  Block comment begins with /* and ends with */ Example: /* This is a block comment that spans across three lines */ MELJUN CORTES,BSCS,ACS
  15. 15. Programming Style: Comments (continued) 15 MELJUN CORTES,BSCS,ACS
  16. 16. Objectives 16 Introduction to C++ : Identifier, keyword, mnemonic Programming Style Data Types Arithmetic Operations Variables and Declaration Statements MELJUN CORTES,BSCS,ACS
  17. 17. Data Types 17 • Data type: a set of values and the operations that can be applied to these values • Two fundamental C++ data groupings: – – Class data type (a class): created by the programmer Built-in data type (primitive type): part of the C++ compiler MELJUN CORTES,BSCS,ACS
  18. 18. Data Types (continued) 18 Built-in data types Numerical Data Types Integer Types Integer Types USEG20/Saidatul Rahah FloatingFloatingPointTypes PointTypes
  19. 19. Data Types: Integer 19 USEG20/Saidatul Rahah
  20. 20. Data Types: Integer (continued) 20 int data type: whole numbers, optionally with + or – sign Example: 2 char data type: individual character; any letter, digit, or special character enclosed in single quotes Example: ‘A’ Character values are usually stored in ASCII code USEG20/Saidatul Rahah
  21. 21. Data Types: Integer (continued) 21 USEG20/Saidatul Rahah
  22. 22. Data Types: Integer (continued) 22 Escape character: the backslash, ; indicates an escape sequence Escape sequence: tells compiler to treat the following characters as special instruction codes USEG20/Saidatul Rahah
  23. 23. 23 Refer Table 2.4: Escape Character. p61 USEG20/Saidatul Rahah
  24. 24. Data Types: Integer (continued) 24 bool data type: represents Boolean (logical) data; restricted to two values: true or false sizeof operator: shows the number of bytes used to store values of any data type Values returned by sizeof are compiler dependent USEG20/Saidatul Rahah
  25. 25. Data Types: Integer (continued) 25 USEG20/Saidatul Rahah
  26. 26. Data Types: Integer (continued) 26 Signed data type: one that permits negative, positive, and zero values Unsigned data type: permits only positive and zero values An unsigned data type provides essentially double the range of its signed counterpart USEG20/Saidatul Rahah
  27. 27. Data Types: Integer (continued) 27 USEG20/Saidatul Rahah
  28. 28. Data Types: Floating-Point Types 28 Floating-point number (real number): zero or any positive or negative number containing a decimal point Examples: +10.625 5. -6.2  No special characters are allowed Three floating-point data types in C++: float (single precision)  double (double precision)  long double  USEG20/Saidatul Rahah
  29. 29. Data Types: Floating-Point Types (continued) 29 USEG20/Saidatul Rahah
  30. 30. Objectives 30 Introduction to C++ : Identifier, keyword, mnemonic Programming Style Data Types Arithmetic Operations Variables and Declaration Statements USEG20/Saidatul Rahah
  31. 31. Arithmetic Operations 31 • Arithmetic operators are binary operators • Binary operators: require two operands • Different data types can be used in the same arithmetic expression USEG20/Saidatul Rahah
  32. 32. Arithmetic Operations: Expression Types 32  Expression: any combination of operators and operands that can be evaluated to yield a value 15 + 5 = 20 operands operator  Mixed-mode expression: contains integer and floating- point operands; yields a double-precision value USEG20/Saidatul Rahah
  33. 33. Arithmetic Operations : Summary of Operators 33 USEG20/Saidatul Rahah
  34. 34. Arithmetic Operations (continued) 34 Integer Data Type Floating Point Data Type 7 + 3 = 10 7.0 + 3.0 = 10.0 7–3=4 7 .0– 3.0 = 4.0 7 * 3 = 21 7.0 * 3.0 = 21.0 7/3=2 7.0 / 3.0 = 2.33 7% 3=1 •If both operands are integer data type, output will be INTEGER •If ONE operand is floating point data Type, output will be FLOATING POINT USEG20/Saidatul Rahah
  35. 35. Arithmetic Operations: Operator Precedence & Associativity 35 Rules for writing arithmetic expressions:  Never place two consecutive binary arithmetic operators side by side  Use parentheses to form groupings; contents within parentheses are evaluated first  You may nest parentheses within other parentheses; evaluated from innermost to outermost  Use the * operator for multiplication, not parentheses USEG20/Saidatul Rahah
  36. 36. Arithmetic Operations: Operator Precedence & Associativity (continued) 36 Expressions with multiple operators are evaluated by precedence of operators  Associativity: the order in which operators of the same precedence are evaluated USEG20/Saidatul Rahah
  37. 37. Arithmetic Operations: Operator Precedence & Associativity (continued) 37 Example: 8+5*7%2*4 = 8 + 35 % 2 * 4 =8+1*4 =8+5 =12 USEG20/Saidatul Rahah
  38. 38. Objectives 38 Introduction to C++ : Identifier, keyword, mnemonic Programming Style Data Types Arithmetic Operations Variables and Declaration Statements USEG20/Saidatul Rahah
  39. 39. Variables and Declaration Statements 39 • Variable: symbolic identifier for a memory address where data can be held • Use identifier naming rules for variable names USEG20/Saidatul Rahah
  40. 40. Variables and Declaration Statements (continued) 40 Assignment statement: used to store a value into a variable Value of the expression on the right side of the = is assigned to the memory location of the variable on the left side of the = Examples: num1 = 45; num2 = 12; total = num1 + num2; USEG20/Saidatul Rahah 45 num1
  41. 41. Variables and Declaration Statements (continued) 41 Declaration statement: specifies the data type and identifier of a variable; sets up the memory location Syntax: <dataType> <variableName>; A variable must be declared before it is used! Data type is any valid C++ data type Example: int sum; int num1, num2, num3; float pi = 3.142; Initialized in declaration USEG20/Saidatul Rahah
  42. 42. Problem Solving 42 Write a C++ program to calculate the average of two double-precision numbers.  Apply Software Development Procedure:  Step 1: Analyze the problem  Output - average  Inputs - 2 numbers ( num1, num2)  Step 2: Develop a solution Assign values to num1 and num2  Calculate and display average    Step 3: Code the solution Step 4: Test and correct the program USEG20/Saidatul Rahah
  43. 43. Problem Solving (continued) 43 Flowchart: start start Input 2 Input 2 numbers numbers Calculate Calculate average average #include <iostream.h> Variable int main() declaration { double num1, num2, average; num1 = 45.5; Assignment num2 = 10.2; statement average = (num1+num2)/2; cout<<“Average is: “<<average; Display Display average average end end USEG20/Saidatul Rahah return 0; } output
  44. 44. Common Programming Errors 44 Missing parentheses after main Missing or incorrect braces around function body Misspelling a reserved word Missing ending double quotes on string literal Missing semicolon at end of statement Adding a semicolon at end of #include statement Missing n to indicate new line Substituting letter O for zero and vice versa USEG20/Saidatul Rahah
  45. 45. Common Programming Errors (continued) 45 Failing to declare all variables Storing incorrect data type into a variable Attempting to use a variable with no value Dividing integer values incorrectly Mixing data types in the same expression USEG20/Saidatul Rahah
  46. 46. Short Quiz 46 Write down your name, SID no, class name on a piece of paper and answer below question. 1. List 4 rules for forming identifier names. 2. Solve expression given below. 12 + 5 % 2 * 8 – 3 Thank You for Listening….. USEG20/Saidatul Rahah

×