You are on page 1of 44

Mirpur University Of Science And Technology

ASSINGMENT NO#02

NAME: Zuhaib Ahmad

Class: 01 Semester

Roll No: FA20BEE068

Section: B

Subject: Computing Programing

Teacher: SIR USMAN

Course Code: EE-112


Introduction to
Computing

Chapter no. 04

Question 01:

Is the literal 4 a valid C++ expression?

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:

Is the variable x a valid C++ expression?

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:

Is x + 4 a valid C++ expression? Yes, it is a

valid C++ expression.

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:

Sort the following binary operators in order of high to low precedence:

+, -, *, /, %, =.

*>/>%>+>->=

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

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;
mod = x % y;
cout << x << “ + ” << y << “ = ” << sum << ‘\n’;
cout << x << “ - ” << y << “ = ” << diff << ‘\n’;
cout << x << “ * ” << y << “ = ” << pro << ‘\n’;
cout << x << “ / ” << y << “ = ” << div << ‘\n’;
cout << x << “ % ” << y << “ = ” << mod << ‘\n’;
}

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:

Given the following declaration:


int x = 2;
Indicate what each of the following C++ statements would print?

(a) std::cout << "x"<< '\n';


x
(b) std::cout << 'x'<< '\n';
x
(c) std::cout << x << '\n';
2
(d) std::cout << "x + 1"<< '\n';
x+1
(e) std::cout << 'x'+ 1 << '\n';
121
(f) std::cout << x + 1 << '\n';
3

Question 09:

Sort the following types in order from narrowest to widest: int, double, float, long, char?

char < int < float < long < double

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:

What is printed by the following statement:

std::cout << /* 5 */ 3 << '\n';?

3. Because anything written in // is ignored as comments.

Question 12:

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 + 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:

How are single-line comments different from block comments?

