1 / 20

CPCS LAB2

CPCS LAB2. arithmetic operator & cin I.Mona Alshehri. The output formatting functions. setw (width) setw (n) - output the value of the next expression in n columns. The output is right justified.

psnell
Download Presentation

CPCS LAB2

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. CPCS LAB2 arithmetic operator & cin I.Mona Alshehri

  2. The output formattingfunctions setw(width) • setw(n) - output the value of the next expression in n columns. • The output is right justified. • If the number of specified columns is less than the number of columns required by the output, then the output is automatically expanded to the required number of columns. • To use the manipulator setw, the program must include the header file iomanip.h

  3. Example: cout<<"12345678901234567890"<<endl; cout<<setw(5)<<11<<endl; cout<<setw(5)<<“ALA”<<endl<<endl; cout<<setw(6)<<123<<endl; cout<<setw(2)<<345<<setw(4)<<19<<endl; • Output: 12345678901234567890 11 ALA 123 345 19

  4. Arithmetic operators: + addition - subtraction * multiplication / division % remainder (mod operator) Examples: f+7 p-c b*m x/y r%s

  5. Rules of arithmetic operator precedence: ( ) inside to outside * / % left to right + - left to right • The operators +, -, *, and / can be used with both integral and floating point data types, while % is used only for integral data type to find the remainder in ordinary division. Example: (3+4)* 5 + 2 /(32-1) + 8%3

  6. Example Arithmetic Expression Result Description 2 + 5 7 13 + 89 102 34 - 20 14 45 - 90 -45 2 * 7 14 5/2 2 14 / 7 2

  7. Expression Result Description 34 % 5 4 In the division 34/5, the quotient is 6 and the remainder is 4. -34 % 5 -4 In the division -34 / 5, the quotient is -6 and the remainder is -4. 4 % 6 4 In the division 4/6, the quotient is 0 and the remainder is 4. Example

  8. Assignment operator: = (right to left) example: a = 32; a = a-2; • Additional assignment operators: += -= *= /= %= a+=2; a=a+2; a-=2; a=a-2; c=c*2; c*=2; c=c/2; c/=2; c=c%2; c%=2;

  9. Incrementoperator- increment the value of a variable by 1 Decrement operator- decrement the value of a variable by 1. Pre Increment:++variable Post Increment:variable++ Pre Decrement:--variable Post Decrement:variable--

  10. Example: // Preincrementing and postincrementing #include <iostream.h> int main() { int c; c = 5; cout << c << endl; // print 5 cout << c++ << endl; // print 5 then postincrement cout << c << endl << endl; // print 6 }

  11. Example: { int c = 5; cout << c << endl; // print 5 cout << ++c << endl; // preincrement then print 6 cout << c << endl; // print 6 getch(); // successful termination }

  12. A Look at cin Int num1; cout << “Enter a number: “; cin >> num1; coutcin <<>> insertion extraction “put to” “get from” whitespace characters ignored

  13. cin & cout main(){cout << cin >> }

  14. :OUTPUT Statement • The output statement is used to display items on the screen. • The syntax of cout together with << is cout<<item; OR cout<<item_1<<item_2<<…<<item_n; OR cout<<item_1 <<item_2 << . . . <<item_n;

  15. Types of items 1. Numbers: integer or float numbers • cout<<10; 10 • cout<<1.25; 1.25 • cout<<1<<4<<2; 142 • cout<<-534; -534 • cout<<1,25; Incorrect statement(syntax error)

  16. Types of items 2. Characters: • A single character must be enclosed in single quotes such as: • cout<<‘A’; A • cout<<‘H’<<‘e’<<‘l’<<‘l’<<‘o’; Hello • cout<<‘A’<<‘ ’<<‘B’<<‘ ’<<‘C’; A B C

  17. Types of items 3. Text: • Multiple characters must be enclosed in double quotes such as: • cout<<“Hello”; Hello • cout<<“Mona Ali Mohammad”; Mona Ali Mohammad • cout<<“Mona”<<“Ali”<<“Mohammad”; MonaAliMohammad • cout<<“Mona”<<“ Ali ”<<“ Mohammad”; Mona Ali Mohammad

  18. Types of items 4. Special Characters: • The new line character : '\n‘ • When \n is encountered in the string, the cursor is positioned at the beginning of the next line. • \n may appear anywhere in the string. • cout<<“Hello\nThere”; Hello There • The tab character : '\t‘ • Tab to next tab stop approx 8 spaces for each \t • cout<<“Hello\tThere”; • Hello There

  19. Common Programming Errors • not initializing variables before use • forgetting >> to separate variables in cin • applying ++ or-- to an expression (x-y)++

  20. Exercise: • Write a program display your name in point that was specified by user (x,y)?

More Related