You are on page 1of 11

1.

Write a short program that asks for your height in integer inches and
then converts your
height to feet and inches. Have the program use the underscore
character to indicate
where to type the response. Also, use a const symbolic constant to
represent the conversion
factor.
My Answer:
1
2
3 #include <iostream>
4 int main()
5{
6 const int convFeetoInches = 12;
7 using namespace std;
8 cout.setf(ios_base::fixed, ios_base::floatfield); // fixed-point
9
1 int inches = 0;
0
cout << "Enter your height in inches here: ";
1
cin >> inches; cout << endl;
1
cout << "Your heght is " << int (inches) / int (convFeetoInches) << " feet and "
1
<< inches % convFeetoInches << " inches.";
2
1
3 cin.get();
1 cin.get();
4
return 0;
1
}
5
1
6

Responses to C++ Primer Plus Chapter 3 Exercise 1 Answer


1.

Jeff Mattson says:


June 4, 2012 at 7:52 pm

Where is the underscore?


Reply

2.

JHub says:
November 19, 2012 at 12:11 pm
Use this code to add the underscore to the cout statement.
Original cout << "Enter your height in inches here: ";
Underscore cout << "Enter your height in inches here:____\b\b\b\b ";
Reply

3.

buzz says:

August 17, 2013 at 6:19 am


#include
int main()
{
using namespace std;
const int feet_to_inches = 12;
int height;
cout <> height;
int inches = height / feet_to_inches;
int feets = height % feet_to_inches;
cout << "Your height is: " << inches << " inches and " << feets
<< " feets" <<endl;
return 0;
}
Reply

4.

buzz says:
August 17, 2013 at 6:23 am
#include //OMG Please delete my first comment
int main()
{
using namespace std;

const int feet_to_inches = 12;


int height;
cout <> height;
int inches = height / feet_to_inches;
int feets = height % feet_to_inches;
cout << "Your height is: " << inches << " inches and " << feets
<< " feets" <<endl;
return 0;
}
2. Write a short program that asks for your height in feet and inches and your weight
in
pounds. (Use three variables to store the information.) Have the program report your
body mass index (BMI). To calculate the BMI, first convert your height in feet and
inches
to your height in inches (1 foot = 12 inches). Then, convert your height in inches to
your height in meters by multiplying by 0.0254. Then, convert your weight in pounds
into your mass in kilograms by dividing by 2.2. Finally, compute your BMI by
dividing
your mass in kilograms by the square of your height in meters. Use symbolic
constants
to represent the various conversion factors.
My Answer:
1

#include <iostream>

int main()

4
5

const int convFeetoInches = 12;

const double convInchesToMeter = 0.0254;

const double convLBtoKG = 2.2;

8
9

using namespace std;

10

cout.setf(ios_base::fixed, ios_base::floatfield); // fixed-point

11
12

int feet = 0;

13

cout << "Enter your height [feet]: "; cin >> feet; cout << endl;

14

int inches = 0;

15

cout << "Enter your height [inches]: "; cin >> inches; cout << endl;

16

int weight = 0;

17

cout << "Enter your weight [pounds]: "; cin >> weight; cout << endl;

18
19

double heightM = (feet*convFeetoInches + inches)*convInchesToMeter;

20

double massKG = weight / convLBtoKG;

21

double BMI = massKG / (heightM*heightM);

22
23

cout << "Your BMI is " << BMI;

24
25

cin.get();

26

cin.get();

27

return 0;

28

3. Write a program that asks the user to enter a latitude in degrees, minutes, and
seconds
and that then displays the latitude in decimal format. There are 60 seconds of arc
to a
minute and 60 minutes of arc to a degree; represent these values with symbolic
constants.
You should use a separate variable for each input value. A sample run should
look like this:
Enter a latitude in degrees, minutes, and seconds:
First, enter the degrees: 37
Next, enter the minutes of arc: 51
Finally, enter the seconds of arc: 19
37 degrees, 51 minutes, 19 seconds = 37.8553 degrees
My Answer:

#include <iostream>

int main()

4
5

using namespace std;

cout.setf(ios_base::fixed, ios_base::floatfield); // fixed-point

7
8

cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;

int degrees;

1
0

cout << "First enter the degrees: "; cin >> degrees; cout << endl;
int minutes;

11
cout << "Next enter the minutes of arc: "; cin >> minutes; cout << endl;
1
2

int seconds;

1
3

cout << "Finally, enter the seconds of arc: "; cin >> seconds; cout <<
endl;

1
4

