C++ primer plus Chapter 3 after class questions

1. why does c++ have multiple shaping?

C++ has a variety of integer types. You can select the most appropriate type according to specific requirements to meet the development requirements. For example, you can use short to store spaces, long to ensure storage capacity, or you can find types that can improve the speed of specific calculations.

2. declare variables that match the following description.
a. short integer with a value of 80
b. unsigned int integer with value 42110
c. Integer with value 300000000

short num1 = 80;
unsigned int num2 = 42110;
long long num3 = 3000000000;

3. What measures does c++ provide to prevent exceeding the range of shaping?

C++ does not provide the function to automatically prevent exceeding the integer limit. You can use the header file climits to determine the limit.

4. what is the difference between 33l and 33?

The type of constant 33L is long, and the default type of constant 33 is int.

5. are the following two c++ statements equivalent?
char grade =45;
char grade = 'A';

These two statements are not really equivalent, although they are equivalent for some systems. Most importantly, only on systems using ASCII code, the first statement sets the score to the letter A, while the second statement can also be used on systems using other codes (this method is more recommended). Second, 45 is an int constant and 'a' is a char constant.

6. how to use c++ to find the characters represented by code 88? Point out at least two methods

char c = 88;

cout << c<<endl;

cout << char (88) << endl;

cout << (char)88<<endl;

7. assigning a long value to the float variable will cause rounding error. How about assigning a long value to the double variable? What about assigning the long long value to the double variable?

The answer to this question depends on the length of these two types. If long is 4 bytes, there is no loss. Because the maximum long value will be 2billion, or 10 digits. Since double provides at least 13 significant digits, no rounding is required. The long long type can provide 19 significant digits, which exceeds the 13 significant digits guaranteed by double.

8. what are the results of the following c++ expressions?
a.89+2
b.63/4
c.3/46
d.6.03/4
e.15%4

a.91

b.15

c.0

d.1.50

e.3

9. suppose x1 and x2 are two double variables, you should add them as integers, and then assign the result to an integer variable. Please write a c++ statement to complete this task. What if you add them as double values and convert them to ints?

int num = long long (x1) + long long (x2);

int num1 = int (x1 + x2);

10. what type of variables are in each of the following statements?
a.auto cars=15;
b.auto iou=150.37f;
c.auto level='B';
d.auto crat='U'/U00002155';
e.auto fract=8.25f/2.5;

a.int

b.float

c.char

d.char32_t ('U'identification)

e.double, 2.5 has no identifier, so it is a double type. 8.25f is a float type. However, the float type is promoted during the division process, so the result is a double type.

Programming problems

1. write a small program that requires users to use an integer to indicate their height (in inches), and then convert the height to feet. The program uses underlined characters to indicate the input position. In addition, a const symbol is used to represent the conversion factor.

#include <iostream>
using namespace std;

const double index = 12;

void test01();

int main()
{
	test01();
	return 0;
}

void test01()
{
	double height;
	cout << "Please enter your height (inches):______\b\b\b\b";
	cin >> height;
	cout << "What is your height" << height / index << "foot" << endl;
}

  1. Write a program that requires you to enter his height in feet and inches and his weight in pounds. (three variables are used to store this information.) The program reports their BMI (Body Mass Index). To calculate BMI, the program indicates the user's height in inches (one foot is twelve inches) and converts the height in inches to the height in meters (1 inch = 0.0254 meters). Then, convert the weight in pounds to the weight in kilograms (1kg = 2.2 pounds). Finally, calculate the corresponding BMI - weight (kg) divided by the square of height (m). Symbolic constants are used to represent various conversion factors.

#include <iostream>
#include <math.h>
using namespace std;

const double inches_per_foot = 12.0;
const double metres_per_inch = 0.0254;
const double pounds_per_kilogram = 2.2;

int main()
{
    double foot, inch, pound;
    cout << "Enter the height of foot:" << endl;
    cin >> foot;
    cout << "Enter the height of inch:" << endl;
    cin >> inch;
    cout << "Enter the height of pound:" << endl;
    cin >> pound;
    double height = (foot * inches_per_foot + inch) * metres_per_inch;
    double weight = pound / pounds_per_kilogram;
    double BMI = weight / pow(height, 2);
    cout << "the BMI is " << BMI << endl;
    return 0;
}

  1. Write a program that requires the user to enter a latitude in degrees, minutes and seconds, and then display the latitude in degrees. One degree is 60 minutes, and one minute equals 60 seconds. Please express these values in the form of symbolic constants. For each input value, a separate variable should be used to store it. The following is what happens when the program runs:
    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

#include <iostream>
#include <math.h>
using namespace std;

const double degree_per_minutes = 60;
const double minutes_per_seconds = 60;

void test01();

int main()
{
	test01();
	return 0;
}

