0% found this document useful (0 votes)
4 views41 pages

Sec 03

The document covers various C++ programming concepts, including arithmetic and relational operators, type casting, and the use of manipulators for formatting output. It provides exercises for correcting code errors, performing arithmetic operations, and predicting program outputs. Additionally, it explains implicit and explicit casting, combined assignment operators, and formatting techniques using manipulators like setw, setprecision, and showpoint.

Uploaded by

hamdyn7030
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views41 pages

Sec 03

The document covers various C++ programming concepts, including arithmetic and relational operators, type casting, and the use of manipulators for formatting output. It provides exercises for correcting code errors, performing arithmetic operations, and predicting program outputs. Additionally, it explains implicit and explicit casting, combined assignment operators, and formatting techniques using manipulators like setw, setprecision, and showpoint.

Uploaded by

hamdyn7030
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Sec 03

C++ operation : Arithmetic Operators


Precedence of arithmetic operators
Exercises

15
3
324
Equality and Relational operators
Exercises
What is wrong with the following program?
How would you correct it?

#include <iostream>
using namespace std;
int main()
{
number = 62.7;
double number;
cout << number << endl;
return 0;
}
Write assignment statements that perform the
following operations with the variables a, b, and c.

• A) Adds 2 to a and stores the result in b.

B) Multiplies b times 4 and stores the result in a.

C) Divides a by 3.14 and stores the result in b.

D) Subtracts 8 from b and stores the result in a.

E) Stores the value 27 in a.

F) Stores the character ‘K’ in c.


What will the following programs print on the
screen?

#include <iostream>
using namespace std; 0
int main() 100
{
int freeze = 32, boil = 212;
freeze = 0;
boil = 100;
cout << freeze << endl << boil << endl;
return 0;
}
Exercise

1. The variable x starts with the value 0.


2. The variable y starts with the value 5.
3. Add 1 to x
4. Add 1 to y.
5. Add x and y, and store the result in y.
6. Display the value in y on the screen.;
7. The variable j starts with the value 10.;
8. The variable k starts with the value 2.
9. The variable l starts with the value 4.
10. Store the value of j time's k in j.
11. Store the value of k time's l in l
12. Add j and 1, and store the result in k.;
13. Display the value in the variable k on the screen
What will the following programs print on the screen?

#include <iostream>
using namespace std; 8
int main ( ) 2
{
int x = 0, y = 2;
x = y * 4;
cout « x « endl « y « endl ;
return 0;
}
Exercise
Find the Errors
Find the Errors

#include iostream
using namespace std;
int main();
}
int a, b, c \\ Three integers
a=3
b=4
c=a+b
cout < "The value of c is %d" < c;
return 0;
{
Predict the Output

6 3 12
Programs Exercises

• Write a program that reads in the radius of a circle as an integer and

prints the circle’s diameter, and area. Use the constant value 3.14159

for Pi (π). Do all calculations in output statements.

• Diameter = 2 *π* radius.

• Area = π*(radius) ^2
Programs Exercises

• Write a program that has the following character variables: first,

middle, and last. Store your initials in these variables and then display

them on the screen


Programs Exercises

• Write a program that stores your name, address, and phone number in

three separate string objects. Display the contents of the string objects

on the screen.
Programs Exercises

• Write a program that defines an integer variable named age and a float variable

named weight. Store your age and weight, in the variables. The program should

display these values on the screen in a manner similar to the following:

• Program Output

My age is 26 and my weight is 180 pounds.


Type Conversion/Casting
• Implicit Casting - converting a smaller type to a larger type size.
• char -> int -> float -> double
int b = 5;
float i = b;

• Explicit Casting - converting a larger type to a smaller type size.


• double -> float -> int -> char

1 - 21
Type Casting

• Type Casting Syntax:

• The C++ casting Syntax:

• The C-style Syntax :



Example
1. double number = 3.7;
int val;
val = static_cast<int>(number);

2. Write a program that reads two integers x and y and


prints z = x / y.
• Where x and y are of int type, Z is of type float.
What will the following program display?

#include <iostream>
using namespace std;
int main()
{
int integer1, integer2;
double result;
integer1 = 19;
integer2 = 2;
result = integer1 / integer2;
cout << result << endl;
result = static_cast<double>(integer1) / integer2;
cout << result << endl;
result = static_cast<double>(integer1 / integer2);
cout << result << endl;
return 0;
}
Find the Errors
Example

•Write a C++ program to convert small letter to capital letter.

26
Combined Assignment operators
Predict the Output Multiple Assignment

6 3 12
Formatting
Formatting Output
The way a value is printed is called its formatting.
C++ has special instructions that allow the user to control
output attributes such as spacing, decimal point precision,
data formatting and other features.

Stream Manipulators are :


•setw(n)
•fixed #include <iomanip>
•showpoint
•setprecision (n)
•left
•right 30
“ Setw “ Manipulator
Syntax of setw : Setw(field width)

 will leaf blank spaces before value of variable.

 Example:
price =9.5 , rate=8.76
cout << setw(10) << price << setw(7) << rate<<endl;
 output:

31
Example

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int intValue = 3928;
double doubleValue = 91.5;
string stringValue = "John J. Smith";

cout << "(" << setw(5) << intValue << ")" << endl;
cout << "(" << setw(8) << doubleValue << ")" << endl;
cout << "(" << setw(16) << stringValue << ")" <<endl;

return 0;
}
The setprecision Manipulator
The precision is the total number of digits that appear before and after
the decimal point.
The number of significant digits with which floating-point values are
displayed by using the setprecision manipulator.
Syntax of setprecision : setprecision(significant digits)
Example:

