university of nizwa college of arts and ... university of nizwa college of arts and sciences...

21
1 UNIVERSITY OF NIZWA COLLEGE OF ARTS AND SCIENCES DEPARTMENT OF MATHEMATICAL AND PHYSICAL SCIENCES COMPUTER SCIENCE SECTION COMP151 INTRODUCTION TO ALGORITHMS AND PROGRAMMING INSTRUCTOR NAME: Dr. M.SYED SHAHUL HAMEED ASSIGNMENT 2 Date of Assignment: 21-03-2017 Date of Submit: 04-04-2017 STUDENT’S NAME: _________________________________________________ STUDENT’S ID: _____________________________________________________ 1. Study the following Program and show what it will print on the screen #include<iostream> using namespace std; int main() { cout<<”The works of Wolfgang\ninclude the following”; cout<<”\n The Turkish March”<<endl; cout<<”in G Minor.”<<endl; return(0); } OUTPUT: 2. What will the following program print on the screen? #include<iostream> using namespace std; int main() { int freeze=32,boil=212; freeze=0; boil=100; cout<<freeze<<endl<<boil<<endl; return(0); } OUTPUT:

Upload: vanmien

Post on 25-Mar-2018

223 views

Category:

Documents


1 download

TRANSCRIPT

1

UNIVERSITY OF NIZWA

COLLEGE OF ARTS AND SCIENCES

DEPARTMENT OF MATHEMATICAL AND PHYSICAL SCIENCES

COMPUTER SCIENCE SECTION

COMP151 INTRODUCTION TO ALGORITHMS AND PROGRAMMING

INSTRUCTOR NAME: Dr. M.SYED SHAHUL HAMEED

ASSIGNMENT – 2

Date of Assignment: 21-03-2017

Date of Submit: 04-04-2017

STUDENT’S NAME: _________________________________________________

STUDENT’S ID: _____________________________________________________

1. Study the following Program and show what it will print on the screen

#include<iostream>

using namespace std;

int main()

{

cout<<”The works of Wolfgang\ninclude the following”;

cout<<”\n The Turkish March”<<endl;

cout<<”in G Minor.”<<endl;

return(0);

}

OUTPUT:

2. What will the following program print on the screen?

#include<iostream>

using namespace std;

int main()

{

int freeze=32,boil=212;

freeze=0;

boil=100;

cout<<freeze<<endl<<boil<<endl;

return(0);

}

OUTPUT:

2

3. What will the following program print on the screen?

#include<iostream>

using namespace std;

int main()

{

int x=0,y=2;

x=y*4;

cout<<x<<"\n"<<y<<"\n";

return(0);

}

OUTPUT:

4. What will the following program print on the screen?

#include<iostream>

using namespace std;

int main()

{

cout<<”I am the incredible”;

cout<<”computing \nmachine”;

cout<<”\nand I will \namaze\n”;

cout<<”you.”;

return(0);

}

OUTPUT:

5. What will the following program print on the screen?

#include<iostream>

using namespace std;

int main()

{

cout<<”Be careful\n”;

cout<<”This might/n be a trick”;

cout<<”question\n”;

return(0);

}

OUTPUT:

3

6. What will the following program print on the screen?

#include<iostream>

using namespace std;

int main()

{

int a,x=23;

a=x%2;

cout<<x<<endl<<a<<endl;

return(0);

}

OUTPUT

7. Every complete statement ends with

a).period

b).# symbol

c).semicolon

d).ending brace

8. Which of the following statement is correct?

a).#include(iostream)

b).#include{iostream}

c).#include<iostream>

d).#include[iostream]

e).all the above

9. Every C++ program must have a

a).cout statement

b).function main

c).#include statement

d).all the above

10. Preprocessor directives begin with

a).#

b).!

c).<

d).*

e).none of the above

4

11. There are number of syntax errors in the following program. Locate as many as

you can

12. Write a program that displays the following pattern

*

***

*****

*******

Program Errors

#include<iostream>

using name space std;

int main();

}

int a;b;c;

a=3

b=4

c=a+b

Cout<<”THE VALUE OF C IS”<<C;

return;

{

5

13. Write a program that displays the following pattern

*

***

*****

*******

*****

***

*

14. What does the following code print?

cout<<”*\n**\n***\n****\n*****”<<endl;

OUTPUT:

6

15. What, if anything, prints when each of the following C++ statements is

performed? If nothing prints, then answer “nothing”. Assume x=2 and y=3.

a). cout<<x;

b).cout<<x+x;

c).cout<<”x=”;

d).cout<<”x=”<<x;

e).cout<<x+y<<”=”<<y+x;

f).z=x+y;

g).cin>>x>>y;

h).//cout<<”x+y=”<<x+y;

i).cout<<”\n”;

16. Write a program that displays the following pattern

* * * * * * * *

* * * * * * * *

* * * * * * * *

* * * * * * * *

