Lecture 3(Basics)
Lecture 3(Basics)
Simp Structur
le ed
arra stru unio class
floatin y ct n
g
float doubl long
e double
enum
integr Addres
al s
unsign
ed
cin - some facts ...
Possible formats of input:
Example: 1) 101112
int a, b, c;
cin >> a >> b >> c; 2) 10 11 12
3) 10 11
= <enter> key 12
= space
4) 10 11
12
5) 10
11
12
Character Objects
• A variable of type char can store any single
character value.
• Character data in a C++ program is represented
by writing the appropriate symbol between single
quote signs
Ex: 'a ' '#' 'Z '
• Declaring and initializing a variable of type
character.
char letter = 'a' ;
Character Objects
Ex: char grade;
cout << "Enter your grade: ";
cin >> grade;
cout << "You received a grade of " << grade
<< endl;
Statement Data
i j
1. cin >> i; 32
Example:
char nextCh;
cin.get(nextCh); // will read any character
Input whitespace Characters …
Example
What is the input in each case?
char ch1, ch2, ch3;
ABCD
Case 1: Case 2:
cin.get(ch1); AB cin >> ch1; AB
cin.get(ch2); CD cin >> ch2; CD
cin.get(ch3); cin >> ch3;
Example:
Number Whole Fraction
-12.352 -12 .352
123.4 123 .4
Real Numbers …
Example 1:
Number Mantissa Exponent
-5.07e15 -5.07 15
Example 2:
Number Mantissa Exponent
1.23e-5 1.23 5
means 1.23 x 10-5 = 0.0000123
void main()
{
cout << 4.5 << endl; 4.5
cout << -.375 << endl; -0.375
cout << 10.6666666 << endl; 10.6667
cout << 67800000.0 << endl; 6.78e+007
cout << 523.0 << endl; 523
cout << 523000000000. << endl; 5.23e+011
cout << 0.000098 << endl; 9.8e-005
}
Output Format for Real Numbers
• There are three modes for output of real
values:
- Default : depending on size of number,
default
chooses between, scientific and
fixed.
- Scientific: for very small or very large
numbers.
- Fixed: which is the way we are used to
seeing
real numbers.
Manipulators
double a = 123.545;
cout << fixed << showpoint;
cout << setw(10) <<setprecision(3) << a
<< endl;
123.5
Output Format for Real Numbers …
Example 2: What output is generated by the following
program segment?
double a = 123.545;
cout << fixed; // showpoint removed
cout << setw(8) << setprecision(3) << a
<< endl;
123.545
Output Format for Real Numbers …
Example 2: …….
double a = 123.545;
cout << fixed; // showpoint removed
cout << setw(6) << setprecision(2) << a
<< endl;
1) 123.54
2) 123.5
Output Format for Real Numbers …
Example 2: ……
double a = 123.545;
cout << fixed; // showpoint removed
cout << setw(2) << setprecision(2) << a
<< endl;
Example 2
double r = 2.0, s;
int a = 5, b = 2, c;
c = a/b;
s = a/b;
s = a/r;
c = a/r; //warning
Implicit Type Conversion …
Example 3
char c = 'a'; //ascii code = 97
int a = 16;
double d = 1.5;
cout << (c + a + d) << endl;
// Result will be 114.5. How?
Explicit Type Conversion
13.53
f = d * a;
b = d * a;
Explicit Type Conversion …
Exercise
Each statement uses type conversion. Give
the value assigned to x.
a) int x; b) int x, y=20; 76
c) char x;
d) int x, y =20;
x = char(65);
x = int(3.8) * y; 60
f) char ch =‘F’;
int x;
x = ch;
Arithmetic Assignment
Each of the numerical operators +, - *, /
and % can be combined with the
assignment operator to create an
arithmetic assignment operator.
• total_tax += state_tax; is equivalent
to
total_tax = total_tax + state_tax;
• count = count - 1; can be written as
count -= 1;
• twice = twice * 2; can be written as
twice *= 2;
Arithmetic Assignment …
salary + = bonus;
Arithmetic Assignment …
Fill in the values for the objects a and b.
#include <iostream>
using namespace std;
void main()
{
int a = 20, b;
a -= 4; // a = ______
a = 20;
b = 4;
a += b; // a = ______
b *= a; // b = ______
a %= b + 6; // a = ______
}
Practical Work
C++ systems provide a header file climits, which
contains declarations of constants related to the
specific computer and machine on which you are
working. Two of these constants are INT_MAX and
INT_MIN, the largest and smallest int values for your
particular computer. Write a proram to print out the
values of INT_MAX and INT_MIN. Be sure to include
appropriate comments in your program, and use
indentation.
Practical Work
• Write a C++ program that converts a Celsius temperature
to its Fahrenheit equivalent. The formula is
Fahrenheit =9/5 Celsius +32
Make the Celsius temperature a named constant so that
its value can be changed easily. The program should print
both the values of Celsius temperature and its equivalent
Fahrenheit, with appropriate identifying messages. Be
sure to include comments in your program, choose
meaningful identifiers, and use indentation.