Anda di halaman 1dari 11

Deitel & Deitel

Exercise # 2
2.2) ________ model the interactions among objects in a system with an emphasis
on when these interactions occur. a) Class diagrams b) Sequence diagrams c) Communication diagrams d) Activity diagrams

2.3) Which of the following choices lists stages of a typical software life cycle in
sequential order? a) Design, analysis, implementation, testing b) Design, analysis, testing, implementation c) Analysis, design, testing, implementation d) Analysis, design, implementation, testing

2.4) answers: a. // Calculate the product of three integers b. int x; int y; int z; int result; c. cout << "Enter three integers: "; d. cin >> x >> y >> z; e. result = x * y * z; f. cout << "The product is " << result << endl; g. return 0; 2.5) answers:
// Calculate the product of three integers #include <iostream> // allows program to perform input and output // function main begins program execution void main() { clrscr(); int x; // first integer to multiply int y; // second integer to multiply int z; // third integer to multiply int result; // the product of the three integers cout << "Enter three integers: "; // prompt user for data cin >> x >> y >> z; // read three integers from user result = x * y * z; // multiply the three integer store result cout << "The product is " << result << endl; // print result; end line getch();// indicate program executed successfully } // end function main

2.6) answers: a.
Error: Semicolon after the right parenthesis of the condition in the if statement. Correction: Remove the semicolon after the right parenthesis. The semicolon after the right parenthesis is a null (or empty) statement a statement that does nothing.

b.
Error: The relational operator =>. Correction: Change => to >=, and you may want to change "equal to or greater than" to "greater than or equal to" as well.

2.9) answers:
a) Cout<<Enter two numbers; b) Int a=b*c; c) \\ a pro gram performs a payroll calculation d) Int a,b,c; cin>>a; cin>>b; cin>>c;

2.10) answers:
a) The output will be 2. b) The output will be 4. c) The output will be x=. d) The output will be x=2. e) The output will be 5=5. f) IT assigns the value of sum of x and y to z. g) IT will enter the value for x and y from user. h) It will be considered as comment by compiler i) It will shift next output console to next line.

2.11) answers: Fill in the blanks in each of the following: a) What arithmetic operations are on the same level of precedence as multiplication? ______. b) When parentheses are nested, which set of parentheses is evaluated first in an arithmetic expression? ______. c) A location in the computer's memory that may contain different values at various times throughout the execution of a program is called a ______. 2.12) answers:
These statements will print, a) b) 2 4

c) d) e) f) g) h) i)

x= x=2 5-5 nothing nothing nothing nothing

2.13) answers:
The answer is b) p = i+j+k+7; & a) cin>>b>>c>>d>>e>>f;

2.14) answers:

The correct statement for ax3 +7 is e) y = a * (x*x*x)+7;

2.15) answers:
a) The value of x will be 15. b) The value of x will be 3. c) The value of x is 324.

2.16) answers:
Void main() { clrscr(); Int a,b;

#include<constream.h>

cout<< Enter the first number\n\t; cin>>a; cout<<\nEnter second number; cin>>b; cout<<\n\tThe sum is<<a+b; cout<<\nThe product is <<a*b; cout<<\n\tThe difference is<<a-b; cout<<\nThe quotient is <<a/b; getch(); }

2.17) answers:
a) #include<constream.h> void main() { clrscr(); cout<<"1,2,3,4 1,2 2,3 3,4"; getch(); } b) #include<constream.h> void main() { clrscr(); cout<<"1,2,3,4"<<" 1,2 "<<"2,3"<<" 3,4"; getch(); } c) #include<constream.h> void main() { clrscr(); cout<<"1,2,3,4 "; cout<<"1,2 "; cout<<"2,3 "; cout<<"3,4"; getch(); }

2.18) answers:

#include<constream.h> void main()

{ clrscr(); int a,b; cout<<"Enter the first integer\n\t"; cin>>a; cout<<"Enter the second integer\n\t"; cin>>b; if(a>b)

cout<<a<<"\n\tis larger"; if(b>a) cout<<b<<"\tis larger"; if(a==b) cout<<"both numbers are equal"; getch(); }

2.19) answers:
#include<constream.h> void main() { clrscr(); int a,b,c; cout<<"Input three different integers"; cin>>a>>b>>c; cout<<"\nsum is "<<a+b+c; cout<<"\naverage is "<<(a+b+c)/3; cout<<"\nproduct is if(a>b,a>c,b>c) cout<<"\nlargest is "<<a<<"\nsmallest is "<<c; if(b>a,b>c,c>a) cout<<"\nlargest is "<<b<<"\nsmallest is "<<a; if(c>a,c>b,a>b) cout<<"\nlargest is "<<c<<"\nsmallest is getch(); } "<<b; "<<a*b*c;

2.20) answers:
#include<constream.h> void main() { clrscr();

float r,t; t=3.14159; cout<<"Please enter the radius of sphere\n\n"; cin>>r; cout<<"\n\nThe Area of sphere is\n\t\t\a"<<4*t*r*r; cout<<"\n\nThe circumference of sphere is\n\t\t\a"<<(4*t*r*r*r)/3; cout<<"\n\nThe diameter of sphere is"<<2*r; getch(); }