* * * * * * * *

* * * * * * * *

* * * * * * * *

* * * * * * * *

7

17. Write a program that displays the following pattern

*********

* *

* *

* *

* *

* *

* *

* *

*********

8

18. What does the following code print?

#include<iostream>

using namespace std;

int main()

{

cout<<"Hello"

<<"How are you"<<

endl;

cout<<"Good Morning"

<<"Have a nice day"

<<"Welcome"<<endl<<

"Session Starts Now"<<

"\n";

cout<<"**********"<<endl<<"#######"<<

"\n"<<"\"\"\"\"\"\"\"\n\?\?\?\?\?\?\?";

return(0);

}

OUTPUT:

19. Check the following code is correct or not? If it is correct write the output

otherwise mention where it is having errors.

Program Error

#include<iostream>;

Using namespace std

int main();

{

int 7try;

cout<<Enter the value<<\n;

cin>>7try;

cout<<7try;

return (0);

}

9

OUTPUT:

20. Check the following code is correct or not? If it is correct write the output

otherwise mention where it is having errors

#include<iostream>

int main()

{

int a,b,c,d;

a=10;

b=15;

c=20;

d=25;

std::cout<<"VALUE FOR a IS :"<<a<<"\n";

std::cout<<"VALUE FOR b IS :"<<b<<"\n";

std::cout<<"VALUE FOR c IS :"<<c<<"\n";

std::cout<<"VALUE FOR d IS :"<<d<<"\n";

a=a+5;

b=b+10;

c=c+15;

d=d+20;

std::cout<<"VALUE FOR a IS :"<<a<<"\n";

std::cout<<"VALUE FOR b IS :"<<b<<"\n";

std::cout<<"VALUE FOR c IS :"<<c<<"\n";

std::cout<<"VALUE FOR d IS :"<<d<<"\n";

return(0);

}

OUTPUT:

10

21. Check the following code is correct or not? If it is correct write the output

otherwise mention where it is having errors

#include<iostream>

int main()

{

std::cout<<"This is a C++ Program\n";

std::cout<<"This is a C++"<<"\n"<<"Program";

std::cout<<"This\nis\na\nC++\nProgram\n";

std::cout<<"This\tis\ta\tC++\tProgram\n";

int x;

int y;

int z;

int result;

std::cout<<"Enter three Integers:";

std::cin>>x>>y>>z;

result=x*y*z;

std::cout<<"The Product is :"<<result;

return(0);

}

OUTPUT:

11

22. What header file must be included in programs using cin?

23. Assume value is an integer variable. If the user enters 3.14 in response to the

following programming statement, what will be stored in value?

cin>>value;

a).3.14

b).3

c).0

d).Nothing. An error message displayed

24. Which of the following are illegal variable, and why?

a).x

b).99bottles

c).july97

d).theSalesFigureForFiscalYear98

e).r&d

f).grade_report

g).dayOFWeek

h).3dgraph

i)._employee_num

j).__welcome

k).Mixture#3

l).return

25. Which of the following are illegal variable, and why?

a).const

b).y=z

c).xyz123

d).Bill

e).ThisIsALongOne

f).Sue's

g).two-way

h).int

i).so_is_this_one

j). amount

k).2ndclass

12

26. Write a single C++ statement or line that accomplishes each of the following

a). Print the message “Enter two numbers”

b). Assign the product of variables b and c to variable a

c). Input three integer values from the keyboard into integer variables a, b and c

d). Print the size of short integer

e). Calculate

BMI= Weight in Pounds X 703

Height in inches X Height in inches

f). Print an Uppercase Z

g). Declare variables age, number and max as integer with max initialized to 45

h). Initialize the constant integer variable count to 50

i). Initialize the character variable ch to F

27. Identify the following Literals

a). 23UL

b). 4567

c) 8.2345L

d). ‘c’

e).0xBB

f). 1234.786

g). -1234

h). 057

i). “WELCOME”

j). 4.78345E-5f

k).34567L

l). “Hai Ready to do the Work”

13

28. State which of the following are legal and which are illegal. If illegal, explain your

answer

a). The following are all valid variable names: _under_bar, m928134, t5, j7,

her_sales, his account total, a, b, c, z, z2

b). The statement cout<<”a=5;”; is a typical example of an assignment statement

c). The following are invalid variable names 3g, 87, 67h2, h22, 2h

d). cout<<45;

e). cin>>88;

f). Variable is a location in the computer’s memory that may contain different

values at various times throughout the execution of a program

g). char ‘ch’;

h). cout<<’\n’;

i). cout<<’G’;

j). cin>>miles per litter;

k). x+y+z=v;

l). float main();

29. How would each of the following numbers be represented in E notation?

a). 3.287 X 106

b). -978.65 X 1012

c). 7.65491 X 10-3

d). -58710.23 X 10-4

30. Write a program to read the following pieces of information, each on a separate line :

