Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review 1 Computers and Programming I Dr. Ming Zhang Tel: (757) 594-7065 Fax: (757) 594-7919 Office: Gosnold 217b Subject Overview.

Similar presentations


Presentation on theme: "Review 1 Computers and Programming I Dr. Ming Zhang Tel: (757) 594-7065 Fax: (757) 594-7919 Office: Gosnold 217b Subject Overview."— Presentation transcript:

1 Review 1 Computers and Programming I Dr. Ming Zhang Tel: (757) 594-7065 Fax: (757) 594-7919 Office: Gosnold 217b Email: mzhang@cnu.edu Subject Overview Dr. Ming Zhang

2 Algorithms * Definition of Algorithm An algorithm is defined as a step-by-step sequence of instructions that must terminate and describes how the data is to be processed to produce the desired outputs * Question for Developing an Algorithm In essence, an algorithm answers the question, “What method will you use to solve this problem?” Introduction to Computers and Programming Dr. Ming Zhang

3 Flowchart for Calculating the Average 1 Start Input values for a, b, and c Calculate the average Display the average End Introduction to Computers and Programming Dr. Ming Zhang

4 Hollow World Using C++ #include using std::cout; // program uses cout using std:cin; // Program uses cin using std:endl; // program uses endl int main ( ) { cout << “Hollow World!” << endl; return 0; // program ended successfully } Hollow World Using C /C++ Dr. Ming Zhang

5 Three Basic Data Values * Integer Numbers - Valid: 0, 5, -10, +25, 1000, 253, -26351, +36 - Invalid: $255.62, 2,523, 3., 6,234,892, +6.0 * Floating-Point Numbers - Valid: +10.625, 5., -6.2, 0.0, 0.33, -6.67, +2. - Invalid: 5,326.25, 24, 123, 6,459, $10.29 * Character Values - Valid: ‘A’, ‘$’, ‘b’, ‘7’, ‘y’, ‘Y’, ‘M’, ‘q’ Problem Solving Using C Dr. Ming Zhang

6 Arithmetic Operations Operation Operator Type Associativity Addition+ BinaryLeft to right Subtraction- BinaryLeft to right Multiplication* BinaryLeft to right Division/ BinaryLeft to right Modulus % BinaryLeft to right Negation- UnaryRight to Left Problem Solving Using C Dr. Ming Zhang

7 Assignment Operations * General Form of Assignment Statement variable = operand; * Constant Operand for Assignment length = 25; (is read, length is assigned the value 25) * Overwritten with New Value length = 25; length = 6.28; ( The 25 that was in length is overwritten with new value of 6.28, because a variable can only store one value at a time) Completing the Basic Dr. Ming Zhang

8 Assignment Variations * Assignment Variations The variable on the left of the equal sign can also be used the right of the equal sign in the assignment statement sum = 20; sum = sum +10; /* sum = 30 */ * Assignment expressions like sum = sum + 25, which use the same variable on both sides of the assignment operator, can be written using the following assignment operators: += -= *= %= Completing the Basic Dr. Ming Zhang

9 Mathematical Library Functions Function Name & Argument(s) Description int abs(int i) Absolute value of i double fabs(double d) Absolute value of d double pow(double d1, double d2) d1 raised to d2 power double exp(double d) e raised to d power double sqrt(doubler d) square root of d double sin(double d) Sin of d(d in radians) double cos(double d) Cosine of d(d: radians) double log(double d) Natural log of d double log10(double d) Common log of d Completing the Basic Dr. Ming Zhang

10 Relational Operators Operator Meaning Example < Less thanage < 30 >Greater than hight > 6.2 <=Less than/equal totaxtable<=20 >=Greante than/equal totemp>= 98.6 ==Equal to grade == 100 !=Not equal tonumber !=250 Selection Control Structure Dr. Ming Zhang

11 Logical Operators * Logical Operators &&: Logical operator AND ||: Logical operator OR !: Logical operator NOT * Examples - (age > 40) && (term < 10) is true only if age is greater than 40 and term is less 10. - (age > 40) || (term < 10) will be true if either age is greater than 40, term is less 10, or both condition are true. Selection Control Structure Dr. Ming Zhang

