C ++ PrimerPlus (6th Edition) Chinese version of Chapter 5 of the programming cycle and expressions relations exercises Answers

I have written reference to the answer, in the VS2019 can be compiled through, not the standard answer, nor is the best answer, for reference only

1. Preparation of a program requires the user to input two integers. The program calculates and outputs all integers between these two integers (including both integers). It is assumed to enter smaller integer. For example, if the user input is 2 and 9, the procedure indicated all integers between 2 to 9 and 44.

#include <the iostream>
the using namespace STD;
int main ()
{
int A, B;
COUT << "enter two integers:";
CIN >> A >> B;
int SUM = 0;
for (int C = A ; C <= B; C ++)
{
SUM = C +;
}
COUT << << a "~" << << B "among all integers and is the" << SUM;
}

2. Use the array objects (instead of an array) and long double (rather than long long) to rewrite Listing 5.4, and calculate 100! Value.

#include< iostream >
#include< array >
using namespace std;
const int arSize = 101;
int main()
{
array<long double, arSize> factorial;
factorial[0] = 1;
factorial[1] = 1;
for (int i = 2; i < arSize; i++)
{
factorial[i] = i * factorial[i - 1];
}
for (int j = 0; j < 16; j++)
{
cout << j << "! = " << factorial[j]<<endl;
}
cout<< arSize-1 << "! = " << factorial[arSize-1] << endl;
}

3. Write a program that requires users to enter numbers. After each entry, the program will be reported so far, and all the accumulated input. When the user inputs 0, the routine ends.

#include <the iostream>
the using namespace STD;
int main ()
{
a float A = 0;
a float SUM = 0;
COUT << "Please enter a number:";
CIN >> A;
the while (A = 0!)
{
SUM + a =;
COUT << "current sum:" << endl << sUM;
COUT << "Please enter a number:";
CIN >> a;
}
}

4.Daphne 10% simple interest invested $ 100. That is, each year the profit is 10% of the investment, or $ 10 per year:

Interest = 0.10 * original deposit

And Cleo 5% compounded invested $ 100. In other words, the current deposit interest (including interest earned) in 5% ,:

The current deposit interest = 0.05 *

Cleo investment of $ 100 in the first year is 5% profit - get $ 105. Next year's profit was $ 105 5% - or $ 5.25, and so on. Write a program to calculate how many years, Cleo investment value to exceed the investment value of Daphne, and displays the value of the investment at this time two people.

#include <the iostream>
the using namespace STD;
const a float daphneInterestRate = 0.1;
const a float cleoInterestRate = 0.05;
int main ()
{
a float daphneSum;
a float cleoSum;
int I;
for (daphneSum = 100, 100 = cleoSum, I = 0; cleoSum <= daphneSum; I ++)
{
daphneSum + = (100 * daphneInterestRate);
cleoSum + = (* cleoSum cleoInterestRate);
}
COUT << "through" I << << "years, Cleo investment value as:" << cleoSum << ", the investment value of Daphne is:" << daphneSum;
}

5. Suppose you want to sell "C ++ For Fools" a book. Write a program, enter sales (number of books, rather than sales) in each month of the year. By cycling program, using the month is initialized to char * string array (or array of string objects) to prompt monthly, and the input data is stored in an int array. Then, the program the total number of each element in the array is calculated and reported this year's sales.

#include< iostream >
using namespace std;
const int monthNumber = 12;
const char* months[monthNumber] = { “January”,“February”,“March”,“April”,“May”,“June”
,“July”,“August”,“September”,“October”,“November”,“December” };
int main()
{
int *numberPerMonth = new int[monthNumber];
int sum = 0;
for (int i = 0; i < monthNumber; i++)
{
cout<<“请输入”<<months[i]<<“的销售数额:”;
cin >> numberPerMonth[i];
sum += numberPerMonth[i];
}
cout << “全年销售总数为:” << sum;
delete numberPerMonth;
}

6. Complete the programming exercise 5, but this time using a two-dimensional array to store input - - 3 years in sales each month. It reports annual sales volume as well as three years of total sales.

