Review 1 Computers and Programming I Dr. Ming Zhang Tel: (757) Fax: (757) Office: Gosnold 217b Subject Overview Dr. Ming Zhang
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
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
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
Three Basic Data Values * Integer Numbers - Valid: 0, 5, -10, +25, 1000, 253, , Invalid: $255.62, 2,523, 3., 6,234,892, +6.0 * Floating-Point Numbers - Valid: , 5., -6.2, 0.0, 0.33, -6.67, 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
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
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
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
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
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
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 (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
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
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
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
if-else Nested within the if part if ( expression1) { if (expression2) statement 1; else statement 2; } else Statement3; Selection Control Structure Dr. Ming Zhang
if-else Nested within the else part if ( expression1) statement 1; else if (expression2) statement2; else statement3; Selection Control Structure Dr. Ming Zhang
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
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
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
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
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
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
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
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 e e-3 Dr. Ming Zhang
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
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
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
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
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
Pre/Postincrement of the Counter * Postincrement of the Counter for (int counter = 1; counter <= 10; counter ++) cout << counter ; Output: * Preincrement of the Counter for (int counter = 1; counter <= 10; ++counter) cout << counter ; Output: !!!!!!!!!!!!!!!!!!!!!!! Dr. Ming Zhang
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
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
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
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
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
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
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
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
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
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
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
Exercises/Home Work You should study ALL questions from the Exercises and Home Work !!! Dr. Ming Zhang
Test 1 Week 6, Monday, 20% Good Luck!!! Dr. Ming Zhang