12 If Statement * Syntax of if Statement if (condition) statement executed if condition is “true” * General Form of if Statement if (expression) statement; Selection Control Structure Dr. Ming Zhang

13 If-else Statement * Syntax of if-else Statement if (condition) statement executed if condition id “true” else statement executed if condition is “false” * General Form of if-else Statement if (expression) statement1; else statement2; Selection Control Structure Dr. Ming Zhang

14 Nested if Statement * The inclusion of one or more if statements whthin an existing if statement is called a nested if statement. * if ( expression1) { if (expression2) statement 1; } else Statement3; Selection Control Structure Dr. Ming Zhang

15 if-else Nested within the if part if ( expression1) { if (expression2) statement 1; else statement 2; } else Statement3; Selection Control Structure Dr. Ming Zhang

16 if-else Nested within the else part if ( expression1) statement 1; else if (expression2) statement2; else statement3; Selection Control Structure Dr. Ming Zhang

17 Exercise: if-else Chain (C ++) #include using std::cout; using std::endl; using std::cin; int main( ) {char marcode; cout << “Enter a marital code:”; cin >> marcode; if (marcode == ‘M’) cout << “Individual is married.”<< endl; else if (marcode == ‘S’) cout << “Individual is single.” << endl; else if (marcode == ‘D’) cout << “Individual is divorced.” << endl; return (0);} Selection Control Structure Dr. Ming Zhang

18 Pseudocode * Pseudocode Pseudocode is an artificial and informal language that helps programmers develop algorithm. Pseudocode is simular to everyday English. * Pseudocode consists only of executable statements - those that are executed when the program has been converted from pseudocode to C++ and is run.( no declaration part) Dr. Ming Zhang

19 while Loop Enter the while statement Test the Expression = 0 expression exit Step 1 while Loop Expression != 0 Execute the statement after the parentheses (step 2a) Go back and reevaluate the express (step 2b) Dr. Ming Zhang

20 Top-Down Stepwise Refinement * The top is a single statement that conveys the overall function of the program. * Then we divided the top into a series of smaller tasks and list these in the order in which they need to be performed. This results the first refinement. * To proceed to the next level of refinement until no further level refinement could be taken. Dr. Ming Zhang

21 Cast Operator static_cast * Cast Operator static_cast creates a temporary floating-point copy of its operand in parentheses - total. * Using a cast operator in this manner is called explicit conversion. * The value stored in total is still an integer. * The calculation now consists of a floating- point value (the temporary float version of total) Dr. Ming Zhang

22 Promotion * The C++ compiler only knows to evaluate expressions in which the data types of the operands are identical. * To ensure that the operands are of the same type, the compiler performs an operation called promotion(also called implicit conversion) on selection operands. * For example, in an expression containing the data types int and float, int operands are promoted to float. Dr. Ming Zhang

23 setiosflags(ios::fixed|iso::showpoint) * The stream manipulator setiosflags(ios::fixed|iso::showpoint) sets two output formatting options, namely ios::fixed and iso::showpoint. * The vertical bar character ( | ) separates multiple options in a setiosflags call. Dr. Ming Zhang

24 ios::fixed * The option ios::fixed causes a floating- point value to be output in so-called fixed-point format (such as 92.34), not the scientific notation (such as 9.23e+1). * Fixed-Point Format Scientific Notation 234.56 2.3456e+2 0.00479 4.79e-3 Dr. Ming Zhang

25 iso::showpoint * The option iso::showpoint forces the decimal point and trailing zero to print even if the value is a whole number amount (such as 88.00). * Without the option iso::showpoint, such a value prints in C++ as 88 without the trailing zero and without the decimal point. * The printed value is rounded to indicated number of decimal positions, although the value in the memory remains unaltered. Dr. Ming Zhang

26 Condition Operator (?:) * Conditional Operator (?:) - Ternary Operator Operand1 ? Operand2 : Operand3 * First Operand Condition Expression * Second Operand the value for the entire conditional expression if the condition is true. * Third Operand the value for the entire conditional expression if the condition is false. Dr. Ming Zhang

27 Preincrement and Postincrement * Preincrement ++a Increment a by 1, then use the new value of a in the expression in which a resides. * Postincrement a++ Use the current value of a in the expression in which a resides, then increment a by 1 Dr. Ming Zhang

