# Object Oriented Programming By Robert Lafore - Chapter 2 Complete Solution

Chapter 3 Complete Solution.....

Program 1:

Assuming there are 7.481 gallons in a cubic foot, write a program that asks the user to enter a number of gallons, and then displays the equivalent in cubic feet.

Code:

#include <iostream>

// Code By HI

using namespace std;

int main ()

{

float cubic_foot, gallons;

cout<<"Enter the number of Gallons: ";

cin>>gallons;

cubic_foot= gallons/7.481;

cout<<gallons<<" Gallons = "<<cubic_foot<<" Cubic Foot"<<endl;

return 0;

}

Chapter 3 Complete Solution.....

Program 2:

Write a program that generates the following table:

1990         135

1991        7290

1992        11300

1993        16200

Use a single cout statement for all output

Code:

#include <iostream>
// Code By HI
#include <iomanip>
using namespace std;
int main ()
{
cout<<"1990"<<setw(12)<<"135"<<endl<<"1991"<<setw(12)<<"7290"                              <<endl<<"1992"<<setw(12)<<"11300"<<endl<<"1993"<<setw(12)<<"16200"<<endl;
return 0;
}

Program 3:
Write a program that generates the following output: 10 20 19 Use an integer constant for the 10, an arithmetic assignment operator to generate the 20, and a decrement operator to generate the 19.

Code:

#include <iostream>
// Code By HI
using namespace std;
int main ()
{
int i=10;
cout<<i<<endl;
i=i+i;
cout<<i<<endl;
cout<<--i<<endl;
return 0;
}

Program 4:
Write a program that displays your favorite poem. Use an appropriate escape sequence for the line breaks. If you don’t have a favorite poem, you can borrow this one by Ogden Nash: Candy is dandy, But liquor is quicker.

Code:

#include <iostream>
// Code By HI
using namespace std;
int main ()
{
cout<<"Candy id Dandy, \nBut liquor is quicker"<<endl;
return 0;
}

Program 5:
A library function, islower(), takes a single character (a letter) as an argument and returns a nonzero integer if the letter is lowercase, or zero if it is uppercase. This function requires the header file CTYPE.H. Write a program that allows the user to enter a letter, and then displays either zero or nonzero, depending on whether a lowercase or uppercase letter was entered. (See the SQRT program for clues.)

Code:

#include <iostream>
// Code By HI
using namespace std;
int main ()
{
char a;
cout<<"Enter a character: ";
cin>>a;
cout<<islower(a)<<endl;
return 0;
}

Program 6:
On a certain day, the British pound was equivalent to \$1.487 U.S., the French franc was \$0.172, the German Deutschmark was \$0.584, and the Japanese yen was \$0.00955. Write a program that allows the user to enter an amount in dollars, and then displays this value converted to these four other monetary units.

Code:

#include <iostream>
// Code By HI
using namespace std;
int main ()
{
double pound=1.487, franc=0.172, german= 0.584, yen=0.00955;
double dollars;
cout<<"Enter your amount in Dollars: ";
cin>>dollars;
pound=dollars/pound;
franc=dollars/franc;
german=dollars/german;
yen=dollars/yen;
cout<<dollars<<" Dollars = "<<pound<<" Pounds"<<endl;
cout<<dollars<<" Dollars = "<<franc<<" Franc"<<endl;
cout<<dollars<<" Dollars = "<<german<<" German"<<endl;
cout<<dollars<<" Dollars = "<<yen<<" Yen"<<endl;
return 0;
}

Program 7:

You can convert temperature from degrees Celsius to degrees Fahrenheit by multiplying by 9/5 and adding 32. Write a program that allows the user to enter a floating-point number representing degrees Celsius, and then displays the corresponding degrees Fahrenheit.

Code:

#include <iostream>
using namespace std;
// Code By HI
int main ()
{
float cel, fah;
cout<<"Enter temperature in degrees Celcius: ";
cin>>cel;
fah=(cel*9/5)+32;
cout<<cel<<" Celcius = "<<fah<<" Fahrenheit "<<endl;
return 0;
}

Program 8:

When a value is smaller than a field specified with setw(), the unused locations are, by default, filled in with spaces. The manipulator setfill() takes a single character as an argument and causes this character to be substituted for spaces in the empty parts of a field. Rewrite the WIDTH program so that the characters on each line between the location name and the population number are filled in with periods instead of spaces, as in Port_city.....2425785

Code:

#include <iostream>
#include <iomanip>
// Code By HI
using namespace std;
int main ()
{
cout<<"City Name "<<setfill('_')<<setw(30)<<" Population Number"<<endl
<<"Bahawalpur "<<setfill('_')<<setw(15)<<" 5000"<<endl
<<"Lahore "<<setfill('_')<<setw(20)<< " 20000"<<endl
return 0;

}

Program 9:

Write a program that encourages the user to enter two fractions, and then displays their sum in fractional form. (You don’t need to reduce it to the lowest terms.) The interaction with the user might look like this:

Enter the first fraction: 1/2
Enter the second fraction: 2/5
Sum = 9/10