#include< iostream >
using namespace std;
const int monthNumber = 12;
int main()
{
int(* p)[monthNumber] = new int[3][monthNumber];
for (int i = 0; i < 3; i++)
{
cout << “请输入第” << i + 1 << “年的销售额:”;
for (int j = 0; j < monthNumber; j++)
{
cin >> p[i][j];
}
}
int sum = 0;
int sumAll = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < monthNumber; j++)
{
sum += p[i][j];
}
cout << “第” << i + 1 << “年的销售额为:” << sum<<endl;
sumAll += sum;
sum = 0;
}
cout <<"Total sales for the three years:" << sumAll << endl;
delete[] p;
}

7. design a structure called the car, in his store the following information about the car: Manufacturer (string stored in the character array or string object), year (integer). Write a program that asks the user how many cars. Then, the process used to create a new dynamic array by a corresponding number of car structures. Next, the program prompts the user for each vehicle manufacturer (may consist of multiple words) and year information. Please note that this requires special care, because it will alternately read numeric and string (see Chapter 4). Finally, the program will display the contents of each structure. The operation of the program are as follows:

How many cars do you wish to catalog? 2

Car #1:

Please enter the make: Hudson Hornet

Please enter the year made: 1952

Car #2:

Please enter the make: Kaiser

Please enter the year made: 1951

Here is your collection:

1952 Hudson Hornet

1951 Kaiser

#include< iostream >
#include< string >
using namespace std;
int main()
{
struct Car
{
string manufacturerName;
int year;
};
int carNumber;
cout << "How many cars do you wish to catalog? ";
(cin >> carNumber).get();
Car* p = new Car[carNumber];
for (int i = 0; i < carNumber; i++)
{
cout << “Car #” << i + 1 << “:”<<endl;
cout << "Please enter the make: ";
getline(cin,p[i].manufacturerName);
cout << "Please enter the year made: ";
(cin >> p[i].year).get();
}
cout << “Here is your collection:\n”;
for (int j = 0; j < carNumber; j++)
{
cout << p[j].year << " " << p[j].manufacturerName<<endl;
}
delete []p;
}

8. write a program that uses a circulating char array and to read one word, until the user input so far done. Subsequently, the program states that the user inputs the number of words (not including done included). The following is the operation of the program:

Enter words (to stop,type the word done):

anterter birthday category dumpster

envy finagle geometry done for sure

you enter a total of 7 words.

#include< iostream >
using namespace std;
const int number = 20;
int main()
{
char words[number];
int wordsNumber = 0;
cout << “Enter words (to stop,type the word done):” << endl;
cin >> words;
while (strcmp(words,“done”))
{
wordsNumber++;
cin >> words;
}
cout << “you enter a total of " << wordsNumber << " words.”;
}

9. Write a program described in a practice before the meet, but use the string object instead of an array of characters. Include the header string in the program, using relational operators test for comparison.

#include< iostream >
#include< string >
using namespace std;
int main()
{
string words;
int wordsNumber = 0;
cout << “Enter words (to stop,type the word done):” << endl;
cin >> words;
while (words!=“done”)
{
wordsNumber++;
cin >> words;
}
cout << “you enter a total of " << wordsNumber << " words.”;
}

10. Write a nested loop program used, requires the user to enter a value indicating how many lines to be displayed. Then, the program will display the corresponding number of lines asterisk, wherein the first row comprises an asterisk, the second row comprising two asterisks, and so on. The number of lines Each line contains the number of characters equal to the user specified in the asterisk would not be enough, in front of the asterisk period. The operation of the program are as follows:

Enter number of rows: 5

. . . . *

. . . * *

. . * * *

. * * * *

* * * * *

#include< iostream >
using namespace std;
int main()
{
cout << "Enter number of rows: ";
int rowNumber;
cin >> rowNumber;
for (int i = 1; i <= rowNumber; i++)
{
for (int j = 1; j <= rowNumber-i; j++)
{
cout << ". ";
}
for (int j = 1; j <= i; j++)
{
cout << "* ";
}
cout << endl;
}
}

Released four original articles · won praise 0 · Views 56

Guess you like

Origin blog.csdn.net/HuLaTangDuoLaZi/article/details/104116470