Professional Documents
Culture Documents
ASSINGMENT NO#02
Class: 01 Semester
Section: B
Chapter no. 04
Question 01:
No, an expression is the one which involves any arithmetic operators. Since no operator is used
with 4, so it is an invalid expression.
Question 02:
No, an expression is the one which involves any arithmetic operators. Since no operator is used
with x, so it is an invalid expression.
Question 03:
Question 04:
What affect does the unary + operator have when applied to a numeric
expression?
It would have no effect on a numeric expression. The unary + operator only shows
completeness of the expression, when applied to a numeric value, variable, or expression, the
resulting value is no different from the original value of its operand.
Question 05:
+, -, *, /, %, =.
*>/>%>+>->=
Question 06:
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).
#include <iostream>
using std::cout;
using std::cin;
int main() {
int x, y, sum, diff, pro, div, mo
Question 07:
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.
#include <iostream>
using std::cout;
using std::cin;
int main() {
double x, y, sum, diff, pro, div;
cout << “Please enter the first number: “;
cin >> x;
cout << “Please enter the second number: “;
cin >> y;
sum = x + y;
diff = x - y;
pro = x * y;
div = x / y;
cout << x << “ + ” << y << “ = ” << sum << ‘\n’;
cout << x << “ - ” << y << “ = ” << diff << ‘\n’;
cout << x << “ * ” << y << “ = ” << pro << ‘\n’;
cout << x << “ / ” << y << “ = ” << div << ‘\n’; }
The program will generate an error if we use modules operator with double-precision floating
points. The % operator is used with integers.
Question 08:
Question 09:
Sort the following types in order from narrowest to widest: int, double, float, long, char?
Question 10:
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.
i1 + i2 = 7
i1 / i2 = 0
i2 / i1 = 2
i1 * i3 = -6
d1 + d2 = 7
d1 / d2 = 0.4
d2 / d1 = 2.5
d3 * d1 = -1
d1 + i2 = 7
i1 / d2 = 0.4
d2 / i1 = 2.5
i2 / d1 = 2.5
i1 / i2 * d1 = 0
d1 * i1 / i2 = 0.8
d1 / d2 * i1 = 0.8
i1 * d1 / d2 = 0.8
i2 / i1 * d1 = 4
d1 * i2 / i1 = 5
d2 / d1 * i1 = 5
i1 * d2 / d1 = 5
Question 11:
Question 12:
i1 * (i2 + i3) = 4
d1 + d2 + (d3 / 3) = 6.8333
3 * (d1 + d2) * (d1 - d3) = 52.5
(3 + 4 + 5) / 3 = 4
d1 + (d2 * d3) = -0.5
d1 + d2 * d3 = -0.5
Question 13:
Single line comments are written in single line and enclosed by // while block comments are
written in multi lines and enclosed by /*.
Question 14:
No.
Question 15:
Using many comments are better as they would help the reader to understand the program.
Question 16:
Comments are added in the program to help the reader understand it better.
Question 18:
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.
Question 19:
Consider the program which contains errors and answers which line contains what kind of
error?
d1 = d2 = 0;
The variable d2 was not declared. (Compile-time error)
n1*n2 = d1;
The symbol * cannot be used as a variable. (Compile-time error)
The last statement of program does not contain any error.
Question 20:
What distinguishes a compiler warning from a compiler error? Should you be concerned about
warnings? Why or why not?
When a compiler tells that this program cannot be executed, it means that there is an error of
some sort and so program will not execute until that error is removed. On the other hand,
warnings are not a matter of concern, if something is wrong, the compiler automatically
corrects it before executing the program.
Question 21:
What are the advantages to enhancing the warning reporting capabilities of the
compiler?
It helps in writing a correct program and also helps in getting the required output from a
program. Warning shows us that some particular part in the program may cause some
problems while getting required output.
Warnings are good because they let you find unintentional behavior before they turn into bugs.
Question 22: 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
Question 23:
y1 = ++x1;
y2 = x2++;
3 3
3 2
Because using ++x1 increases its value so after the increment statement the value changes.
Question 24:
#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?
The warning shows that cout and cin are not declared variables.
#include <iostream>
Int main () {
Double C, r;
std::cout << “Please enter the circle’s radius: “;
std::cin >> r;
std::cout << “Circumference is: “ << C << ‘\n’; }
Question 25:
Write a C++ program that receives two mathematical points from the user and
computes and prints their midpoint.
#include <iostream>
using std::cout;
using std::cin;
int main()
{
double x1, y1, x2, y2;
char left_paren, comma, right_paren;
cout << "The Mid-Point of " << left_paren << x1 << comma << y1 << right_paren << " and " <<
left_paren << x2 << comma << y2 << right_paren << " is " << "(" << (x1+x2)/2 <<
"," << (y1+y2)/2 << ")";
}
Question 26:
Write a program that calculate the calories of energy ingested and calculates how much
you have to run or walk in order to expend that energy.
#include <iostream>
using std::cout;
using std::cin;
int main()
cout << "The number of Bean Burritos, Bowls of Salad and Milkshakes eaten (Write in
corresponding order): ";
cout << "You have ingested " << totcal << " Calories" << '\n';
run = totcal/100.0;
cout << "You will have to run " << run << " miles to expend that much energy";
}
Chapter No. 05
Question 01:
Bool type variable can assume only two values 0 or 1. It’s either true or false.
Question 02:
“Bool” means Boolean which has only two values 1 or 0. The value is either set to true or false.
Question 03:
Question 04:
Question 05:
It is interpreted as true and 1 will be assigned to the bool variable. Because every value other
than 0 is considered true in C++.
Question 06:
Yes, we can assign an integer value to a bool variable but every value which is not zero is
considered as
true and only 0 is assigned false.
Question 07:
Yes, true can be assigned to an int variable and it will get the value of 1.
Question 08:
Given the following declarations:
int x = 3, y = 5, z = 7;
expressions:
(a) x == 3 = True
(e) x != y – 2 = False
Question 09:
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
x>y
(d) !(x <= y)
x>=y
(g) x != 0
Already simplified.
(h) x == 0
Already simplified.
Question 10:
It’s a simple statement that’s true regardless of the condition, such as,
x < = 0 || x > 0
Question 11:
This is the opposite of tautology. Contradiction is always false regardless of the condition, say
Question 12:
Write a C++ program that requests an integer value from the user. If the value is between1
and 100 inclusive, print “OK” otherwise, do not print anything.
Question 13:
Write a C++ program that requests an integer value from the user. If the value is between1
and 100 inclusive, prints “OK” otherwise, prints “Out of range.”?
#include<iostream> int
main()
{
int x;
std::cout << “Enter integer: “;
std::cin >> x; if(x>1 &&
x<100)
cout << ”OK” << ‘\n’;
else cout << “Out of Range\n”;
}
Question 14:
Find and point out the logical errors in the program given in the question. Also repair the
program.
Errors:
1. The declared variable is not used to take input from the user (OR) the variable “month” was not
declared.
2. All the compound statements are written without curly braces. The line followed by the “if”
statement will get executed as related to “if” statement. But “else” would generate an error
because there would be no “if” statement before that.
Correct Program:
if (value == 1){
cout << "You entered one. \n"; }
Question 15:
if (i < j) { if (j < k)
i = j;
else j =
k;
}
else {
if (j > k) j = i; else i = k;
std::cout << “i = “ << i << “ j = “ << j << “ k = “ << k; }
What would be the result for the following value of above used integers.
1. i is 3, j is 5 and k is
7 i = 5, j = 5, k = 7
2. i is 5, j is 7 and k is
3 i = 5, j = 3, k = 3
3. i is 7, j is 3 and k is
5 i = 5, j = 3, k = 5
4. i is 5, j is 3 and k is
7 i = 7, j = 3, k = 7
Question 16:
Consider the following C++ program that prints one line of text:
Question 17:
1. 3
wow 3
2. 21
whoa 21
3. 5
6
4. 17
27
5. -5
wow -5
cout << "Smallest integer entered is: " << c << endl <<
endl;
Question 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.
endl;
if (c >= b && c >= a && c >= d && c >= e)
cout << "\n\nGreatest integer entered is: " << c << endl << if (c <=b && c
Question 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”.
}
While Loops
Program 01:
Program 02:
Program 03:
Program 04:
Program 05:
Program 06:
cout << "1. Add\n" << "2. Subtract\n" << "3. Multiply\n"
<< "4. Divide\n" << "5. Exit the program\n\n"; cin >> choice;
cout << " -\n";
if (choice==1) cout << "Addition is: " << num1 + num2 << endl << endl;
else if (choice==2) cout << "Subtraction is: " << num1 - num2 << endl << endl;
else if (choice==3) cout << "Multiplication is: " << num1 * num2
<< endl << endl;
else if (choice==4) cout << "Division is: " << num1 / num2 << endl
<< endl;
else if (choice==5) done = false;
}
}
Program 07:
Program 08:
Program 09:
Program 10:
while (a<=250){
cout << a << '\t' << b << endl; a++;
b++;
}
}
ChapterNo.06
1. In Listing 6.4 (addnonnegatives.cpp) could the condition of the if statement have used > instead of
>= and achieved the same results? Why?
Because if we use >= symbol and entered 0, it would have done no addition in the program. So
we used only > because 0 is of no use for this program to input.
2. In Listing 6.4 (addnonnegatives.cpp) could the condition of the while statement have used >
instead of >= and achieved the same results? Why?
No, because the purpose of the program was to end the program if the user entered a
negative integer. Since 0 is not considered as negative integer so we used >= to include 0 as
well.
3. Use a loop to rewrite the following code fragment so that it uses just one std::cout and one '\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';
#include
<iostream> using
namespace std; int
main(){
int number = 2;
while (number <=
16){
cout << number <<
'\n'; number =
number + 2;
}
}
4. 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?
Its not a good thing because we want to keep prompting the user to enter a number unless a
negative is entered to end the program. Moving std::cin out of the loop will ask the user to
enter the number only once and that’s not what we want to do.
5. How many asterisks does the following code fragment print?
int a = 0;
while (a < 100) {
It will print infinite number of asterisks because while loop becomes an infinite loop
since there’s no increment in the variable. The while condition is going to be satisfied every
time and the loop will keep on executing.
It would not print any asterisk because the condition in the while loop is going to be false
and would never get executed.
It would also print infinite amount of asterisks because its an infinite loop. This would
happen because there’s no increment in variable “a” and while condition for “a” is going
to get satisfied every time and the program will keep looping.
0) std::cout <<
"*"; a++;
}
std::cout << '\n';
This code fragment will print 20 asterisks on the screen in a single row.
10. How many asterisks does the following code fragment print?
int a = 0;
while (a < 100)
{ int b = 0;
while (b < 40) {
if ((a + b) % 2 == 0)
std::cout <<
"*"; b++;
}
std::cout <<
'\n'; a++;
}
It would print about 2000 asterisks on screen. 100 rows with each row containing 20 asterisks.
11. How many asterisks does the following code fragment print?
int a = 0;
while (a < 100)
{ int b = 0;
while (b < 100)
{ int c = 0;
while (c < 100)
{ std::cout <<
"*"; c++;
}
b+
+;
}
a+
+;
}
std::cout << '\n';
This code fragment will print numbers starting from 0 to 99 without any space between them.
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';
}
int n = 0, m =
100; while (n !=
m) { std::cin >>
n;
std::cout << "n = " << n <<
'\n'; if (n < 0)
break; }
15. 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.
int x = 100,
y; while (x >
0) { std::cin
>> y; if (y
== 25) { x--;
continue;
}
std::cin >> x;
std::cout << "x = " << x << '\n';
}
Ans:
#include
<iostream> using
namespace std; int
main(){
int x, y, count =
0; while (count <=
100){
cin >> y;
if (y == 25) count+
+; cin >> x;
if (y!=25)
cout << "x = " << x << '\n';
}
16. Suppose you were given some code from the 1960s in a language that did not support structured
statements like while. 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.
int i = 0;
top: if (i >= 10)
goto end;
std::cout << i <<
'\n'; i++;
goto
top;
end:
#include
<iostream> using
namespace std; int
main(){
int i = 0;
while(i <
10){
cout << i <<
endl; i++;
}
}
It would not print anything since the while loop does not contain any statements. But since, it
the condition is going to be true every time the program will keep running in the background
but there would be no result shown on the screen.
18. 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.
#include<iostream
>
#include<iomanip>
using namespace
std; int main()
{
int x, row;
cout << "Enter an integer:
"; cin >> x;
row = 0;
while (row <
x){ int column =
0; while (column
< x){
19. 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 values entered.
#include
<iostream> using
namespace std; int
main(){
int turns = 1;
double user_input, sum = 0, smallest, largest;
if (turns == 1)
{ smallest =
user_input; largest
= user_input;
}
turns++;
}
20. Write a C++ program that allows the user to enter any number of non-negative 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 terminating 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”.
#include <iostream>
using namespace
std; int main (){
21. Redesign Listing 6.21 so that it draws a sideways tree pointing right.
#include
<iostream> using
namespace std; int
main (){
int row = 0;
while (row <=
size){ int
column = 0;
while (column <
row){ cout <<
"*"; column++;
}
cout << endl;
row++;
}
row = (size -
1); while (row
>= 0){
int column = 0;
while (column <
row){ cout <<
"*"; column++;
}
cout <<
endl;
row--;
}
}
22. Redesign Listing 6.21 so that it draws a sideways tree pointing towards left?
#include
<iostream> using
namespace std; int
main(){
row = 0;
while (row < x){
count = 0;
while (count < x -
row){ cout << "
"; count++;
}
count = 0;
while (count < row +
1){ cout << "*";
count++;
}
row = x;
while (row >= 0){
count = 0;
while (count < x -
row){ cout << "
";
count ++;
}
count = 0;
while (count < row +
1){ cout << "*";
count ++;
}
cout <<
endl;
row--;
}
}