You can take advantage of the fact that the extraction operator (>>) can be chained to read in more than one quantity at once: cin >> a >> dummychar >> b;

Code:

#include <iostream>
// Code By HI
using namespace std;
int main ()
{
int a,b,c,d;
char fraction_1, fraction_2;
cout<<"Enter first fraction: ";
cin>>a>>fraction_1>>b;
cout<<"Enter second fraction: ";
cin>>c>>fraction_2>>d;
int numerator=(a*d)+(b*c);
int denominator=  b*d;
cout<<"Sum is: "<<numerator<<" / "<<denominator<<endl;
return 0;
}

Program 10:

In the heyday of the British Empire, Great Britain used a monetary system based on pounds, shillings, and pence. There were 20 shillings to a pound, and 12 pence to a shilling. The notation for this old system used the pound sign, £, and two decimal points, so that, for example, £5.2.8 meant 5 pounds, 2 shillings, and 8 pence. (Pence is the plural of a penny.) The new monetary system, introduced in the 1950s, consists of only pounds and pence, with 100 pence to a pound (like U.S. dollars and cents). We’ll call this new system decimal pounds. Thus £5.2.8 in the old notation is £5.13 in decimal pounds (actually £5.1333333). Write a program to convert the old pounds-shillings-pence format to decimal pounds. An example of the user’s interaction with the program would be

Enter pounds: 7
Enter shillings: 17
Enter pence: 9
Decimal pounds = £7.89

In most compilers, you can use the decimal number 156 (hex character constant ‘\x9c’) to represent the pound sign (£). In some compilers, you can put the pound sign into your program directly by pasting it from the Windows Character Map accessory.

Code:

#include <iostream>
// Code By HI
using namespace std;
int main ()
{
double pound, shilling, pence;
cout<<"Enter Pounds: ";
cin>>pound;
cout<<"Enter Shillings: ";
cin>>shilling;
cout<<"Enter Pence: ";
cin>>pence;
double totalpounds= (pence/(12*20))+(shilling/20);
double decimalpounds= pound+totalpounds;
cout<<"Decimal Pounds = "<<decimalpounds<<( '\x9c')<<endl;
return 0;
}

Program 11:

By default, the output is right-justified in its field. You can left-justify text output using the manipulator setiosflags(ios::left). (For now, don’t worry about what this new notation means.) Use this manipulator, along with setw(), to help generate the following output:

Last name           First name           Street address          Town            State
-------------------------------------------------------------------------------------------------------------------------
Jones                   Bernard                109 Pine Lane           Littletown     MI
O’Brian                 Coleen                  42 E. 99th Ave.          Bigcity          NY
Wong                    Harry                    121-A Alabama St.    Lakeville      IL

Code:

#include <iostream>
#include <iomanip>
// Code By HI
using namespace std;
int main ()
{
cout<<setiosflags(ios::left)<<setw(12)<<"Name"<<setw(12)<<"City"<<setw(12)                  <<"State"<<setw(12)<<"Country"<<endl;
cout<<setfill('_')<<setw(45)<<"_"<<endl
<<endl;
cout<<setfill(' ')<<setiosflags(ios::left)<<setw(12)<<"Ali"<<setw(12)<<"Lahore"                    <<setw(12)<<"Punjab"<<setw(12)<<"Pakistan"<<endl;
cout<<setiosflags(ios::left)<<setw(12)<<"Usman"<<setw(12)<<"Karachi"
<<setw(12)<<"Sindh"<<setw(12)<<"Pakistan"<<endl;
return 0;
}

Program 12:

Write the inverse of Exercise 10, so that the user enters an amount in Great Britain’s new decimal-pounds notation (pounds and pence), and the program converts it to the old pounds-shillings-pence notation. An example of interaction with the program might be

Enter decimal pounds: 3.51
Equivalent in old notation = £3.10.2.

Make use of the fact that if you assign a floating-point value (say 12.34) to an integer variable, the decimal fraction (0.34) is lost; the integer value is simply 12. Use a cast to avoid a compiler warning. You can use statements like

float decpounds; // input from user (new-style pounds)
int pounds; // old-style (integer) pounds
float decfrac; // decimal fraction (smaller than 1.0)

pounds = static_cast(decpounds); // remove decimal fraction
decfrac = decpounds - pounds; // regain decimal fraction

You can then multiply decfrac by 20 to find shillings. A similar operation obtains pence.

Code:

#include <iostream>
using namespace std;
// Code By HI
int main ()
{
double decpounds;
cout<<"Enter decimal pounds: ";
cin>>decpounds;
int pounds= static_cast<int>(decpounds);
double frac_pounds= decpounds-pounds;
double dec_shillings= frac_pounds*20;
int shillings= static_cast<int>(dec_shillings);
double frac_shillings= dec_shillings-shillings;
int pence=static_cast<int>(frac_shillings*12);
cout<<decpounds<< ('\x9c')<<" = "<<('\x9c')<<pounds<<"."<<shillings<<"."
<<pence<<endl;
return 0;
}