2.21) answers:
#include<iostream.h> #include<conio.h> void main() { clrscr(); cout<<"*********\n\n*\t*\n\n*\t*\n\n*\t*\n\n*\t*\n\n*\t*\n\n*********\n";

cout<<setw(8)<<"***\n\n"<<setw(3)<<"*"<<setw(6)<<"*\n\n"<<setw(2)< <"*"<<setw(8)<<"*\n\n"<<setw(2)<<"*"<<setw(8)<<"*\n\n"<<setw(2)<< "*"<<setw(8)<<"*\n\n"<<setw(2)<<"*"<<setw(8)<<"*\n\n"<<setw(3)<<"* "<<setw(6)<<"*\n\n"<<setw(6)<<"***\n";

cout<<setw(6)<<"*\n\n"<<setw(7)<<"***\n\n"<<setw(8)<<"*****\n\n"<<s etw(6)<<"*\n\n"<<setw(6)<<"*\n\n"<<setw(6)<<"*\n\n"<<setw(6)<<"*\n\n ";

cout<<setw(10)<<"*\n\n"<<setw(6)<<"*"<<setw(6)<<"*\n\n"<<setw(4)< <"*"<<setw(10)<<"*\n\n"<<setw(2)<<"*"<<setw(14)<<"*\n\n"<<setw(4) <<"*"<<setw(10)<<"*\n\n"<<setw(6)<<"*" <<setw(6)<<"*\n\n"<<setw(8)<<"*"; getch(); }

2.22) answers:

The output is * ** *** **** *****

2.23) answers:
#include<constream.h> void main() { clrscr(); int a; cout<<"Enter any number\n\t"; cin>>a; if(a%2==0) cout<<"\n\tThe number is Even"; if(a%2!=0) cout<<"\n\tThe number is odd"; getch(); }

2.24) answers:
#include<constream.h> void main() { clrscr(); int a; cout<<"Enter any number\n\t"; cin>>a; if(a%2==0) cout<<"\n\tThe number is Even"; if(a%2!=0) cout<<"\n\tThe number is odd"; getch(); }

2.25) answers:
#include<constream.h> void main() { clrscr();

int a,b; cout<<"enter two integers\n\t"; cin>>a>>b; if(a%b==0) cout<<"both are multiple of each other"; if(a%b!=0) cout<<"the numbers are not multiple of each other"; getch(); }

2.26) answers:
#include<constream.h> #include<iomanip.h> void main() { clrscr(); cout<<"* * * * * * * * * *\n * * * * * * * * * *\n* * * * * * * * * *\n * * * * * * * * * *\n* * * * * * * * * *\n * * * * * * * * * *\n* * * * * * * * * *\n * * * * * * * * * *"; getch(); }

2.26)(b) answers:
#include<constream.h> #include<iomanip.h> void main() { clrscr(); cout<<"* * * * * * * * * *\n"; cout<<" * * * * * * * * * *\n"; cout<<"* * * * * * * * * *\n"; cout<<" * * * * * * * * * *\n"; cout<<"* * * * * * * * * *\n"; cout<<" * * * * * * * * * *\n";

cout<<"* * * * * * * * * *\n"; cout<<" * * * * * * * * * *"; getch(); }

2.27) answers:
#include<constream.h> void main() { clrscr(); char x; cout<<\nEnter any letter; cin>>x; cout<<\nIn ASCII it is<<static_cast< int >( 'A' ); getch(); }

2.28) answers:
#include<constream.h> void main() { clrscr(); long int a,b,c,d,e; cout<<"\nEnter five digit number : "; cin>>a; b=a/10000; cout<<"\n\n"<<b; //b=4 c=a/1000; b=b*10; c=c-b; //c=42 //b=40 //c=2 //a=42339

cout<<"\t"<<c; d=a/100; c=b+c; c=c*10; d=d-c; //d=423 //c=42 //c=420 //d=3

cout<<"\t"<<d; e=a/10; c=c*10; d=d*10; d=d+c; e=e-d; //e=4233 //c=4200 //d=30 //d=4230 //e=3

cout<<"\t"<<e; e=e*10; d=d*10; e=e+d; a=a-e; //e=30 //d=42300 //e=42330 //a=9

cout<<"\t"<<a; getch(); }

2.29) answers:
#include<constream.h> void main() { clrscr(); long int x=1; cout<<"\ninteger\tsquare\tcube\n"<<x<<"\t"<<x*x<<"\t"<<x*x*x<<"\n"; x++; cout<<x<<"\t"<<x*x<<"\t"<<x*x*x<<"\n"; x++; cout<<x<<"\t"<<x*x<<"\t"<<x*x*x<<"\n"; x++; cout<<x<<"\t"<<x*x<<"\t"<<x*x*x<<"\n"; x++; cout<<x<<"\t"<<x*x<<"\t"<<x*x*x<<"\n"; x++;

cout<<x<<"\t"<<x*x<<"\t"<<x*x*x<<"\n"; x++; cout<<x<<"\t"<<x*x<<"\t"<<x*x*x<<"\n"; x++; cout<<x<<"\t"<<x*x<<"\t"<<x*x*x<<"\n"; x++; cout<<x<<"\t"<<x*x<<"\t"<<x*x*x<<"\n"; x++; cout<<x<<"\t"<<x*x<<"\t"<<x*x*x; getch(); }