C++ Primer Plus (Six Edition) Chapter 3, Programming Exercises

Q1. Write a short program that tasks for you height in integer inches and then converts your height to feet and inches. Have the program use the underscore charactor to 

       indicate where to type the response. Also use a const symbolic constant to represent the conversion factor.

A:  Here it is:

 // conversion.cpp -- converts inches to feet and inches
#include <iostream>
int main()
{
    using namespace std;
    const int Inc_per_fot = 12;
    int inc;


    cout << "Enter you height in inches: ";
    cin >> inc;
    cout << "So your height is " << inc << " inches, " << endl << "and it equals to ";
    int fot = inc / Inc_per_fot;
    int inches = inc % Inc_per_fot;
    cout << fot << " feet, and " << inches << " inches. " << endl;
    return 0;
}


Q2. 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 conver your height in feet and inches to your height inches (1 foot = 12 inches). Then convert your height in inc-

     hes 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 symblic constants to represent the various conversion factors.

A:

int main()
{
    using namespace std;
    const int Inch_per_foot = 12;
    const float Meter_per_inch = 0.0254;
    const float Poun_per_kilo = 2.2;
    cout << "In order to compute your BMI, please tell system your height in feet and inches." << endl;
    int foot;
    cout << "First, enter the feet:  ";
    cin >>foot;
    cout << "Then, enter the inches: ";
    int inch;
    cin >>inch;
    float (meter) = (foot * Inch_per_foot + inch) * Meter_per_inch;


    cout << "Now please enter your weight in pounds: ";
    int poun;
    cin >> poun;
    float kilo = poun / Poun_per_kilo;
    cout << "So your BMI is " << kilo / (meter * meter) << endl;
    return 0;


}


Q3. Write a program that tasks the user to enter a latiude in degrees, minutes, and seconds and that then displays the latiude 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 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.853 degrees

A:

// latitude.cpp -- latitude conversion
#include <iostream>
int main()
{
    using namespace std;
    const int Sec_per_min = 60;
    const int Minu_per_deg = 60;
    int sec, minu, deg;
    cout << "Enter a latitude in degrees, minutes, and seconds: " << endl;
    cout << "First, enter the degrees: ";
    cin >> deg;
    cout << "Next, enter the minutes of arc: ";
    cin >> minu;
    cout << "Finally, enter the seconds of arc: ";
    cin >> sec;


    double minut, degr;
    minut = double (sec) / double (Sec_per_min) + double (minu);
    degr =  double (minut) / double (Minu_per_deg) + deg;
    cout << deg << " degrees, " << minu << " minutes, " << sec << " seconds = " << degr << " degrees" << endl;
    return 0;
}


Q4. Write a program that asks the user to enter the number of seconds as an integer value (use type long, or, if available, long, 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, 17 hours, 46 minutes, 40 seconds

A:

// division.cpp -- divide seconds into days, hours, minutes and seconds
#include <iostream>
int main()
{
    using namespace std;
    const int Hour_per_day = 24;
    const int Minute_per_hour = 60;
    const int Second_per_minute = 60;
    long long second;
    cout << "Enter the number of seconds: ";
    cin >> second;


    int minute;
    minute = second / Second_per_minute;
    int hour;
    hour = minute / Minute_per_hour;
    int day;
    day = hour / Hour_per_day;


    int days, hours, minutes,seconds;
    seconds = second - minute * Second_per_minute;
    minutes = minute - hour * Minute_per_hour;
    hours = hour - day * Hour_per_day;
    days = day;


    cout << second << " seconds = " << days << " days, " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds" << endl;
    return 0;
}


Q5. Write a program that requests the user to enter the current world population and the current population of the U.S. (or lf some other nation of your choice). Store the

       information in variables of type long long. Have the program display the percent that that the U.S. (or other nation's) population is of the world's population. The out-

       put should look something like this:

       Enter the world's population: 6898758899

       Enter the population of the US: 310783781

       The population of the US is 4.50492% of the world population.

     

       You can use the Internet to get more recent figures.

A:

// percent.cpp -- computes the percent that the population of the US is the population of the world
#include <iostream>
int main()
{
    using namespace std;
    long long population1, population2;
    cout << "Enter the world's population: ";
    cin >> population1;
    cout << "Enter the population of the US: ";
    cin >> population2;


    double percent;
    percent = double(population2) / double(population1) * 100;
    cout << "The population of the US is " << percent << "% of the world population." << endl;
    return 0;


Q6. 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 got-

      ten. Or, if you prefer, the program can request distance in kilogmeters and petrol in liters and then report the result European style, in liters per 100 kilometers.

A:

// dosage.cpp -- compute gasoline dosage
#include <iostream>
int main()
{
    using namespace std;
    int miles, gallons;
    cout << "How many miles you have driven? : ";
    cin >> miles;
    cout << "And how many gallons of gasoline you have used? : ";
    cin >> gallons;
    cout << "So " << double (gallons) /double (miles) << " miles per gallon your car has gotten." << endl;


    const float kmils = 62.14;
    const float galiters = 3.875;
    float kilo, liter;
    kilo = float (miles) / float (kmils);
    liter = float (gallons) / float (galiters) * 100;
    cout << "Or, " << liter / kilo << " liters petrol per 100 kilometers." << endl;
    return 0;
}


Q7. 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 

       mile per gallon. Note that in addition to using different units of measurement, the U.S. approach (distance / fuel) is the inverse of the European appoach (fuel / dist-

      ance). 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.

A:

// consumption.cpp -- computes an automobile gasoline consumption
#include <iostream>
int main()
{
    using namespace std;
    const float galiters = 3.875;
    const float kilmiles = 62.14;
    int lpk;
    cout << "Enter the consumption in liters that your automobile runs 100 kilometers: ";
    cin >>lpk;


    double gallons, miles;
    gallons = double (lpk) / double (galiters);
    miles = double (kilmiles);
    cout << "So per gallon that your automobile can run " << double (miles) / double (gallons) << " miles.";
    cout << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值