Anda di halaman 1dari 13

Robert Lafore

Exercise # 1 Questions
1) Pascal, BASIC and C are procedural languages, while C++ is object oriented language.

2) A widget is to the blue print for a widget as an object is to a) b) c) d) A member function. A class. An operator. A data item.

3) Two major components of an object are data and functions that operate on that data. 4) In C++, a function contained within a class is called, a) b) c) d) A member function. An operator. A class function. A method.

5) Protecting data from access by unauthorized functions is called data hiding. 6) Which of the following are good reasons to use an object oriented language? a) b) c) d) You can define your own data type. Program statements are simple than in procedural languages. An OO program can be taught to correct its own errors. Its easier to conceptualize an OO program.

7) Objects model entities in the real world more closely than do functions. 8) True or False: A C++ program is similar to a C program expect for the details of coding. 9) Bundling data and functions together is called encapsulation. 10) When a language has the capability to produce new data types , it is said to be a) b) c) d) Reprehensible. Encapsulated. Overloaded. Extensible.

11) True or False: You can easily tell from any two lines of code, whether a program is in C++ or in C. 12) The ability of a function or operator to act in different ways on different data types is called polymorphism.

13) A normal C++ operator that acts in special ways on newly defined data types is said to be, a) b) c) d) Glorified. Encapsulated. Classified. Overloaded.

14) Memorizing the new terms used in C++ is, a) b) c) d) Critically important. Something you can return to later. The key to wealth and success. Completely irrelevant.

15) The unified Modeling language is, a) b) c) d) A program that builds physical models. A way to look at the organization of a program. The combination of C++ and FORTRAN. Helpful in developing software systems.

Robert Lafore
Questions
1) Dividing a program into functions a) b) c) d) Is the key to object-oriented programming Makes the program easier to conceptualize. May reduce the size of the program. Makes the program run faster.

Exercise # 2

2) A function body must be followed by parentheses. 3) A function body is defined by braces { }. 4) Why is the main( ) function special? Ans. It is the first function executed when the program starts.

5) A C++ instruction that tells the computer to do something is called a statement. 6) Write an example of a normal C++ comment and an example of an old-fashioned comment. Ans. Normal = \\this is a normal comment. Old-fashioned = \* this is an old-fashioned comment*\.

7) An expression a) b) c) d) Usually evaluates to a numerical value. Indicates the emotional state of the program. Always occurs outside a function. Mau be part of a statement.

8) Specify how many bytes are occupied by the following data types in 32-bit system: a) b) c) d) Type int = 4 Type float = 4 Type long double = 10 Type long = 4

9) True or False: A variable of type char can hold the value 301. 10) What kind of program elements are the following? a) 12 = integer constant. b) a character constant. c) Junglejim = variable name or identifier. d) Junglejim ( ) = function name. 11) Write a statement that display on the screen, a) The character x = cout<<x; b) The name jim = cout<<jim; c) The number 509 = cout<<509; 12) True or False: in an assignment statement, the value on the left of the equal sign is always equal to the value on the right side. 13) Write a statement that displays the variable George in a field ten characters wide. Ans. Cout<<setw(10)<<George; 14) What header file must you #include with your source file to use cout and cin? Ans. <iostream.h> 15) Write a statement that gets a numerical value from the key board and plaaces it in the variable temp. Ans. Cin>>temp; 16) What header file must you #include with your program to use setw? Ans. <iomanip.h>. 17) Two exceptions to the rule that the compiler ignores whitespace are the string constant and preprocessor directives. 18) True or false: its perfectly all right to use variables of different data types in the same arithmetic expression. 19) The expression 11%3 evaluates to 2. 20) An arithmetic assignment operator combines the effect of what two operators? Ans. Assignment (=) and arithmetic (+,*,-,/) 21) 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.

Ans. With arithmetic assignment operator temp + = 23; Without arithmetic assignment operator temp = temp + 23; 22) The increment operator increases the value of a variable by how much? Ans. It increases by 1.

23) Assuming var1 starts with the value of 20. What will be the following code print out? cout<<var1--; cout<<++var1; Ans. 20 & 20 each time. 24) In the example we have seen so far header files have been used for what purpose? Ans. Header files are used for declaration, data for library, objects and over loaded operators. 25) The actual code for library functions is contained in a library file.

Exercises Q 1 Answer)
#include<constream.h> void main() { clrscr(); long double g; //g stands for gallons cout<<"please enter the number of gallons\n\t"; cin>>g; cout<<"\nThe cube feet are\t\a"<<g*0.134; getch(); }

Q 2 Answer)
#include<constream.h> void main() { clrscr(); cout<<"\n1990\t135\n1991\t7290\n1992\t11300\n1993\t16200"; getch(); }

