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

Slides:



Advertisements
Similar presentations
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Advertisements

ITEC113 Algorithms and Programming Techniques
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2006 Pearson Education, Inc. All rights reserved Introduction.
Control Structures in C++ while, do/while, for switch, break, continue.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Basic Elements of C++ Chapter 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
For Repetition Structures (L13) * General Form of the for Statement * Components of a Typical for Header * Pre/Postincrement of the Counter * Stream Manipulator.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
1 Lecture 4 for loops and switch statements Essentials of Counter-Controlled Repetition Counter-controlled repetition requires  Name of control.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Input, Output, and Processing
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Nested Control Structures * Conditional Operator (?:) * Preincrement and Postincrement * Predecrement and Postdecrement * Counter-Controlled Repetition.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Chapter 05 (Part III) Control Statements: Part II.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
1 Lecture 3 Control Structures else/if and while.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Selection Control Structures (L08) * Selection Criteria * if Statements * The if-else Statement Selection Control Structure Dr. Ming Zhang.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Recap……Last Time [Variables, Data Types and Constants]
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Top-Down Stepwise Refinement (L11) * Top-Down Stepwise Refinement * Cast Operator * Promotion (Implicit Conversion) * Unary Operator * Multiplicative Operator.
Switch Selection Structure (L14) * General Form of the switch Statement * Details of switch Statement * Flowchart of switch Statement * cin.get * Character.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Chapter 5 - Control Statements: Part 2 Outline 5.3 for Repetition.
Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 7 JavaScript: Control Statements, Part 1
Introduction to Computer Programming
Chapter 4 – C Program Control
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++.
Engineering Problem Solving with C++, Etter/Ingber
Basic Elements of C++ Chapter 2.
Chapter 7 Additional Control Structures
Structured Program
3-4-5 Introduction.
3 Control Statements:.
2.6 The if/else Selection Structure
Chapter 4 - Program Control
Presentation transcript:

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