ASHOOO
ASHOOO
ASHOOO
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.
#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”);
}
#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.
(c) 2
(d) std::cout << "x + 1"<< '\n';
(d) x+1
(e) std::cout << 'x'+ 1 << '\n';
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
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.
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.
#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
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.
ANS: #include<iostream>
#include<iomanip>
using namespace std;
int main() {
int a, i, c;
THE END