Chapter Review
- Have more than one an integer type lets you choose the type that's best suited to a particular need. For example, you could use to
short
conserve space or to long
guarantee storage capacity or to find, a particular Typ E speed up a particular calculation.
short ribs = 80; // or short int ribs = 80;
unsigned int q = 42110; // or unsigned q = 42110;
unsigned long ants = 3000000000; // or long long ants = 3000000000;
Note:don ' t count on int
being large enough to hold 3,000,000,000. Also If your system supports universal list-initialization, you could use it:
short rbis = {80};
unsigned int q {42110};
long long ants {3000000000};
- C + + provides no automatic safeguards to keep the from exceeding integer limits; You can use the
climits
header file to determine what the limits is.
- The constant 33L is type
long
and whereas the constant is type int
.
- The statements is not really equivalent, although they has the same effect on some system. Most importantly, the first statement assigns the letter A to only on
grade
a system using the ASCII code, while the Secon D statement also works for other codes. Second, a type int
constant, whereas is A
a type char
constant.
- Here is four ways:
char c = 88;
cout << c << endl; // char type prints as character
cout.put(char(88)); // put() prints char as character
cout << char(88) << endl; // new-style type cast value to char
cout << (char) 88 << endl; // old-style type cast value to char
- The answer depends on how large the types is. If
long
is 4 bytes, there is no loss. Thats because the largest long
value would be about 2 billion, which is digits. Because double
provides at least significant figures, no rounding would is needed. The long long
type, on the other hand, can reach of digits, which exceeds the significant figures guaranteed for double
.
a. 8 * 9 + 2 is 72 + 2 is 74
b. 6 * 3 / 4 is 18 / 4 is 4
c. 3 / 4 * 6 is 0 * 6 is 0
d. 6.0 * 3 / 4 is 18.0 / 4 is 4.5
e. 15 % 4 is 3
- Either of the following would work for the first task:
int pos = (int) x1 + (int) x2;
int pos = int (x1) + int (x2);
To add them as type double
and then convert, you could do either of the following:
int pos = (int) (x1 + x2);
int pos = int (x1 + x2);
- A. Intb. FLOATC. Chard. Char32_te. Double
Programming Exercises1
#include <iostream>constint Factor{12};int main(){ usingnamespace std; "Input your height(inches): "; int height, feet, inches; cin >> height; feet = height / Factor; inches = height % Factor; "Your height is equal to "" feet,"" inches" << endl; return0;}
2
#include <iostream>intMain () {using namespaceStdConst DoubleInches_per_foot = A;Const Doubleinches_per_meter{0.0254};Const Doublekilo_per_pound{2.2}; cout <<"Enter your height in feet and inches (example:14):";DoubleFeetDoubleInches cin >> feet; cin >> inches; cout <<"Enter your weight in pounds:";DoubleWeight CIN >> Weight;Doublei = (feet * inches_per_foot + Inches) * inches_per_meter; cout <<"Your BMI is:"<< weight * Kilo_per_pound/(i * i) << Endl;return 0;}
3
#include <iostream>intMain () {using namespaceStdConst Doublemid{60.0}; cout <<"Enter a latitude in degrees, minutes, and seconds:"<< Endl; cout <<"First, enter the degrees:";intDegrees CIN >> degrees; cout <<"Next, enter the minutes of Arc:";intMinutes Cin >> minutes; cout <<"Finally, enter the seconds of Arc:";intSeconds cin >> seconds; cout << degrees <<"degrees,"<< minutes <<"minutes,"<< seconds <<"seconds ="<<Double(degrees) + Minutes/mid + seconds/(Mid * mid) << Endl;return 0;}
4
#include <iostream>intMain () {using namespaceStdConst inth_per_day{ -};Const intM_per_hour = { -};Const intS_per_seconds = -; cout <<"Enter The number of seconds:";Long LongSeconds cin >> seconds;Long LongD = seconds/(H_per_day * m_per_hour * s_per_seconds);Long Longh = seconds% (H_per_day * m_per_hour * s_per_seconds)/(M_per_hour * s_per_seconds);Long Longm = seconds% (H_per_day * m_per_hour * s_per_seconds)% (M_per_hour * s_per_seconds)/s_per_seconds;Long Longs = seconds% (H_per_day * m_per_hour * s_per_seconds)% (m_per_hour * s_per_seconds)% S_per_seconds; cout << seconds <<"seconds ="<< D <<"Days,"<< h <<"Hours,"<< m <<"minutes,"<< s <<"Seconds\ n";return 0;}
5
#include <iostream>int main(){ usingnamespace std; "Enter the world's population: "; longlong p_of_world; cin >> p_of_world; "Enter the population of the China: "; longlong p_of_nation; cin >> p_of_nation; "The population of the China is "doubledouble100.0 "% of the world population. \n"; return0;}
6
#include <iostream>int main(){ usingnamespace std; "Enter you have driven in miles: "; int mile; cin >> mile; "Enter you have costed of gasoline: "; int gas; cin >> gas; doubledouble" miles per gallon your car has gotten. \n"; return0;}
7
#include <iostream> int Main () {using namespace std; const double Miles_per_kilo{6.214e-1 }; const double Liters_per_gallon = 3.875 ; cout << "Enter European style (liters per kilometers):" ; double EU; CIN >> EU; int temp = int ((1.0 /EU) * (Miles_per_kilo * Span class= "FL" >100.0 ) * Liters_per_gallon); cout << EU << l/100 km = " << temp << " mpg \n ; return 0 ;}
C + + Primer plus the sixth edition of the English version Chapter 3