cout << degrees << " degrees, " << minutes << " minutes, " << seconds
<< " seconds = ";

1
5

float degrees2 = float (degrees) + ( float (minutes) + float


(seconds)/60)/60;

1
6

cout << degrees2 << " degrees." << endl;

1
7
1
8

cin.get();
cin.get();

1
9
2
0

return 0;
}

2
1
2
2
2
3
4. Write a program that asks the user to enter the number of seconds as an
integer value (use type long) and that then displays the equivalent time in days,
hours, minutes, and seconds. Use symbolic constants to represent the number of
hours in the day, the number of minutes in an hour, and the number of seconds
in a minute. The output should look like this:
Enter the number of seconds: 31600000
31600000 seconds = 365 days, 46 minutes, 40 seconds
My Answer:
1

#include <iostream>

int main()

4
5

const int HrsPerDay = 24;

const int MinsInHour = 60;

const int SecsInMin = 60;

8
9

using namespace std;

1
0

cout.setf(ios_base::fixed, ios_base::floatfield); // fixed-point

11
cout << "Enter the number of seconds: ";
1
2
1

long int seconds;

cin >> seconds;

1
4

int SecsInHour = SecsInMin*MinsInHour;


int SecondsPerDay = SecsInHour*HrsPerDay;

1
5

int Days = seconds / SecondsPerDay;

1
6

int SecondsPerHour = SecsInMin*MinsInHour;


int Hours = (seconds % SecondsPerDay)/SecondsPerHour;

1
7
1
8
1
9

int Minutes = (seconds - Days*SecondsPerDay Hours*SecondsPerHour)/SecsInMin;


int SecondsLast = seconds - Days*SecondsPerDay Hours*SecondsPerHour - Minutes*SecsInMin;
cout << seconds << " seconds = " << Days << " days, " << Hours << "
hours, ";

2
0

cout << Minutes << " minutes, " << SecondsLast << " seconds.";

2
1
2
2

cin.get();

2
3
2
4
2
5
2
6
2
7
2
8
2

cin.get();
return 0;
}

9
5. Write a program that asks how many miles you have driven and how many
gallons of
gasoline you have used and then reports the miles per gallon your car has gotten.
Or, if
you prefer, the program can request distance in kilometers and petrol in liters
and then
report the result European style, in liters per 100 kilometers.
My Answer:
1

#include <iostream>

int main()

4
5

using namespace std;

cout.setf(ios_base::fixed, ios_base::floatfield); // fixed-point

7
8

int miles;

int gallons;

10
11

cout << "Enter the number of miles travelled: ";

12

cin >> miles;

13

cout << "Enter the number of gallons used: ";

14

cin >> gallons;

15
16

float MPG;

17

MPG = float (miles)/float (gallons);

18
19

cout << "The MPG is " << MPG << endl;

20
21

cin.get();

22

cin.get();

23

return 0;

24

6. Write a program that asks you to enter an automobile gasoline consumption


figure in the
European style (liters per 100 kilometers) and converts to the U.S. style of miles
per gallon.
Note that in addition to using different units of measurement, the U.S approach
(distance / fuel) is the inverse of the European approach (fuel / distance). Note
that 100
kilometers is 62.14 miles, and 1 gallon is 3.875 liters. Thus, 19 mpg is about 12.4
l/100
km, and 27 mpg is about 8.7 l/100 km.
My Answer:
1

#include <iostream>

int main()

4
5

const double USkm100 = 62.14;

const double GallLT = 3.785;

using namespace std;

//cout.setf(ios_base::fixed, ios_base::floatfield); // fixed-point

9
10

cout << "Enter EU Ltr/100km: ";

11

double LtrKM;

12

cin >> LtrKM;

13

double USGAL = LtrKM/GallLT;

14

double USMPG = USkm100/USGAL;

15

cout << "The US equivalent MPG - " << USMPG << endl;

16
17
18

cin.get();

19

cin.get();

20

return 0;

21

Matthew says:
December 27, 2011 at 7:01 am
Hey, In this code you wrote wrong const. GallLT should be 3.875, not 3.785. It should be
something like this:
#include
#include
using namespace std;
const float PETROL = 62.14f;
int main()
{
// variables //
float petrol(0.f);
///////////////
cout << "LT / 100km:" << endl;
cout <> petrol;
cout << endl;
//float finalResult = petrol / petrol;
float secondResult = PETROL / petrol;

//finalResult=3.875f;
secondResult *= 3.875f;
cout << "Final result: " << secondResult << " MPG";
_getch();
}

You might also like