33
Example
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double quotient, number1 = 132.364, number2 = 26.91;
quotient = number1 / number2;
cout << quotient << endl; // quotient= 4.91877
cout << setprecision(5) << quotient << endl;
cout << setprecision(4) << quotient << endl;
cout << setprecision(3) << quotient << endl;
cout << setprecision(2) << quotient << endl;
cout << setprecision(1) << quotient << endl;
return 0;
}
The fixed Manipulator
The fixed Manipulator forces cout to print the digits in fixed-point
notation, or decimal.
float values are written using fixed-point notation, which means the
value is represented with exactly as many digits in the fraction part
as specified by the precision field and with no exponent part.
When the fixed and setprecision manipulators are used
together, the value specified by the setprecision manipulator will
be the number of digits to appear after the decimal point, not
the number of signficant digits.

35
Example

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double quotient, number1 = 132.364, number2 = 26.91;
quotient = number1 / number2;
cout << quotient << endl; // quotient= 4.91877
cout << fixed;
cout << setprecision(5) << quotient << endl;
cout << setprecision(4) << quotient << endl;
cout << setprecision(3) << quotient << endl;
cout << setprecision(2) << quotient << endl;
cout << setprecision(1) << quotient << endl;
return 0;
}
The show point Manipulator

By default, f1oating-point numbers are not displayed with trailing


zeroes, and f1oating-point numbers that do not have a fractional part are
not displayed with a decimal point.
If we want the numbers padded with trailing zeroes, we must use the
showpoint manipulator .

37
Example 1
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x = 123.4, y = 456.0;
cout << setprecision(6) << x << endl;
cout << y << endl;
return 0;
}

Example 2
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x = 123.4, y = 456.0;
cout << setprecision(6) <<showpoint << x << endl;
cout << y << endl;
return 0;
}
The left and right Manipulators

By default, output is right justified. each value is right-justified,


or aligned to the right of its print field.

The left manipulator, used to make the values to be left-justified .

39
Example 1
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x = 146.789, y = 24.2, z = 1.783;
cout << setw(10) << x << endl;
cout << setw(10) << y << endl;
cout << setw(10) << z << endl;
return 0;
}

Example 2
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x = 146.789, y = 24.2, z = 1.783;
cout << left << setw(10) << x << endl;
cout << setw(10) << y << endl;
cout << setw(10) << z << endl;
return 0;
}
Manipulators are operators used in C++ for formatting output.
Stream Manipulator Description
setw(n) Establishes a print field of n spaces.
Displays floating-point numbers in fixed
Fixed point notation.
Causes a decimal point and trailing zeroes to
showpoint be displayed, even if there is no fractional
part.
setprecision (n) Sets the precision of floating-point numbers.
left Causes subsequent output to be left justified.
right Causes subsequent output to be right justified.

41

You might also like