Q 3 Answer)
#include<constream.h> void main() { clrscr(); int a=10,b,c; b=a*2; c=b-1; cout<<a<<"\n"<<b<<"\n"<<c; getch(); }

Q 4 Answer)
#include<constream.h> void main() { clrscr(); cout<<"\n\tMy Favorite Poem\n\nTwinke twinkle little star,\n\n\thow i wonder what you are.\n\nup above up so high,\n\n\tLike a diamond in the sky.\n\nTwinke twinkle little star,\n\n\tHow i wonder what you are."; getch(); }

Q 5 Answer)
#include<constream.h> #include<ctype.h> void main() { clrscr(); char a; cout<<" enter a lower case letter\n\t"; cin>>a; cout<<"\n\t"<<islower(a); getch(); }

Q 6 Answer)
#include<constream.h> void main() { clrscr(); long double d,p,f,g,y;//d for dollars, p for pounds, f for franc, g for germen and y for yen. cout<<"\tPlease Enter your amount in U.S $\n\t"; cin>>d; p=d*0.672; cout<<"\n\tYour amount in pounds is = "<<p<<" Pounds"; f=d*5.814; cout<<"\n\tYour amount in francs is = "<<f<<" Francs"; g=d*1.172; cout<<"\n\tYour amount in Germen Duetschemark is = "<<g<<" Duetschemark"; y=d*104.712; cout<<"\n\tYour amount in japani yens is = "<<y<< "Yens";

getch(); }

Q 7 Answer)
#include<constream.h> void main() { clrscr(); long double c; cout<<"\n\tEnter temperature in celsius\t"; cin>>c; cout<<"\n\tTemper in Fahrenheit is = "<<9*(c+32)/5<<" Degrees"; getch(); }

Q 8 Answer)
#include<constream.h> #include<iomanip.h> void main() { clrscr(); cout<<"portcity"<<setw(18)<<setfill(3)<<"2425785"; getch(); }

Q 9 Answer)

#include<constream.h> void main() { clrscr(); long double a,b,c,d; cout<<"\nenter first fraction's nominator\t"; cin>>a; cout<<"\nenter first fraction's denominator\t"; cin>>b; cout<<"\nenter 2nd fraction's nominator\t"; cin>>c; cout<<"\nenter 2nd fraction's denominator\t"; cin>>d; cout<<a<<"/"<<b<<"+"<<c<<"/"<<d<<" = "<<(a*d+b*c)/(b*d);

getch(); }

Q 10 Answer)
#include<constream.h> void main() { clrscr(); int pound,shilling,pence; cout<<"\nenter pounds : "; cin>>pound; cout<<"\nenter shillings : "; cin>>shilling; cout<<"\nenter pence : "; cin>>pence; getch(); } cout<<"\n\n\aDecimel pounds = \x9c"<<pound<<"."<<((shilling*5)-5)+pence;

Q 11 Answer)
#include<constream.h> #include<iomanip.h> void main() { clrscr(); cout<<"last name"<<setw(3)<<setiosflags(ios::left)<<"First name"; getch(); }

Q 12 Answer)

#include<constream.h> void main() { clrscr(); float decpounds,decfrac; int pounds,x,y; cout<<"Enter decimal pounds :"; cin>>decpounds; pounds=decpounds; decfrac=decpounds-pounds; x=decfrac*20; getch(); } cout<<x;

Robert Lafore
Exercises Q 1 Answer)
#include<constream.h> void main() { clrscr(); long double x; int y; cout<<" enter number : "; cin>>x; cout<<endl; for(y=1;y<=30;y++) { cout<<x*y<<"\t"; if(y%10==0) cout<<endl; } getch(); }

Exercise # 3

Q 2 Answer)

#include<constream.h> void main() { clrscr(); long double f,c; int x; cout<<"Type\n\n 1 to convert temperature from Fahrenheit to Celsius\n\n 2 to convert temperature from Celsius to Fahrenheit\n\t"; cin>>x; if(x==1) { cout<<"\nEnter temperature in Fahernheit : "; cin>>f; cout<<"\n In Celsius it is : "<<5*(f-32)/9<<" Degrees"; } if(x==2) { cout<<"\nEnter temperature in Celsius : "; cin>>c; cout<<"\n In Fahrenheit it is : "<<(9*c/5)+32<<" Degrees"; }

getch(); }

Q 3 Answer)
#include<constream.h> int main() { clrscr(); char ch; unsigned long total=0; cout<<"Enter six digits"; while((ch=getch())!='\r') {total=total*10+ch-'0';} cout<<"\nNumber is : "<<total<<endl; return 0; }

