You are on page 1of 29

FUNDAMENTAL

C++
PROGRAMING
ASSIGNMENT NO 2
CHAPTER NO 4
Expression and Arithmetic’s
Q1: Is the literal 4 a valid C++ expression?
Answer:-
No,
An expression is the one which involves any arithmetic
operators. Since no operator has been used with 4, so it
is an invalid expression.

Q2. Is the variable x a valid C++ expression?


Answer:-
No,
An expression is the one which involves any arithmetic
operators. Since no operator has been used with x, so it is
an invalid expression.

Q3. Is x + 4 a valid C++ expression?


Answer:-
Yes, It is a valid expression.
Q4: What affect does the unary + operator have when
applied to a numeric expression?
Answer:-
It has no effect on a numeric expression.

Q5: Sort the following binary operators in order of high


to low precedence: +, -, *, /, %, =.
Answer:-
( *> / >% >+> - > = )

Q6: Write a C++ program that receives two integer values


from the user. The program then should print the
sum (addition), difference (subtraction), product
(multiplication), quotient (division), and remainder after
division (modulus). Your program must use only integers.
A sample program run would look like (the user enters
the 10 and the 2 after the colons, and the
program prints the rest):
Answer:

#include<iostream>
using namespace std;
int main()
{
int x,y,sum,diff,pro,div,rem,mod;
cin>>x;
cin>>y;
sum=x+y;
diff=x-y;
pro=x*y;
div=x/y;
rem=x\y;
mod=x%y;
cout <<sum<<diff<<pro<<div<<rem<<mod;
system(“pause”);
}

Please enter the first number: 10


Please enter the second number: 2
10 + 2 = 12
10 - 2 = 8
10 * 2 = 20
10 / 2 = 5
10 % 2 = 0

Q7: Write a C++ program that receives two double-


precision floating-point values from the user. The
program then should print the sum (addition), difference
(subtraction), product (multiplication), and
quotient (division). Your program should use only
integers.
A sample program run would look like (the user enters
the 10 and the 2.5 after the colons, and the
program prints the rest):
Please enter the first number: 10
Please enter the second number: 2.5
10 + 2.5 = 12.5
10 - 2.5 = 7.5
10 * 2.5 = 25
10 / 2.5 = 4
Can you explain the results it produces for all these
operations? What happens if you attempt to
compute the remainder after division (modulus) with
double-precision floating-point values?
Answer:-

#include<iostream>
using namespace std;
int main()
{
int x,y,sum,diff,pro,div,rem,mod;
cin>>x;
cin>>y;
sum=x+y;
diff=x-y;
pro=x*y;
div=x/y;
rem=x\y;
mod=x%y;
cout <<sum<<diff<<pro<<div<<rem<<mod;
system(“pause”);
}
If we calculate modulus with double floating point
values, then the numbers after decimal point will be
expelled automatically.

Q8. Given the following declaration:


int x = 2;
Indicate what each of the following C++ statements
would print?
(a) std::cout << "x"<< '\n';
(a) x

(b) std::cout << 'x'<< '\n';

(c) std::cout << x << '\n';

(c) 2
(d) std::cout << "x + 1"<< '\n';

(d) x+1
(e) std::cout << 'x'+ 1 << '\n';

(f) std::cout << x + 1 << '\n';


(f) 3

Q9:- Sort the following types in order from narrowest to


widest: int, double float, long, char?

ANSWER :Char < Int < float < long < double
Q10. Given the following declarations:
int i1 = 2, i2 = 5, i3 = -3;
double d1 = 2.0, d2 = 5.0, d3 = -0.5;
Evaluate each of the following C++ expressions.
(a) i1 + i2 = 7
(b) i1 / i2 = 0
(c) i2 / i1 = 2
(d) i1 * i3 = -6
(e) d1 + d2 = 7
(f) d1 / d2 = 0.4
(g) d2 / d1 = 2.5

(h) d3 * d1 = -1
(i) d1 + i2 = 7
(j) i1 / d2 =
(k) d2 / i1
(l) i2 / d1
(m) i1/i2*d1
(n) d1*i1/i2
(o) d1/d2*i1
(p) i1*d1/d2
(q) i2/i1*d1
(r) d1*i2/i1
(s) d2/d1*i1
(t) i1*d2/d1

Q11. What is printed by the following statement:


std::cout << /* 5 */ 3 << '\n';?
Answer:-
3. Because anything written in // is ignored as
comments.

Q12. Given the following declarations:


int i1 = 2, i2 = 5, i3 = -3;
double d1 = 2.0, d2 = 5.0, d3 = -0.5;
Evaluate each of the following C++ expressions.