Your ID

Your Age

Your Sex

Your Telephone number

Your Height

Your Weight

Your ZIP code

Use only a single cout statement to display all of this information

14

31. A customer in a store purchasing five items. The prices are

Price for Item1 = 5.5 OMR

Price for Item2 = 2.6 OMR

Price for Item3 = 7.8 OMR

Price for Item4 = 12.6 OMR

Price for Item5 = 10.0 OMR

Write a Program that holds the prices of the five items in five variables. Display

each item’s price, the subtotal of the sale, the amount of sales tax, and total.

Assume the sales tax is 6%.

15

32. An electronic company sells circuit boards at a 40 percent profit. Write a program

that will calculate the selling price of a circuit board that costs 15.9 OMR. Display

the result on the screen.

33. A program has the following variable definitions.

long miles;

int feet;

float inches;

Check the above statements are correct or not?

Write one cin statement that reads a value into each of these variables.

34. What does the following code print?

cout<<”\”Run, Spot, run, \” she said.”;

35. Does the following program calculate the correct values? If not give the reason.

#include<iostream>

using namespace std;

int main()

{

short int var1=25000;

unsigned short int var2=25000;

unsigned short int var3=30000;

var1=(var1*5)/3;

var2=(var2*5)/3;

var3=(var3*5)/2;

cout<<var1<<endl;

cout<<var2<<endl;

cout<<var3;

return(0);

}

16

36. Check the following code is correct or not? If it is correct write the output

otherwise mention where it is having errors - 2 Marks

#include<iostream>

using namespace std;

int main()

{

int count=10;

cout<<count<<'\n';

cout<<count<<'\t';

count=count-1;

cout<<count<<'\n';

cout<<count<<'\t';

count=count-1;

cout<<count<<'\t';

count=count-1;

cout<<count<<'\n';

cout<<count<<'\t';

count=count-1;

cout<<count<<'\t';

count=count-1;

cout<<count<<'\t';

count=count-1;

cout<<count<<endl;

return(0);

}

Output:

17

37. Find the result for the following expressions

Expression Result

5+2*4

10/2-3

8+12*2-4

4+17%2-1

6-3*2+7-1

6+3*5

12/2-4

9+14*2-6

5+19%3-1

(6+2)*3

14/(11-4)

9+12*(8-3)

(6+17)%2-1

(9-3)*(6+9)/3

12+22%4*4+12-6/2

45+2*3*4%2-3+72

38. Convert the following Algebraic Expression into C++ Expression

Algebraic Expression C++ Expression

18

39. Write a program to check the given number is less than 100 or greater than 100 by

using ternary operator.

40. Write a program to find the biggest among three numbers through conditional

operator.

19

41. What is the output of the following program?

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

20

42. Evaluate the following expression.

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)

A=(s*r)/(p+q)%t*r+s*(r%4)+s/t

A=t*p*q/s%r+(s*t)/2

A++

A=p*q*r*s*t%(p+q)/(s-q)+p+t+s

A=p+t+(q*r)%2-t

A=q/1+p*(t%3)/r/s+7

A=p+q+t-(s%q)+t*2+p+t*2

++A

43. Evaluate the following expression. Assume a=6,b=2,c=8,d=7, Ch=’A’, t=’X’, m=’K’

Expression Result

‘X’==m

(a>=b)&&(c!=b)

(d<b)||(m!=t)

(Ch==’A’)||(t==’E’)||(c>=0)

(a+=b)==c

d%=b

!((a*=5)>c)

(c-=a)!=a

(d>b)&&(c<a)&&(m==’k’)

44. If a is 5, b is 10, c is 15 and d is 0 what are the truth values of the following

expressions?

a). c==a+b

b). a!=7

c). b<=a

d). a>5

e). a+d>=c-b

f). d/a<c*b

45. If a is 5, b is 10, c is 15 and d is 0 what are the truth values of the following

expressions?

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

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

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

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

21

46. True or False?

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

false or 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.

c). A logical expression is constructed from relational expressions by use of the

logical operators not, and and or.

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.

e). It is perfectly all right to use variables of different data types in the same

arithmetic expression. f). A variable must be declared before it can be used

g). Variable names may be begin with a number

h). Variable names may be up to 31 characters long

i). A left brace (open brace) in a C++ program should always be followed by a

right brace (close brace) later in program.

j).You will be unable to execute your program until you correct all the syntax

errors in it

k). C++ language is not a case sensitive language

l).The #define directive is not a C++ statement but a directive for the pre-

processor

m). Manipulators are operators used with the extraction operator >> to manipulate

the way of data display.

n).When new value placed into variable, overwrites the old value

o). Once the default (right justified) is changed with left alignment then all the

setw(n) follows the left alignment

p). The keyword left should be given before setw(n).

q).We cannot use integer data type variable to store characters

r). cin requires the user to press the [ENTER] key when finished entering data.