void test01()
{
	int degrees;
	cout << "First,enter the degrees :_____\b\b\b\b" ;
	cin >> degrees;

	int minutes;
	cout << "Next,enter the minutes of arc :______\b\b\b\b" ;
	cin >> minutes;

	int seconds;
	cout << "Finally,enter the seconds of arc :______\b\b\b\b" ;
	cin >> seconds;

	double  latitude;
	latitude = degrees + minutes / degree_per_minutes + seconds / minutes_per_seconds/ degree_per_minutes;
	cout << degrees << "degrees, " << minutes << "minutes, " << seconds << "seconds = " << latitude <<" degrees" << endl;

	
}

  1. Write a program that requires the user to input the number of seconds in integer mode (stored with long or long long variable), and then display the time in days, hours, minutes and seconds. Use symbolic constants to indicate the number of hours per day, minutes per hour, and seconds per minute. The output of this program should be similar to the following:
    Enter the number of seconds:31600000
    31600000 seconds = 365 days,17 hours,46 minutes,40 seconds

#include <iostream>
#include <math.h>
using namespace std;

const int min_per_sec = 60;
const int hours_per_sec = 60 *  60;
const long day_per_sec = 24 * 60 * 60;
const long long year_per_sec = 365 * 24 * 60 * 60;

void test01();

int main()
{
	test01();
	return 0;
}

void test01()
{
	long long seconds;
	cout << "Enter the number of seconds :__________\b\b\b\b\b\b\b\b" ;
	cin >> seconds;

	int years = seconds / year_per_sec;

	int days = (seconds % year_per_sec )/ day_per_sec;

	int hours = (seconds % day_per_sec) / hours_per_sec;
	
	int minutes = (seconds % hours_per_sec) / min_per_sec;

	cout << seconds << "seconds =  " << years << "years, " << days<< "days ," << hours <<" hours," << minutes << " minutes," << seconds % min_per_sec << " seconds" << endl;


}

  1. Write a program that requires users to enter the current global population and the current population of the United States (or the population of other countries). Store this information in the long long variable and let the program display the percentage of the global population of the United States (or other countries). The output of this program is similar to the following:
    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.

#include <iostream>
using namespace std;

void test01();

int main()
{
	test01();
	return 0;
}

void test01()
{
	long long worldPopulation;
	cout << "Enter the world's population :__________\b\b\b\b\b\b\b\b" ;
	cin >> worldPopulation;

	long long USPopulation;
	cout << "Enter the population of the US :__________\b\b\b\b\b\b\b\b";
	cin >> USPopulation;

	double pencentage = double(USPopulation) / worldPopulation *100;
	cout << "The population of the US is " << pencentage << "% of the world population." << endl;
	
}

6. write a program that requires the user to input the driving mileage (miles) and the amount of gasoline used (gallons), and then point out that the vehicle's fuel consumption is one gallon. If you like, you can also let the program ask the user to enter the distance in kilometers and the amount of gasoline in liters, and then point out the European style results

#include <iostream>
using namespace std;

void test01(int choice);

int main()
{
	cout << "Select unit 1: km2: miles" << endl;
	int choice;
	cin >> choice;
	test01(choice);
	return 0;
}

void test01(int choice)
{
	if (choice != 1) {
		long  distance;
		cout << "Enter mileage(mile):__________\b\b\b\b\b\b\b\b";
		cin >> distance;

		int Gasoline;
		cout << "Input gasoline volume (gallons) :__________\b\b\b\b\b\b\b\b";
		cin >> Gasoline;

		double Miles_per_gallon = static_cast<double> (Gasoline) / distance;
		cout << "Per mile" << Miles_per_gallon << "gallon." << endl;
	}
	else
	{
		long  distance;
		cout << "Enter mileage(kilometre):__________\b\b\b\b\b\b\b\b";
		cin >> distance;

		int Gasoline;
		cout << "Input gasoline volume (L) :__________\b\b\b\b\b\b\b\b";
		cin >> Gasoline;

		double kilom_per_gallon = static_cast<double> (Gasoline) / distance;
		cout << "Per kilometer" << kilom_per_gallon << "rise." << endl;
	}
}

  1. Write a program that requires each user to input the car's fuel consumption (in litres per 100 km) in European style, and then convert it to American style fuel consumption - miles per gallon. Note that the US method (distance / fuel) is the opposite of the European method (fuel / distance) except that different units of measurement are used. 100 kilometers equals 62.14 miles, and a gallon equals 3.875 liters. Therefore, 19mpg is about 12.41/100km, and 127mpg is about 8.71/100km.

#include <iostream>
using namespace std;

int main()
{
    double fuel_per_km;
    cout << "Fuel consumption of car(Fuel consumption per 100 km(L)):" << endl;
    cin >> fuel_per_km;
    double fuel_per_gallon = 62.14 / (fuel_per_km / 3.875);
    cout << "The mileage per gallon:" << fuel_per_gallon << endl;
    system("PAUSE");
    return 0;
}

Tags: C++

Posted by A584537 on Thu, 02 Jun 2022 02:02:44 +0530