670 likes | 779 Views
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
E N D
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
Main Hardware Component Categories: • Central Processing Unit (CPU) • Main Memory • Secondary Memory / Storage • Input Devices • Output Devices
Main Hardware Component Categories Figure 1-2
Main Memory • Addresses – Each byte in memory is identified by a unique number known as an address.
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.
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.
Machine Language • Machine language instructions are binary numbers, such as1011010000000101 • Rather than writing programs in machine language, programmers use programming languages.
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
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
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
Chapter 2: Introduction to C++
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; }
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!
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; }
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
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;
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 */
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
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;
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
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).
Outline • Basic “Hello World!!” • Variables • Data Types • int • char & string • float • bool • Illustration
Integer Data Types • Integer variables can hold whole numbers such as 12, 7, and -99.
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
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 “”
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
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
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
Chapter 3: Expressions and Interactivity
The cin Object • Standard input object • Like cout, requires iostream file • Used to read input from keyboard
How to use cin? SYNTAX These examples yield the same result. cin >> length ; cin >> width ; or cin >> length >> width ; cin >> Variable >> Variable .. . ;
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;
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
Some C++ operators PrecedenceOperator Description Higher + Positive - Negative ----------------------------------------------------------- * Multiplication / Division % Modulus ------------------------------------------------------------ + Addition - Subtraction ------------------------------------------------------------- Lower = Assignment
y = 3 * x /2; A = (3*x+2)/(4*a-1); d = b*b -4*a*c; Algebra and C++ expressions
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.
Example • double parts; • parts = 15/6; //parts=2 • double parts; • parts = 15.0/6; //parts=2.5
Other Similar Statements Slide 3- 46
Combined Assignment Operators Slide 3- 47
Mathematical Library Functions • #include <cmath> • Commonly used functions: • where n can be an integer, double, float. Slide 3- 48
Chapter 4: Making Decisions
Outline • Relational Operations • If statement • If • If/else statement • If/else if • Logical operators • Switch
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:
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
The if Statement • General Format: if (expression) statement; • If the expression is true, then statementis executed. • If the expressionis false, then statementis skipped.