1 / 66

Exam1 Review

Exam1 Review. Why Program?. Computer – programmable machine designed to follow instructions Program (both art and science)– instructions in computer memory to make it do something Programmer – person who writes instructions (programs) to make computer perform a task

karan
Download Presentation

Exam1 Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exam1 Review

  2. Why Program? Computer – programmable machine designed to follow instructions Program (both art and science)– instructions in computer memory to make it do something Programmer – person who writes instructions (programs) to make computer perform a task SO, without programmers, no programs; without programs, a computer cannot do anything

  3. Main Hardware Component Categories: • Central Processing Unit (CPU) • Main Memory • Secondary Memory / Storage • Input Devices • Output Devices

  4. Main Hardware Component Categories Figure 1-2

  5. Main Memory • Addresses – Each byte in memory is identified by a unique number known as an address.

  6. Main Memory • In Figure 1-4, the number 149 is stored in the byte with the address 16, and the number 72 is stored at address 23.

  7. Programs and Programming Languages • A program is a set of instructions that the computer follows to perform a task • We start with an algorithm, which is a set of well-defined steps.

  8. Example Algorithm for Calculating Gross Pay

  9. Machine Language • Machine language instructions are binary numbers, such as1011010000000101 • Rather than writing programs in machine language, programmers use programming languages.

  10. Programs and Programming Languages • Types of languages: • Low-level: used for communication with computer hardware directly. Often written in binary machine code (0’s/1’s) directly. • High-level: closer to human language such as C++, Java

  11. From a High-Level Program to an Executable File

  12. Syntax • The rules of grammar that must be followed when writing a program • Controls the use of key words, operators, programmer-defined symbols, and punctuation

  13. Variables • A variable is a named storage location in the computer’s memory for holding a piece of data. • In Program 1-1 we used three variables: • The hours variable was used to hold the hours worked • The rate variable was used to hold the pay rate • The pay variable was used to hold the gross pay

  14. Chapter 2: Introduction to C++

  15. preprocessor directive which namespace to use beginning of function named main beginning of block for main output statement string literal end of block for main send 0 to operating system Parts of a C++ Program comment // sample C++ program #include <iostream> using namespace std; int main() { cout << "Hello, there!"; return 0; }

  16. The \n Escape Sequence • You can also use the \n escape sequence to start a new line of output. This will produce two lines of output:cout << "Programming is\n";cout << "fun!"; Notice that the \n is INSIDE the string. Programming is fun!

  17. Example 1 • What is the output from the next program? #include <iostream> using namespace std; int main( ) { cout << "Computers, computers everywhere\n as far as\n\nI can C"; return 0; }

  18. Comments • Used to document parts of the program • Explanatory remarks made within the program • Intended for persons reading the source code of the program: • Indicate the purpose of the program • Describe the use of variables • Explain complex sections of code • Are ignored by the compiler

  19. Single-Line Comments Begin with // through to the end of line: int length = 12; // length in inches int width = 15; // width in inches int area; // calculated area // calculate rectangle area area = length * width;

  20. Multi-Line Comments • Begin with /*, end with */ • Can span multiple lines: /* this is a multi-line comment */ • Can begin and end on the same line: int area; /* calculated area */

  21. Programming Style • The visual organization of the source code • Includes the use of spaces, tabs, and blank lines • Does not affect the syntax of the program • Affects the readability of the source code

  22. Variables and Literals • Variable: a storage location in memory • Syntax: data-type variable-name; • Has a name and a type of data it can hold • Must be declared before it can be used: int item;

  23. Variable Declaration

  24. What is an identifier? • An identifier is a programmer-defined name for some part of a program: variables, functions, etc. • A programmer chooses identifiers as long as the rules are followed, however, using meaningful (mnemonic) identifiers is a good programming practice • C++ is a case-sensitive language, lower case and upper case are different

  25. Identifier Rules • The first character of an identifier must be an alphabetic character or and underscore ( _ ), • After the first character you may use alphabetic characters, numbers, or underscore characters, no special characters are allowed ( !, @, #, $, %, &, *, etc.), or commas. • Upper- and lowercase characters are distinct (C++ is a case sensitive language).

  26. Valid and Invalid Identifiers

  27. Outline • Basic “Hello World!!” • Variables • Data Types • int • char & string • float • bool • Illustration

  28. Integer Data Types • Integer variables can hold whole numbers such as 12, 7, and -99.

  29. The char Data Type • Used to hold characters or very small integer values • Usually 1 byte of memory • Character literals must be enclosed in single quote marks. Example: 'A' • Numeric value of character from the character set is stored in memory: MEMORY: letter CODE: char letter; letter = 'C'; 67

  30. Character Strings • A series of characters in consecutive memory locations: string greeting; greeting = “Hello”; • Stored with the null terminator, \0, at the end: • Comprised of the characters between the “”

  31. Floating-Point Data Types • The floating-point data types are:floatdoublelong double • They can hold real numbers such as: 12.45 -3.8 • Stored in a form similar to scientific notation • All floating-point numbers are signed

  32. The bool Data Type • Represents values that are true or false • bool variables are stored as small integers • false is represented by 0, true by 1: bool allDone = true; bool finished = false; allDone finished 1 0

  33. Test Program Illustrating Assignment #include <iostream> using namespace std; int main () { int length1, width1 =2, area1, length2, width2 = 3, area2; length1 = 10; length2 = length1; length1 = 5 + 3; length1 = length2; area1 = length1 * width1; area2 = area1 + 10; area2 = area2 + 10; return 0; } length1 width1 2 area1 length2 width2 3 area2

  34. Chapter 3: Expressions and Interactivity

  35. The cin Object • Standard input object • Like cout, requires iostream file • Used to read input from keyboard

  36. How to use cin? SYNTAX These examples yield the same result. cin >> length ; cin >> width ; or cin >> length >> width ; cin >> Variable >> Variable .. . ;

  37. Displaying a Prompt • A prompt is a message that instructs the user to enter data. • You should alwaysusecoutto display a prompt before each cin statement.cout << "How tall is the room? ";cin >> height;

  38. Mathematical Expressions • Can create complex expressions using multiple mathematical operators • An expression can be a literal, a variable, or a mathematical combination of constants and variables

  39. Some C++ operators PrecedenceOperator Description Higher + Positive - Negative ----------------------------------------------------------- * Multiplication / Division % Modulus ------------------------------------------------------------ + Addition - Subtraction ------------------------------------------------------------- Lower = Assignment

  40. y = 3 * x /2; A = (3*x+2)/(4*a-1); d = b*b -4*a*c; Algebra and C++ expressions

  41. When You Mix Apples and Oranges: Type Conversion • Operations are performed between operands of the same type. • If not of the same type, C++ will convert one to be the type of the other • This can impact the results of calculations.

  42. Example • double parts; • parts = 15/6; //parts=2 • double parts; • parts = 15.0/6; //parts=2.5

  43. Other Similar Statements Slide 3- 46

  44. Combined Assignment Operators Slide 3- 47

  45. Mathematical Library Functions • #include <cmath> • Commonly used functions: • where n can be an integer, double, float. Slide 3- 48

  46. Chapter 4: Making Decisions

  47. Outline • Relational Operations • If statement • If • If/else statement • If/else if • Logical operators • Switch

  48. Relational Operators • Relational operations allow you to compare numeric and char values and determine whether one is greater, less, equal to, or not equal to another. • Operators:

  49. int x, y ; x = 4; y = 6; EXPRESSION VALUE x < y true x + 2 < y false x != y true x + 3 >= y true y == x false y == x+2 true y = x + 3 7

  50. The if Statement • General Format: if (expression) statement; • If the expression is true, then statementis executed. • If the expressionis false, then statementis skipped.

More Related