Q 4 Answer)
#include<constream.h> int main() { clrscr(); long double x,y,s; char op,ch; do{ cout<<"Enter ist digit ,peration & 2nd digit\n\t"; cin>>x>>op>>y; switch(op) { case'*' : s=x*y; break; case'/' : s=x/y; break; case'+' : s=x+y; break; case'-' : s=x-y; break; } cout<<"\n\tAnswer is : "<<s; cout<<"\n\ndo you want to continue enter (Y for yes and N for no)"; cin>>ch; } while (ch != 'n'); return 0; }

Q 5 Answer)

#include<constream.h> void main() { int rows; int x; int space; for (rows = 0; rows <= 5; rows++) {

for (space = 5; space >= rows; space--) { cout << " "; } for (x = 0; x <= rows; x++) cout << "X"; cout << endl; } getch(); }

Q 6 Answer)
Could not solve it..

Q 7 Answer)
#include<constream.h> void main() { clrscr(); long double w,x,y,z; cout<<"\nEnter amount\t"; cin>>x; cout<<"\nEnter %age\t"; cin>>y; cout<<"\nEnter no. of years\t"; cin>>z; y=y/100; for(w=1;w<=z;w++) { x=x+(x*y); } cout<<"At the end of"<<z<<" years your amonut is : "<<x<<" Dollras"; getch(); }

Q 8 Answer)
#include<constream.h> void main() { clrscr(); int pnds,sh,pnc,pnds2,sh2,pnc2,x,y,z; char ch; do{ cout<<"enter first amount:\npounds : "; cin>>pnds; cout<<"\nshillings : "; cin>>sh; cout<<"\npence : "; cin>>pnc; cout<<"enter 2nd amount:\npounds : "; cin>>pnds2; cout<<"\nshillings : "; cin>>sh2;

cout<<"\npence : "; cin>>pnc2; x=pnds+pnds2; y=sh+sh2; z=pnc+pnc2; if(z>11) y++; z--; if(y>19) x++; cout<<"\nyour ans is : \x9c\n"<<x<<"."<<y<<"."<<z; cout<<"continue? (Y or N)\n"; cin>>ch; } while(ch!='n'); }

Q 9 Answer)
#include<constream.h> void main() { clrscr(); int w,x,y,z; cout<<"Enter the number of chairs : "; cin>>x; cout<<"\nEnter the no. of guests : "; cin>>y; for(z=y;z>(y-x);z--) { w=z*(y-1); } cout<<"\npossible combinations are : "<<w; getch(); }

Q 10 Answer)

#include<constream.h> void main() { clrscr(); int x=2; long double i,f,p,p1; cout<<"Enter final amount : "; cin>>f; cout<<"\nEnter %age of interest : "; cin>>p; cout<<"\nEnter initial amount : "; cin>>i; p1=p/100; while(i<=f) { i=i+(f*p1); x++;

} cout<<"\n it took "<<x<<" years at interest of "<<p<<" %"; getch(); }

Q 11 Answer)
#include<constream.h> void main() { clrscr(); int pnds, sh, pnc,pnds1,sh1,pnc1,x,y,z; char op; cout<<"enter first amount:\npounds : "; cin>>pnds; cout<<"\nshillings : "; cin>>sh; cout<<"\npence : "; cin>>pnc; cout<<"Enter your desired operation : "; cin>>op; cout<<"\nenter 2nd amount:\npounds : "; cin>>pnds1; cout<<"\nshillings : "; cin>>sh1; cout<<"\npence : "; cin>>pnc1; switch(op) { case'+' : x=pnds+pnds1; y=sh+sh1; z=pnc+pnc1; break; case'-' : x=pnds-pnds1; y=sh-sh1; z=pnc-pnc1; break; case'*' : x=pnds*pnds1; y=sh*sh1; z=pnc*pnc1; break; } { { if(z>11) y++; z=z-12; } if(y>19) x++; y=y-20; } cout<<"\nNow amountis\x9c"<<x<<"."<<y<<"."<<z; getch(); }

Q 12 Answer)

#include<constream.h> void main() { clrscr(); long double x,y,a,b,c,d; char op; cout<<"\nenter first fraction's nominator\t"; cin>>a; cout<<"\nenter first fraction's denominator\t";

cin>>b; cout<<"\nEnter mathematical operator : "; cin>>op; cout<<"\nenter 2nd fraction's nominator\t"; cin>>c; cout<<"\nenter 2nd fraction's denominator\t"; cin>>d; switch(op) { case'+' : x=(a*d+b*c);y=(b*d); break; case'-' : x=(a*d-b*c);y=(b*d);break; case'*' :x=a*c;y=b*d;break; case'/' :x=a*d;y=b*c;break; } cout<<"\n"<<a<<"/"<<b<<" "<<op<<" "<<c<<"/"<<d<<" = "<<x<<"/"<<y<<" = "<<x/y; getch(); }