Home Work 2 (FALL-2023)
Home Work 2 (FALL-2023)
2
PF (FALL-2023)
Content:
1) C++ operators
2) Practices codes set precision an set width
3) Operators
4) Practice Codes of operators
5) Read Chapter 2 and 3
6) ASCII table
7) Read Chapter 2 and 3 (3.7 for iomanip Page 138 to 148)
Read homework carefully from start until End. You can attempt it in multiple times. You must read
Topic 3.7 for iomanip Page 138 to 148 before attempting this homework.
C++ is/has:
1. Strictly typed
2. Static typed
3. Compiled language
4. Faster and resource and performance efficient code.
5. Designed to develop Operating system and embedded software
6. Curly brackets { … } used for blocks
7. Block have local scope
8. Collection of statements/Commands
9. Statement terminator “;”
10. Contains most of programming constructs
11. Best programming language for teaching and learning?
12. Case sensitive language
Remember:
Data is stored in binary form in system memory (Von Neumann Architecture).
By default data is signed (negative and positive)
Representation of binary data is in the form of 2’s complement
Integral data [char (1byte), short (2 bytes), int (4 bytes) and long (8 bytes)] are by default
signed and stored in 2’s complement binary form.
We can make Integral data unsigned explicitly by using reserved keyword unsigned.
Floating-point data float (4-bytes), double (8-bytes) and long double (16-bytes) are
represented in binary notation using IEEE-32 bit and IEEE-64 bit floating point binary
representation.
Address of a variable does not depend upon type of a variable. It depends upon the
architecture of System either 4 byte (32 bit) or 8 byte (64-bit).
Operator precedence vs operator associativity
Operator Arity.
Run following programs in separate .cpp files and carefully study and understand it.
……………………………………………………code.cpp…………………………………………………..
#include <iostream>
#include<cstdlib>
#include<time.h>
#include<fstream>
int main()
{
int integer1; // first number to be input by user
int integer2; // second number to be input by user
int integer3; //Third number to be input by user
int sum; // variable in which sum will be stored
int Average; // variable in which Average will be stored
Average = sum / 3;
cout << "\nSum is :" << sum << endl; // print sum
//…………………………………………1.cpp……………………………………………..
#include <iostream>
#include<iomanip>
int main ()
float f = 12.5544;
cout<<setprecision(6)<<f<<endl;
cout<<setprecision(5)<<f<<endl;
cout<<setprecision(4)<<f<<endl;
cout<<setprecision(3)<<f<<endl;
cout<<setprecision(2)<<f<<endl;
cout<<setprecision(1)<<f<<endl;
cout<<endl;
return 0;
//…………………………………………………….2.cpp…………………………
#include <iostream>
#include<iomanip>
int main ()
float f = 12.5544;
cout<<setprecision(6)<<fixed<<f<<endl;
cout<<setprecision(5)<<fixed<<f<<endl;
cout<<setprecision(4)<<fixed<<f<<endl;
cout<<setprecision(3)<<fixed<<f<<endl;
cout<<setprecision(2)<<fixed<<f<<endl;
cout<<setprecision(1)<<fixed<<f<<endl;
cout<<endl;
return 0;
//…………………………………………………………..3.cpp…………………………………………….
/* This program will explain how to print output using setprecision and setw. This program asks for sales
figures for 3 days. The total sales are calculated and displayed in a table.*/
#include <iostream>
#include <iomanip>
int main()
cout << "Day 1: " << setw(8) << day1 << endl;
cout << "Day 2: " << setw(8) << day2 << endl;
cout << "Day 3: " << setw(8) << day3 << endl;
cout << "Total: " << setw(8) << total << endl;
return 0;
///…………………………………………………..4.cpp…………………………..
/* This program explains how to print Pattern using setfill() and setw. */
#include <iostream>
#include<iomanip>
int main() {
…………………………………………………………………..operators………………………………………………………………………
+a
a = b -a
a += b a + b
a -= b a - b a == b a[b]
a *= b a * b a != b *a
++a
a /= b a / b !a a < b &a a(...)
--a
a %= b a % b a && b a > b a->b a, b
a++
a &= b ~a a || b a <= b a.b ? :
a--
a |= b a & b a >= b a->*b
a ^= b a | b a <=> b a.*b
a <<= b a ^ b
a >>= b a << b
a >> b
Special operators
1 :: Scope resolution
a++ a-- Suffix/postfix increment and decrement
a[] Subscript
3
&a Address-of
Sizeof Size-of
4 .* ->* Pointer-to-member
5 a*b a/b a%b Multiplication, division, and remainder
9
Left-to-right
> >= For relational operators > and ≥ respectively
15 || Logical OR
Right-to-left
Compound assignment by product, quotient, and
*= /= %=
remainder
17 , Comma Left-to-right
First, solve by hand and then program these expressions and see output on screen?
1. 1777 / 5 % 36 / 13
2. 10 / 2 - 3
3. 8 + 12 * 2 - 4
4. 4 + 17 % 2 - 1
5. 6-3*2+7–1
6. 28 / 4 - 2
7. 6 + 12 * 2 - 8
8. 4+8*2
9. (6 + 12) * 2 - 8
10. 6 + 17 % 3 - 2
11. 2 + 22 * (9 - 7)
12. (8 + 7) * 2
13. (16 + 7) % 2 - 1
14. 12 / (10 - 6)
15. (19 - 3) * (2 + 2) / 4
16. !((7 / 12 < 15) || (8 * 0 && 10)) + (19.5 < 30 - 10)
17. 7 + 7 == 70 / 5 * 1 + 25 % 5
18. (5 > 7) * 10 + 5 * 2 / 2 < 5
19. 14 / (11 - 4)
20. 9 + 12 * (8 - 3)
21. (6 + 17) % 2 – 1
22. (9 - 3) * (6 + 9) / 3
23. ~(3 & 3 & 14)
1)
#include <iostream>
#include<iomanip>
int main() {
double d = 33.00;
int x = 15;
return 0;
2)
#include <iostream>
#include<iomanip>
int main() {
double d = 3.00;
int x = 12;
return 0;
3)
#include <iostream>
#include<iomanip>
int main() {
a = a / b;
b = b - a;
b = b / 2;
a = a * b;
4)
#include <iostream>
#include<iomanip>
int main() {
a = a / b;
b = b - a;
b = b / 2;
a = a * b;
5)
#include <iostream>
#include<iomanip>
int main() {
int z = 5, j = 7, k = 6, n = 3;
6)
#include <iostream>
#include<iomanip>
int main()
int i, j , k = 3;
i -= j -= k;
return 0;
7)
#include <iostream>
#include<iomanip>
return 0;
8)
#include <iostream>
#include<iomanip>
int main() {
short i = 32769;
return 0;
Run following programs in separate .cpp files and carefully study and understand it.
//…………………………………………………… 1.cpp…………………………………………………………
#include <iostream>
#include<iomanip>
int main() {
const double I;
int n;
I = 3.14159265358979;
cout << I * I;
//…………………………………………………… 2a.cpp…………………………………………………………
#include <iostream>
int main ()
int a=10;
short b=10;
long c=10;
return 0;
//…………………………………………………… 2b.cpp…………………………………………………………
#include <iostream>
int main ()
float a = 10.01;
double b = 10.01;
cout << "value of Integer a is " << a << " size is " << sizeof(a) << endl;
cout << "value of short c is " << b << " size is " << sizeof(b) << endl;
cout << "value of long c is " << c << " size is " << sizeof(c) << endl;
cout << "size of integer Literal is 10 and its size is " << sizeof(10.01) << endl;
return 0;
//…………………………………………………… 3.cpp…………………………………………………………
#include <iostream>
#include<iomanip>
int main() {
int x = 4;
int y = 6;
double z = 7;
int num2 = z / y * x / 2 * 10 + (y * x + 2) / z;
cout << "\n\tThe value of expression 1 is " << num2 << "." << endl;
cout << "\n\tThe value of expression 2 is " << num3 << "." << endl;
Run following programs in separate .cpp files and carefully study and understand it.
//……………………………………………………………..1.cpp…………………………………………………
#include <iostream>
#include<iomanip>
int main ()
cout<<endl;
cout<<setw(20)<<left<<"Hello";
cout<<setw(20)<<right<<"bye";
cout<<endl;
return 0;
//……………………………………………………………..2.cpp…………………………………………………
#include <iostream>
#include<iomanip>
int main() {
///…………………………………………………..4.cpp…………………………..
#include <iostream>
int main()
return 0;
///…………………………………………………..5.cpp…………………………..
/* This program explains how to print Pattern using setfill() and setw. */
#include <iostream>
#include<iomanip>
int main() {
Challenge question: Find the errors in the following code Segment and correct this code?
int main()
float 5f=1.1
int a = 15;
cout>>"\tSize of a is"<<sizeof(a;
cout>>\t"Size of f is"<<Sizeof(5f);
int a =15;
cout>>"\tSize of a is<<sizeof(a);