(a) i1 + (i2 * i3)


(b) i1 * (i2 + i3)
(c) i1 / (i2 + i3)
(d) i1 / i2 + i3
(e) 3 + 4 + 5 / 3
(f) (3 + 4 + 5) / 3
(g) d1 + (d2 * d3)
(h) d1 + d2 * d3
(i) d1 / d2 - d3
(j) d1 / (d2 - d3)
(k) d1 + d2 + d3 / 3
(l) (d1 + d2 + d3) / 3
(m) d1 + d2 + (d3 / 3)
(n) 3 * (d1 + d2) * (d1 - d3)
Q13: How are single-line comments different from block
comments?
Answer:- single line is written in single line and enclosed
by(//)while block is written in multi lines and enclosed by
(/*).
14. Can block comments be nested?
Answer: no

15. Which is better, too many comments or too few


comments?
Answer:-
In light of the question, to many comments are better as
they help in understanding the program.

Q16. What is the purpose of comments?


Answer:-
. Comments are added in the program to help the reader
understand it better.
Q17. The programs in Listing 3.4 (variable.cpp), Listing 4.4
(reformattedvariable.cpp), and Listing 4.5
(reformattedvariable2.cpp) compile to the same machine
code and behave exactly the same. What
makes one of the programs clearly better than the
others?
Q18. Why is human readability such an important
consideration?
Answer:-
. It is an important consideration because the reader
should be easily able to understand the program. That’s
why comments are used in a program.
Q19. Consider the following program which contains
some errors. You may assume that the comments
within the program accurately describe the program’s
intended behavior.
#include <iostream>
int main() {
int n1, n2, d1; // 1
cin << n1 << n2;
std::cout << n1 + n2 << '\n';
std::cout << n1+n2/2 << '\n';

d1 = d2 = 0;
std::cout << n1/d1 << '\n';
n1*n2 = d1;
std::cout << d1 << '\n';
}
each line listed in the comments, indicate whether or not
a compile-time, run-time, or logic error
is present. Not all lines contain an error.

Q20. What distinguishes a compiler warning from a


compiler error? Should you be concerned about
warnings? Why or why not?
Answer:-
. A compiler tells that this thing in a program is wrong
and it will automatically correct it while executing the
program, this is called warning. While a compiler if tells
that this program cannot be executed, it means that
there is an error.
Warnings are not a matter of concern. Because if
something is wrong, the compiler automatically corrects
it before executing the program.
21. What are the advantages to enhancing the warning
reporting capabilities of the compiler?
Answer:-
. It helps in writing a correct program and also helps in
getting the required output from a program.
Q22. Write the shortest way to express each of the
following statements.
(a) x = x + 1; = x++
(b) x = x / 2; =x/=2
(c) x = x - 1; =x--
(d) x = x + y; =x+=y
(e) x = x - (y + 7); =x-=(y+7)
(f) x = 2*x; = x*=2
(g) number_of_closed_cases = number_of_closed_cases +
2*ncc; = n+=2*ncc

Q23. What is printed by the following code fragment?


int x1 = 2, y1, x2 = 2, y2;
y1 = ++x1;
y2 = x2++;
std::cout << x1 << " " << x2 << '\n';
std::cout << y1 << " " << y2 << '\n';
Why does the output appear as it does?
Answer:-
.22
33
Q24. Consider the following program that attempts to
compute the circumference of a circle given the
radius entered by the user. Given a circle’s radius, r, the
circle’s circumference, C is given by the
formula:
C = 2pr
#include <iostream>
int main() {
double C, r;
const double PI = 3.14159;
C = 2*PI*r;
cout >> "Please enter the circle's radius: ";
cin << r;
std::cout << "Circumference is " << C << '\n';
}
(a) The compiler issues a warning. What is the warning?
(a) It shows the warning that variable x is unidentified.
(b) The program does not produce the intended result.
Why?
(b)
(c) How can it be repaired so that it not only eliminates
the warning but also removes the logic
error?
ANSWER:
Double c,r;
Cin>>r;
const pi=3.14
C=2*pi*r;
Cout<<C;
Q25. In mathematics, the midpoint between the two
points (x1;y1) and (x2;y2) is computed by the formula
_
x1+x2
2
;
y1+y2
2
_
Write a C++ program that receives two mathematical
points from the user and computes and prints
their midpoint.
A sample run of the program produces
Please enter the first point: (0,0)
Please enter the second point: (1,1)
The midpoint of (0,0) and (1,1) is (0.5,0.5)
The user literally enters "(0,0)" and "(1,1)" with the
parentheses and commas as shown. To see how
to do this, suppose you want to allow a user to enter the
point (2:3;9), assigning the x component
of the point to a variable named x and the y component
to a variable named y. You can add the
following code fragment to your program to achieve the
desired effect:
Food Calories
Bean burrito 357
Salad w/dressing 185
Milkshake 388
Table 4.7: Calorie content of several fast food items
double x, y;
char left_paren, comma, right_paren;
std::cin >> left_paren >> x >> comma >> y >> right_paren;
If the user literally types (2.3,9), the std::cin statement
will assign the ( character to the
variable left_paren. It next will assign 2.3 to the variable
x. It assigns the , character to the
variable named comma, the value 9 to the y variable, and
the ) character to the right_paren
variable. The left_paren, comma, and right_paren
variables are just placeholders for the
user’s input and are not used elsewhere within the
program. In reality, the user can type in other
characters in place of the parentheses and comma as
long as the numbers are in the proper location
relative to the characters; for example, the user can type
*2.3:9#, and the program will interpret
the input as the point (2:3;9).

Q26
. Table 4.7 lists the Calorie contents of several foods.
Running or walking burns off about 100 Calories
per mile. Write a C++ program that requests three values
from the user: the number of bean burritos,
salads, and shakes consumed (in that order). The
program should then display the number of miles
that must be run or walked to burn off the Calories
represented in that food. The program should run
as follows (the user types in the 3 2 1):
Number of bean burritos, bowls of salad, and milkshakes
eaten? 3 2 1
You ingested 1829 Calories
You will have to run 18.29 miles to expend that much
energy
Observe that the result is a floating-point value, so you
should use floating-point arithmetic to compute
the answers for this problem.

Q1. What values can a variable of type bool assume?


Answer:-
1 or 0.
Q2. Where does the term bool originate?
Answer:-
. bool means Boolean which has only two values 1 or 0.
Q3. What is the integer equivalent to true in C++?
Answer:-
.1
Q4. What is the integer equivalent to false in C++?
Answer:-
.0
Q5. Is the value -16 interpreted as true or false?
Answer:-
. It is interpreted as false.
Q6. May an integer value be assigned to a bool variable?
Answer:
-. Yes we can assign an integer value to a bool variable but any
value which is 0 or less than 0 is taken as false and value equal
to 1 or greater than 1 is taken as true.
Q7. Can true be assigned to an int variable?
Answer:-
. No. It cannot be assigned to an int variable.
Q8. Given the following declarations:
int x = 3, y = 5, z = 7;
bool b1 = true, b2 = false, b3 = x == 3, b4 = y < 3;
evaluate the following Boolean expressions:
(a) x == 3 = True
(b) x < y = True
(c) x >= y = False
(d) x <= y = True
(e) x != y – 2 = False
(f) x < 10 = True
(g) x >= 0 && x < 10 = True
(h) x < 0 && x < 10 = False
(i) x >= 0 && x < 2 = False
(j) x < 0 || x < 10 = True
(k) x > 0 || x < 10 = True
(l) x < 0 || x > 10 = False
(m) b1 = 1
(n) !b1 = 0
(o) !b2 = 1
(p) b1 && b2 = 0
Q9. Express the following Boolean expressions in simpler form;
that is, use fewer operators. x is an int.
(a) !(x == 2) => x!=2
(b) x < 2 || x == 2 => x<=2
(c) !(x < y) => x>y
(d) !(x <= y) => x>=y
(e) x < 10 && x > 20 => Cannot understand. The condition is
itself false.
(f) x > 10 || x < 20 =>
(g) x != 0
(h) x == 0
Q10. What is the simplest tautology?
Answer:-
. x<=0 || x>0
. Q11What is the simplest contradiction?
Answer:-
. x<=0 && x>0

Q12. Write a C++ program that requests an integer value from


the user. If the value is between 1 and 100
inclusive, print “OK;” otherwise, do not print anything.
Answer:-

#include<iostream>
using namespace std;
int main()
{
int x;
cin>>x;
if(x>1 && x<100)
cout<<”OK”<<endl;
}
Q13. Write a C++ program that requests an integer value from
the user. If the value is between 1 and 100
inclusive, print “OK;” otherwise, print “Out of range.”?
Answer:-.
#include<iostream>
using namespace std;
int main()
{
int x;
cin>>x;
if(x>1 && x<100)
cout<<”OK”<<endl
else
cout<<”ou of range”<<endl;
}
Q14. The following program attempts to print a message
containing the English word corresponding
to a given integer input. For example, if the user enters the value
3, the program should print
"You entered a three". In its current state, the program contains
logic errors. Locate the
problems and repair them so the program will work as expected.

#include <iostream>
int main() {
std::cout << "Please in value in the range 1...5: ";
int value;
std::cin >> value;
// Translate number into its English word
if (month == 1)
{
std::cout << "You entered a";
std::cout << "one";
std::cout << '\n';
}
else if (month == 2)
{
std::cout << "You entered a";
std::cout << "two";
std::cout << '\n';
}
else if (month == 3)
{
std::cout << "You entered a";
std::cout << "three";
std::cout << '\n';
}
else if (month == 4)
{
std::cout << "You entered a";
std::cout << "four";
std::cout << '\n';
}
else if (month == 5)
{
std::cout << "You entered a";
std::cout << "five";
std::cout << '\n';
}
else // Value out of range
{
std::cout << "You entered a";
std::cout << "value out of range";
std::cout << '\n';
}
}
15. Consider the following section of C++ code:
// i, j, and k are ints
if (i < j) {
if (j < k)
i = j;
else
j = k;
120
}
else {
if (j > k)
j = i;
else
i = k;
}
std::cout << "i = " << i << " j = " << j << " k = " << k << '\n';
Q15:What will the code print if the variables i, j, and k have the
following values?
(a) i is 3, j is 5, and k is 7
(b) i is 3, j is 7, and k is 5
(c) i is 5, j is 3, and k is 7
(d) i is 5, j is 7, and k is 3
(e) i is 7, j is 3, and k is 5
(f) i is 7, j is 5, and k is 3
16. Consider the following C++ program that prints one line of
text:
#include <iostream>
int main() {
int input;
std::cin >> input;
if (input < 10) {
if (input != 5)
std::cout << "wow ";
else
input++;
}
else {
if (input == 17)
input += 10;
else
std::cout << "whoa ";
}
std::cout << input << '\n';
}
Q16:
What will the program print if the user provides the following
input?
(a) 3
(b) 21
(c) 5
(d) 17
(e) -5
Q17. Why does the following section of code always print
"ByeHi"?
int x;
std::cin >> x;
if (x < 0);
std::cout << "Bye";
std::cout << "Hi\n";
18. Write a C++ program that requests five integer values from
the user. It then prints the maximum and
minimum values entered. If the user enters the values 3, 2, 5, 0,
and 1, the program would indicate
that 5 is the maximum and 0 is the minimum. Your program
should handle ties properly; for example,
if the user enters 2, 4, 2, 3, and 3, the program should report 2 as
the minimum and 4 as maximum.
19. Write a C++ program that requests five integer values from
the user. It then prints one of two
things: if any of the values entered are duplicates, it prints
"DUPLICATES"; otherwise, it prints
"ALL UNIQUE

Q1. In Listing 6.4 (addnonnegatives.cpp) could the condition of


the if statement have used > instead of
>= and achieved the same results? Why?
Answer:-
. Because if we use >= symbol and entered 0,it would have no
addition in the program. So we can use only > because 0 is of no
use for this program to input.
Q2. In Listing 6.4 (addnonnegatives.cpp) could the condition of
the while statement have used >instead of >= and achieved the
same results? Why?
Answer:-
.
Q3. Use a loop to rewrite the following code fragment so that it
uses just one std::cout an done'\n'.
std::cout << 2 << '\n'; std::cout << 4 << '\n'; std::cout << 6 << '\n';
std::cout << 8 << '\n'; std::cout << 10 << '\n'; std::cout << 12 <<
'\n'; std::cout << 14 << '\n'; std::cout << 16 << '\n';
Answer:-
. #include<iostream>
Using namespace std;
Int main()
{
Unsigned Int x;
Cin>>x;
While(x<=16){
Cout<<x<<endl;
}
x=x+2
}

Q4. In Listing 6.4 (addnonnegatives.cpp) what would happen if


the statement containing std::cin is moved out of the loop? Is
moving the assignment out of the loop a good or bad thing to
do? Why?
Answer:-
.
Q5. How many asterisks does the following code fragment print?
int a = 0;
while (a < 100) { std::cout << "*"; a++;
}std::cout << '\n';
Answer:-
. 100

Q6. How many asterisks does the following code fragment print?
int a = 0; while (a < 100)
std::cout << "*"; std::cout << '\n';
Answer:-
.1
Q7. How many asterisks does the following code fragment print?
int a = 0;
while (a > 100) { std::cout << "*"; a++;
}std::cout << '\n';
Answer:-
. Nothing
Q8. How many asterisks does the following code fragment print?
int a = 0;
while (a < 100) { int b = 0; while (b < 55) { std::cout << "*"; b++;
}std::cout << '\n';
}
Answer:-
. Approximately 5500 times.
Q9. How many asterisks does the following code fragment print?
Answer:-
int
a = 0;
while (a < 100) {
if (a % 5 == 0)
a++; std
Q10. How many asterisks does the following code fragment
print?
Answer:-
int a = 0;
while (a < 100) { int b = 0; while (b < 40) { if ((a + b) % 2 == 0) b++;
std::cout << "*";
}std::cout << '\n'; a++;
}
Q11. How many asterisks does the following code fragment
print?
Answer:-
(a < 100) { int b = 0; while (b < 100) { int c = 0; while (c < 100) {
std::cout << "*"; }a++; }b++; c++;
}std::cout << '\n';
Q12. What is printed by the following code fragment?
Answer:-
int a = 0; while (a < 100)
std::cout << a++; std::cout << '\n';
13. What is printed by the following code fragment?
int a = 0; while (a > 100)
std::cout << a++; std::cout << '\n';
14. Rewrite the following code fragment using a break
statement and eliminating the done variable. Your code should
behave identically to this code fragment.
bool done = false; int n = 0, m = 100; while (!done && n != m)
{ std::cin >> n; if (n < 0)
done = true;
std::cout << "n = " << n << '\n';
}
Q15. Rewrite the following code fragment so it eliminates the
continue statement. Your new code’s logic should be simpler
than the logic of this fragment.
Answer:-
int x = 100, y; while (x > 0) { std::cin >> y; if (y == 25) { x--;
continue; }std::cin >> x;
std::cout << "x = " << x << '\n';
}
Q16. Suppose you were given some code from the 1960s in a
language that did not support structured statements likewhile.
Your task is to modernize it and adapt it to C++. The following
code fragment has been adapted to C++ already, but you must
now structure it with a while statement to replace the gotos.
Your code should be goto free and still behave identically to this
code fragment.
Answer:-
int i = 0; top: if (i >= 10)
goto end;
std::cout << i << '\n'; i++;
end: goto top;
Q17. What is printed by the following code fragment?
int a = 0;
while (a < 100);
std::cout << a++; std::cout << '\n';
Q18. Write a C++ program that accepts a single integer value
entered by the user. If the value entered is less than one, the
program prints nothing. If the user enters a positive integer, n,
the program prints an n×n box drawn with * characters. If the
users enters 1, for example, the program prints
*
If the user enters a 2, it prints
****
An entry of three yields
***
***
***
and so forth. If the user enters 7, it prints
*******
*******
*******
*******
*******
*******
*******
that is, a 7×7 box of * symbols.
ANS: #include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int x, r, c, product;
cout << "enter a table size" << endl;
cin >> x;
c = 1;
while (c <= x) {
r = 1;
while (r <= x) {
product = r*c;
cout << setw(5) << product;
r++;
}
cout << endl;

c++;
}
system("pause");

Q19. Write a C++ program that allows the user to enter exactly
twenty double-precision floating-point values. The program then
prints the sum, average (arithmetic mean), maximum, and
minimum of the

Q20. Write a C++ program that allows the user to enter any
number of nonnegative double-precision floating-point values.
The user terminates the input list with any negative value. The
program then prints the sum, average (arithmetic mean),
maximum, and minimum of the values entered. The termi-
nating negative value is not used in the computations. If the first
number the user supplies is negative, the program simply prints
the text NO NUMBERS PROVIDED.

Q21. Redesign Listing 6.21 (startree.cpp) so that it draws a


sideways tree pointing right; for example, if the user enters 7,
the program would print
******
****
*****
******
*******
******
*****
****
***
***

ANS: #include<iostream>
#include<iomanip>
using namespace std;
int main() {
int a, i, c;

cout << "enter a integer" << endl;


cin >> a;
for (c = 1; c <= a; c++)
{
for (i = 1; i <= c; i++)
cout << "*";
cout << endl;
}
for (c = a; c >= 1; c--)
{
for (i = 1; i <= c; i++)
cout << "*";
cout << endl;
}
system("pause");
}

Q22. Redesign Listing 6.21 (startree.cpp) so that it draws a


sideways tree pointing left; for example, if the user enters 7, the
program would print
***
***
****
*****
******
*******
******
*****
****
******
Ans:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{ int a, i, c, s;
cout << "enter a integer" << endl;
cin >> a;
for (s = a, c = 1; c <= a; s--, c++)
{ cout << setw(s);
for (i = 1; i <= c; i++)
cout << "*";
cout << endl;
}
for (s = 1, c = 1; c <= a; s++, c++)
{
cout << setw(s);
for (i = a; i >= c; i--)
cout << "*";
cout << endl;
}
system("pause");
}

THE END

You might also like