Instructor's Name / Room / Ext. / Email / Office Hours
11A-5 / 544

COMP151 INTRODUCTION TO ALGORITHMS AND PROGRAMMING

ANSWERS FOR ASSIGNMENT – III

/
College of Arts and Sciences
Department of Mathematical and Physical Science
Section: Computer Science
Semester: Spring-2013

1.  Find the result for the following expressions - 4Marks

Expression / Result
5+2*4 / 13
10/2-3 / 2
8+12*2-4 / 28
4+17%2-1 / 4
6-3*2+7-1 / 6
6+3*5 / 21
12/2-4 / 2
9+14*2-6 / 31
5+19%3-1 / 5
(6+2)*3 / 24
14/(11-4) / 2
9+12*(8-3) / 69
(6+17)%2-1 / 0
(9-3)*(6+9)/3 / 30
12+22%4*4+12-6/2 / 29
45+2*3*4%2-3+72 / 114

2.  True or False: It’s perfectly all right to use variables of different data types in the same arithmetic expression -1 Mark

(FALSE)

3.  An arithmetic assignment operator combines the effect of what two operators? -1 Mark

1. ARITHMETIC OPERATORS (+,-,*,/,%)

2. ASSIGNMENT OPERATOR (=)

4.  Convert the following Algebraic Expression into C++ Expression -2 Marks

Algebraic Expression / C++ Expression
/ a/(b*c)
/ (k*a*(t1-t2))/b
/ b*b-4*a*c or (b*b)/(4*a*c)
/ ((a*a)+1)/(b*c)
/ 1/(1+(x*x))
/ (a+b)/(c+d)

5.  Write a statement that uses an arithmetic assignment operator to increase the value of the variable temp by 23. Write the same statement without the arithmetic assignment operator. - 2 Marks

temp+=23;

temp=temp+23;

6.  Write a program to check the given number is less than 100 or greater than 100 by using ternary operator. - 2 Marks

#include<iostream>

using namespace std;

int main()

{

int n;

cout<”Enter the value for Number”<endl;

cin>n;

(n<100)?cout<”The number is less than 100”:cout<”The number is greater than 100”;

return(0);

}

7.  Write a program to find the biggest among three numbers through conditional operator. - 2 Marks

#include<iostream>

using namespace std;

int main()

{

int x,y,z;

cout<”Enter the value for x,y and z”<endl;

cin>x>y>z;

((x>y)?((x>z)?cout<”X is big”:cout<”Z is big”):((y>z)?cout<”Y is big”:cout<”Z is big”));

return(0);

}

8.  What is the output of the following program? - 2 Marks

#include<iostream>

using namespace std;

int main()

{

int x=10,y=25,c,d;

c=++x;

cout<"Value of x is"<x<endl;

cout<"Value of x is"<x<endl;

cout<"Value of c is"<c<endl;

d=y++;

cout<"Value of y is"<y<endl;

cout<"Value of y is"<y<endl;

cout<"Value of d is"<d<endl;

c++;

cout<"Value of c is"<c<endl;

++d;

cout<"Value of d is"<d<endl;

cout<"Value of x is"<x++<endl;

cout<"Value of x is"<x<endl;

cout<"Value of y is"<++y<endl;

cout<"Value of y is"<y<endl;

cout<"Value of x is"<--x<endl;

cout<"Value of x is"<x<endl;

cout<"Value of y is"<y--<endl;

cout<"Value of y is"<y<endl;

return(0);

}

Output:

Value of x is11

Value of x is11

Value of c is11

Value of y is26

Value of y is26

Value of d is25

Value of c is12

Value of d is26

Value of x is11

Value of x is12

Value of y is27

Value of y is27

Value of x is11

Value of x is11

Value of y is27

Value of y is26

9.  Evaluate the following expression. - 3 Marks

Assume p=4,q=2,r=6,s=8,t=5

Expression / Result
A=s*p/q+r-t+s%q+(r*t)/p+(s+q) / 34
A=(s*r)/(p+q)%t*r+s*(r%4)+s/t / 35
A=t*p*q/s%r+(s*t)/2 / 25
A++ / 26
A=p*q*r*s*t%(p+q)/(s-q)+p+t+s / 17
A=p+t+(q*r)%2-t / 4
A=q/1+p*(t%3)/r/s+7 / 9
A=p+q+t-(s%q)+t*2+p+t*2 / 35
++A / 36

10.  Evaluate the following expression. -3 Marks

Assume a=6,b=2,c=8,d=7

Ch=’A’, t=’X’, m=’K’

Expression / Result
‘X’==m / 0 or false
(a>=b)&(c!=b) / 1 or true
(d<b)||(m!=t) / 1 or true
(Ch==’A’)||(t==’E’)||(c>=0) / 1 or true
(a+=b)==c / 1 or true
d%=b / 1
!((a*=5)>c) / 0 or false
(c-=a)!=a / 1 or true
(d>b)&(c<a)&(m==’k’) / 0 or false

11.  If a is 5, b is 10, c is 15 and d is 0 what are the truth values of the following expressions? -2 Marks

a). c==a+b - true

b). a!=7 - true

c). b<=a - false

d). a>5 - false

e). a+d>=c-b - true

f). d/a<c*b - true

12.  If a is 5, b is 10, c is 15 and d is 0 what are the truth values of the following expressions? -2 Marks

a). c==a+b||c==d - true

b). a!=7&c>= 6||a+c<= 20 - true

c). !(b<=12)&a%2==0 - false

d). !(a>5)||c<a+b - true

13.  True or False? -4 Marks

a).A condition or logical expression is an expression that can only take the values false or true. - true

b).A relational expression is constructed from arithmetic expressions combined by the relational operators less than, greater than, equal, not equal, greater than or equal and less than or equal. - true

c).A logical expression is constructed from relational expressions by use of the logical operators not, and and or. - true

d).sizeof() operator accepts one parameter, which can be either a type or a variable itself and returns(integer) the size in bytes of that type or object - true

1