review 1 computers and programming i dr. ming zhang tel: (757) 594-7065 fax: (757) 594-7919 office:...
TRANSCRIPT
-
Review 1
Computers and Programming IDr. Ming Zhang
Tel: (757) 594-7065Fax: (757) 594-7919Office: Gosnold 217b Email: [email protected] Subject Overview Dr. Ming Zhang
-
Algorithms* Definition of AlgorithmAn 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 AlgorithmIn 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 coutusing std:cin; // Program uses cinusing std:endl; // program uses endlint main ( ){cout
-
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
-
Arithmetic OperationsOperation Operator Type Associativity
Addition+ BinaryLeft to rightSubtraction- BinaryLeft to rightMultiplication* BinaryLeft to rightDivision/ BinaryLeft to rightModulus % BinaryLeft to rightNegation- UnaryRight to Left
Problem Solving Using C Dr. Ming Zhang
-
Assignment Operations* General Form of Assignment Statementvariable = operand;* Constant Operand for Assignmentlength = 25; (is read, length is assigned the value 25) * Overwritten with New Valuelength = 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 VariationsThe 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 FunctionsFunction Name & Argument(s) Descriptionint abs(int i) Absolute value of idouble fabs(double d) Absolute value of ddouble pow(double d1, double d2) d1 raised to d2 powerdouble exp(double d) e raised to d powerdouble sqrt(doubler d) square root of ddouble 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 ddouble 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= 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 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
-
If Statement* Syntax of if Statementif (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 Statementif (condition)statement executed if condition id trueelsestatement executed if condition is false
* General Form of if-else Statement if (expression)statement1;elsestatement2; 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 partif ( expression1) { if (expression2) statement 1; else statement 2; } else Statement3; Selection Control Structure Dr. Ming Zhang
-
if-else Nested within the else partif ( expression1)statement 1; else if (expression2) statement2;elsestatement3; 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 > marcode;if (marcode == M) cout
-
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 LoopEnter the while statement
Test the Expression = 0 expression exit Step 1 whileLoop 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 Notation234.56 2.3456e+20.00479 4.79e-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 Operandthe value for the entire conditional expression if the condition is true.* Third Operandthe 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 Counterfor (int counter = 1; counter
-
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.getThe 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 StructureEnter do/while statement
Execute the statement(s)after do exit the Loop false doEvaluate the statementexpression 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 5using std::cout; using std::cin;int main( ){int count, num; float total, average;for(total=0.0,count=1;count num; total = total + num;} average = total/MAX; cout
- Selection within a Loop#include #define MAX 5#define THR 8using std::cout; using std::cin;int main( ){int count, num; float total, average;for(total=0.0,count=1;count num; if(num> THR) total = total + num; else total = total - num;} average = total/MAX; cout
- Evaluating Functions of One Variable# include using std::cout; using std::endl;int main( ){ int x, y;for (x=2; x
- Interactive Loop Control#include using std::cout; using std::endl;int main( ){ int num, final;cout
-
Exercises/Home Work
You should studyALL questions from the Exercises and Home Work !!! Dr. Ming Zhang
-
Test 1
Week 6, Monday, 20%
Good Luck!!! Dr. Ming Zhang