200 likes | 219 Views
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.
E N D
CPCS LAB2 arithmetic operator & cin I.Mona Alshehri
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
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
Arithmetic operators: + addition - subtraction * multiplication / division % remainder (mod operator) Examples: f+7 p-c b*m x/y r%s
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
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
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
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;
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--
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 }
Example: { int c = 5; cout << c << endl; // print 5 cout << ++c << endl; // preincrement then print 6 cout << c << endl; // print 6 getch(); // successful termination }
A Look at cin Int num1; cout << “Enter a number: “; cin >> num1; coutcin <<>> insertion extraction “put to” “get from” whitespace characters ignored
cin & cout main(){cout << cin >> }
: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;
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)
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
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
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
Common Programming Errors • not initializing variables before use • forgetting >> to separate variables in cin • applying ++ or-- to an expression (x-y)++
Exercise: • Write a program display your name in point that was specified by user (x,y)?