Single line comments are written in single line and enclosed by // while block comments are
written in multi lines and enclosed by /*.

Question 14:

Can block comments be nested?

No.

Question 15:

Which is better, too many comments or too few comments?

Using many comments are better as they would help the reader to understand the program.

Question 16:

What is the purpose of comments?

Comments are added in the program to help the reader understand it better.

Question 18:

Why is human readability such an important consideration?

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?

cin << n1 << n2;


cin and was not declared. Insertion symbols are used instead of extraction symbols. (Compile-
time error)
std::cout << n1+n2/2 << '\n';
The method to calculate is incorrect since n1+n2 should’ve been calculated in brackets and
then divided by 2 such as (n1 + n2) / 2. (Run-time error)

d1 = d2 = 0;
The variable d2 was not declared. (Compile-time error)

std::cout << n1 / d1 << '\n';


n1 is being divided by d1 which is equal to zero. (Logic-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:

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?

3 3

3 2

Because using ++x1 increases its value so after the increment statement the value changes.

Question 24:

The program is supposed to find the circumference given by user.

#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.

(b) The program doesn’t produce intended result. Why?


The program will not produce the intended result because the circumference is calculated
without using the value of radius (r). The equation C = 2*PI*r is calculating circumference and
for that we need radius which is collected in the next line so it would produce a logical-error.

(c) How it can be repaired?

#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 << "Please enter the first point: ";


cin >> left_paren >> x1 >> comma >> y1 >> right_paren;
cout << "Please enter the second point: ";
cin >> left_paren >> x2 >> comma >> y2 >> 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()

double burr, sal, milk, totcal, run;

cout << "The number of Bean Burritos, Bowls of Salad and Milkshakes eaten (Write in
corresponding order): ";

cin >> burr >> sal >> milk;

cout << '\n';

totcal = burr*357 + sal*185 + milk*388;

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:

What values can a variable of type bool assume?

Bool type variable can assume only two values 0 or 1. It’s either true or false.

Question 02:

Where does the term “Bool” originate?

“Bool” means Boolean which has only two values 1 or 0. The value is either set to true or false.

Question 03:

What is the integer equivalent to true in C++? The

integer 1 is considered true in C++.

Question 04:

What is the integer equivalent to false in C++?

The integer 0 is considered false in C++.

Question 05:

Is the value -16 interpreted as true or false?

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:

May an integer value be assigned to a bool variable?

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:

Can true be assigned to an int variable?

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;

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

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

(c) !(x < y)

x>y
(d) !(x <= y)

x>=y

(e) x < 10 && x > 20

Given condition is false.

(g) x != 0

Already simplified.

(h) x == 0

Already simplified.

Question 10:

What is the simplest tautology?

It’s a simple statement that’s true regardless of the condition, such as,

x < = 0 || x > 0

Question 11:

What is simplest contradiction?

This is the opposite of tautology. Contradiction is always false regardless of the condition, say

x < = 0 && x > 0

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.

#include <iostream> int


main(){
int value;
std::cout << "Enter value: "; std::cin >> value;
if (value > 1 && value < 100) std::cout << "OK"
<< '\n';
}

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:

#include <iostream> using


namespace std; int main() {
int value;
cout << "Please in value in the range from 1 to 5: "; cin >> value;

if (value == 1){
cout << "You entered one. \n"; }

else if (value == 2){


cout << "You entered two. \n"; }

else if (value == 3){


cout << "You entered three. \n"; }

else if (value == 4){


cout << "You entered four. \n"; }

else if (value == 5){


cout << "You entered five. \n"; }
else cout << "Entered value is out of range. \n";
}

Question 15:

Consider the following section of C++ code.

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:

#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';
}

Question 17:

What happens if user provides following input?

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;

if (d >= b && d >= c && d >= a && d >= e)


cout << "\n\nGreatest integer entered is: " << d << endl <<
endl;
if (d <=b && d <=c && d<=a && d<=e)
cout << "Smallest integer entered is: " << d << endl <<
endl;

if (e >= a && e >= b && e >= c && e >=d)


cout << "\n\nGreatest integer entered is: " << e << endl <<
endl;
if (e <=b && e <=c && e<=d && e<=a)
cout << "Smallest integer entered is: " << e << 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.

#include <iostream> using


namespace std; int main(){
int a, b, c, d, e;
cout << "Enter 5 integers: "; cin >> a >> b
>> c >> d >> e;

if (a >= b && a >= c && a >= d && a >= e)


cout << "\n\nGreatest integer entered is: " << a << endl <<
endl;
if (a <=b && a <=c && a<=d && a<=e)
cout << "Smallest integer entered is: " << a << endl <<
endl;

if (b >= a && b >= c && b >= d && b >= e)


cout << "\n\nGreatest integer entered is: " << b << endl <<
endl;
if (b <=a && b <=c && b<=d && b<=e)
cout << "Smallest integer entered is: " << b << endl <<
endl;

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”.

#include <iostream> using


namespace std; int main()
{
int num1, num2, num3, num4, num5; cout
<< "Enter 5 integers: ";
cin >> num1 >> num2 >> num3 >> num4 >> num5;

if (num1!=num2 && num1!=num3 && num1!=num4 && num1!=num5 &&


num2!=num3 && num2!=num4 && num2!=num5
num3!=num4 && num3!=num5 && num4!=num5)
cout << "\n\nALL UNIQUE\n\n";
else cout << "\n\nDUPLICATES\n\n";

}
While Loops

Program 01:

#include <iostream> using


namespace std; int main()
{
int x;
cout << "Enter positive number (Less than 100): "; cin >> x;

while(x <= 100){


cout << x << endl; x++;
}
}

Program 02:

#include <iostream> using


namespace std; int main(){
bool not_done = true; int
from, upto;
cout << "Enter range: "; cin >>
from >> upto; while(from <=
upto){
cout << from << endl; from+
+;
}
}

Program 03:

#include <iostream> using


namespace std; int main()
{
int x;
int minimum = 0;
cout << "Enter any number: "; cin >> x;
while(x >= 0){
cout << x << endl; x--;
}
}

Program 04:

#include <iostream> using


namespace std; int main(){
int value;
bool not_done = true;
while(not_done){
cout << "Enter value: "; cin >>
value;
if (value>=0 && value<=15) cout << "Value in range...................................\n";
else if (value<0 || value>15) not_done = false; else cout <<
"Invalid input..............................................\n";
}
cout << "Entered value is out of range...............................\n";
}

Program 05:

#include <iostream> using


namespace std; int main(){
int x = 0; while(x <=
10){
cout << "Hello World\n"; x++;
}
}

Program 06:

#include <iostream> using


namespace std; int main()
{
double num1, num2;
bool done = true; int
choice; while(done){
cout << "Enter two integers: "; cin >>
num1 >> num2;
cout << " -\n";

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:

#include <iostream> using


namespace std; int main()
{
bool not_done = true; double
number, sum = 0;
while(not_done){
cout << "Enter number (Entering zero would terminate the program): ";
cin >> number;
if (number!=0) sum = sum + number;
else if (number == 0) not_done = false; else cout <<
"Invalid input..............................................";
}
cout << "\n\nSum is: " << sum << endl << endl;
}

Program 08:

#include <iostream> using


namespace std; int main()
{
int number, i = 1, factorial = 1; cout << "Enter a
positive integer: "; cin >> number;
while ( i <= number) { factorial = factorial
* i;
++i;
}
cout<<"Factorial of "<< number <<" = "<< factorial << endl;
}

Program 09:

#include <iostream> using


namespace std; int main(){
int number, multiple = 1; cout <<
"Enter number: "; cin >> number;
cout << '\n';
while(multiple!=11){
cout << number << " * " << multiple << " = " << number * multiple;
cout << endl;
multiple ++;
}
}

Program 10:

#include <iostream> using


namespace std; int main()
{
int a = 0; char
b = 0;

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) {

std::cout << "*";


a++;
}
std::cout << '\n';

It will print 100 asterisks starting from 0 to 99.

6. How many asterisks does the following code fragment print?


int a = 0;
while (a <
100)
std::cout << "*";
std::cout << '\n';

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.

7. How many asterisks does the following code fragment print?


int a = 0;
while (a > 100)
{ std::cout <<
"*"; a++;
}
std::cout << '\n';

It would not print any asterisk because the condition in the while loop is going to be false
and would never get executed.

8. 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';
}

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.

9. How many asterisks does the following code fragment print?


int a = 0;
while (a < 100)
{ if (a % 5 ==

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';

It should print about 1 million asterisks on screen.

12. What is printed by the following code fragment?


int a = 0;
while (a <
100)
std::cout << a++;
std::cout << '\n';

This code fragment will print numbers starting from 0 to 99 without any space between them.

13. What is printed by the following code fragment?


int a = 0;
while (a >
100)

std::cout << a++;


std::cout << '\n';

Nothing is printed by the above code fragment.

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++;
}
}

17. What is printed by the following code fragment?


int a = 0;
while (a < 100);
std::cout << a++;
std::cout << '\n';

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){

cout << setw(4) <<


"*"; column++;
}
row++;
cout << endl;
}
}

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;

cout << "---- You are suppose to enter 20 values \n\n";

while (turns <= 20){


cout << "Enter value:
"; cin >> user_input;

if (turns == 1)
{ smallest =
user_input; largest
= user_input;
}

sum = sum + user_input;


if (user_input < smallest) smallest =
user_input; if (user_input >= largest) largest
= user_input;

turns++;
}

cout << "\n\nSum is: " << sum;


cout << "\n\nAverage is: " << sum /
20.0; cout << "\n\nLargest number is: "
<< largest;
cout << "\n\nSmallest number is: " <<
smallest; cout << endl << endl;
}

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 (){

double num = 0, sum = 0, largest,

smallest; int turn = 0;


while (num >= 0){
cout << "Enter value:
"; cin >> num;

if (turn == 0 && num < 0) goto

exit; if (turn == 0){


smallest = num;
largest = num;
}
if (num > 0 && num > largest) largest =
num; if (num > 0 && num < smallest)
smallest = num;

if (num > 0) sum = sum +


num; if (num >= 0) turn ++;
}

cout << "\n\nSum is: " << sum;


cout << "\n\nAverage is: " << sum /
turn; cout << "\n\nLargest number is: "
<< largest;
cout << "\n\nSmallest number is: " <<
smallest; cout << endl << endl;
if (num < 0 && turn == 0) exit:
cout << "\n\nNo numbers provided.\n\n";
}

21. Redesign Listing 6.21 so that it draws a sideways tree pointing right.

#include
<iostream> using
namespace std; int
main (){

cout << "Enter size of tree:


"; int size;
cin >> size;

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(){

int x, row, count;


cout << "Enter value for side
tree: "; cin >> x;

row = 0;
while (row < x){

count = 0;
while (count < x -
row){ cout << "
"; count++;
}

count = 0;
while (count < row +
1){ cout << "*";
count++;
}

cout << endl;


row++;
}

row = x;
while (row >= 0){

count = 0;
while (count < x -
row){ cout << "
";

count ++;
}

count = 0;
while (count < row +
1){ cout << "*";
count ++;
}

cout <<
endl;
row--;
}
}

You might also like