html5-img
1 / 19

Correction of the Handout

Correction of the Handout. #include <iostream> //Preprocessor using namespace std; int main (){ ………….. return 0; }

Télécharger la présentation

Correction of the Handout

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Correction of the Handout #include <iostream>//Preprocessor using namespace std; int main (){ ………….. return 0; } • A namespace is a named group of definitions. When objects that are defined within a namespace are used outside of that namespace, either their names must be prefixed with the name of the namespace or they must be in a block that is preceded by a using namespace statement. Namespaces make it possible for a program to use different objects with the same name, just as different people can have the same name. The cout object is defined within a namespace named std (for “standard”) in the <iostream> header file. • Throughout the rest of this class, every program is assumed to begin with the two lines #include <iostream> using namespace std;

  2. Week 3 In this class, you will learn about: • Program Structure, Actions and Data Types • Data Type: Variables • Output and Input action: function cout<< &cin>> • Assignment • Simple calculations Reference: Programming with C++, Theory and Problems of, Second Edition; John R Hubbard (McGraw Hill, 2000, Schaum's Outline Series)

  3. Data Type: Variables • Variables • Name of a variable • A variable has a name, which is given by the programmer by declaration or definition. When the name is given, the memory for it is specified. • Rules for creating names of variables • Rule 1: a name starts with a letter. • Rule 2: a name is unique, which should be different from other words in the program. • Rule 3: names are sensitive to letters upper case or lower case. Doug is not equivalent to doug. • Rule 4: names should be meaningful.

  4. For example, int alice; name of variable: alice type: int(eger) tail: ; Data Type: Variables Using namespace std

  5. Data Types

  6. Part of Integral Type

  7. It shows that the 32 bits it uses to store a float are partitioned into 3 parts: 23 bits for the mantissa, 8 bits for the exponent, and 1 bit for the sign. The 23-bit mantissa produces a floating-point value with 6 significant digits, and the 8-bit exponent yields a range in magnitude from about 10–37 to about 3 × 1038. i.e., 0.0000000000000000000000000000000000001 < |x| < 300,000,000,000,000,000,000,000,000,000,000,000,000 for any variable x declared to have type float. Float type example

  8. A new variable : char (character data) • Definition • A char variable is defined to hold a single character that appears on the keyboard. • Difference between a char and a string is that a char is a single character while a string is a collection of serial characters. • Declaration • char c; • Input a char variable from keyboard • A space cannot be input from keyboard

  9. A new variable : char (character data)

  10. A new variable : char (character data)

  11. Output action: function cout<< • More actions by using a back slash \ • The sign \ is not a normal character but a control character to instruct the computer to take an action corresponding to next character. See table below, • Escape characterEffect\nNewline\tHorizontal tab\rCarriage return\bBackspace\aBell or beep\\Backslash\”Double quote\’Single quote\?Question mark

  12. Input action: input function cin>>Assignment • We have seen that one way of assigning a variable with a value is to input a number of digits on keyboard. Another important way is to use an assignment statement. For example (assignment1.cpp),

  13. Simple calculations

  14. Simple calculations

  15. Integer Calculation Example int main() { // tests operators +,-,*,/,and %: int m=54; int n=20; cout << "m = " << m << " and n = " << n << endl; cout << "m+n = " << m+n << endl; // 54+20 = 74 cout << "m-n = " << m-n << endl; // 54-20 = 34 cout << "m*n = " << m*n << endl; // 54*20 = 1080 cout << "m/n = " << m/n << endl; // 54/20 = 2 cout << "m%n = " << m%n << endl; // 54%20 = 14 Return 0; }

  16. Prefix and postfix increment • Example:k = ++x;//prefix increment • is equivalent to: • x = x + 1; //increment n first • k = x;//assign x’s value to k • Example:k = x++;//postfix increment • is equivalent to • k = x;//assign x’s value to k • x = x + 1;//and then increment x

  17. Applying the Pre-increment and Post-increment Operators int main() { // shows the difference between m++ and ++m: int m,n; m = 44; n = ++m; // the pre-increment operator is applied to m cout << "m = " << m << ", n = " << n << endl; m = 44; n = m++; // the post-increment operator is applied to m cout << "m = " << m << ", n = " << n << endl; }

  18. COMPOSITE ASSIGNMENT OPERATORS • The standard assignment operator in C++ is the equals sign =. In addition to this operator, C++ also includes the following composite assignment operators: +=, -=, *=, /=, and %=. • When applied to a variable on the left, each applies the indicated arithmetic operation to it using the value of the expression on the right.

  19. Applying Composite Arithmetic Assignment Operators int main() { // tests arithmetic assignment operators: int n=22; cout << "n = " << n << endl; n += 9; // adds 9 to n cout << "After n += 9, n = " << n << endl; n -= 5; // subtracts 5 from n cout << "After n -= 5, n = " << n << endl; n *= 2; // multiplies n by 3 cout << "After n *= 2, n = " << n << endl; n / = 3; // divides n by 9 cout << "After n /= 3, n = " << n << endl; n %= 7; // reduces n to the remainder from dividing by 7 cout << "After n %= 7, n = " << n << endl; }

More Related