28 Predecrement and Postdecrement * Predecrement --a Decrement a by 1, then use the new value of a in the expression in which a resides. * Postdecrement a-- Use the current value of a in the expression in which a resides, then decrement a by 1 Dr. Ming Zhang

29 A for Loop Flowchart Initializing Statement(s) Evaluate false the tested exit expression for Loop true Execute the statement after the parentheses Execute the altering list Dr. Ming Zhang

30 Pre/Postincrement of the Counter * Postincrement of the Counter for (int counter = 1; counter <= 10; counter ++) cout << counter ; Output: 12345678910 * Preincrement of the Counter for (int counter = 1; counter <= 10; ++counter) cout << counter ; Output: 12345678910 !!!!!!!!!!!!!!!!!!!!!!! Dr. Ming Zhang

31 Stream Manipulator setw(m) * The call setw(m) specifies that the next value output is printed in filed width of m. * If the value to be output is less than m character positions wide, the value is right justified in the filed by default. * If the value to be output is more than m character position wide, the field width is extended to accommodate the entire value. Dr. Ming Zhang

32 Flowchart of switch Statement case a case a action(s) break case b case b action(s) break....... case z case z action(s) break default action(s) Dr. Ming Zhang

33 cin.get * grade = cin.get The cin.get( ) function reads one character from the keyboard and stores that character in integer variable grade. * Details will be introduced in Chapter 6, “Classes”. Dr. Ming Zhang

34 EOF * EOF (end-of-file) EOF is the symbol whose acronym stands for end-of-file. * EOF normal has the value -1. However, we do not type the value -1 nor do we type the letters EOF. Rather, you type a system- dependent keystroke combination to mean “end-of-file”. * UNIX: -> EOF MS-DOS: -> EOF Dr. Ming Zhang

35 Flowchart of do/while Structure Enter do/while statement Execute the statement(s) after do exit the Loop false do Evaluate the statement expression true Dr. Ming Zhang

36 break Statement * The break statement, when executed in a while, for, do/while, or switch structure, causes immediate exit for that structure. * Program execution continues with the first statement after the structure. * Common uses of the break statement are to escape early from a loop, or to skip the remainder of a switch structure Dr. Ming Zhang

37 Continue Statement * The continue statement, when executed in a while, for, or do/while structure, skips the remaining statements in the body of that structure, and proceeds with the next iteration of the loop. * In while and do/while structures, the loop- continuation test is evaluated immediately after the continue statement is executed. * In the for structure, the increment expression is executed, then the loop-continuation test is evaluated. Dr. Ming Zhang

38 Interactive Input within a Loop #include #define MAX 5 using std::cout; using std::cin; int main( ) {int count, num; float total, average; for(total=0.0,count=1;count<=MAX;++count) { cout << “Enter a number:”; cin >> num; total = total + num;} average = total/MAX; cout << “ The average is” << average<<endl; return(0);} Dr. Ming Zhang

39 Selection within a Loop #include #define MAX 5 #define THR 8 using std::cout; using std::cin; int main( ) {int count, num; float total, average; for(total=0.0,count=1;count<=MAX;++count) { cout > num; if(num> THR) total = total + num; else total = total - num;} average = total/MAX; cout << “ The average is” << average<<endl; return(0);} Dr. Ming Zhang

40 Evaluating Functions of One Variable # include using std::cout; using std::endl; int main( ) { int x, y; for (x=2; x<=6; ++x){ y =10*x*x +3*x -2; //function of one variable cout << x << y<<endl; } return(0); } Dr. Ming Zhang

41 Interactive Loop Control #include using std::cout; using std::endl; int main( ) { int num, final; cout <<“Enter the final number for the table:” cin << final;//variable final used to control a loop for(num = 1; num <= final; ++num) cout << num << (num*num); return(0) } Dr. Ming Zhang

42 Exercises/Home Work You should study ALL questions from the Exercises and Home Work !!! Dr. Ming Zhang

43 Test 1 Week 6, Monday, 20% Good Luck!!! Dr. Ming Zhang


Download ppt "Review 1 Computers and Programming I Dr. Ming Zhang Tel: (757) 594-7065 Fax: (757) 594-7919 Office: Gosnold 217b Subject Overview."

Similar presentations


Ads by Google