1. Write a program that requires users to input two integers. The program will calculate and output the sum of all integers between the two integers (including the two integers). It is assumed that a smaller integer is entered first. For example, if the user enters 2 and 9, the program will indicate that the sum of all integers between 2 and 9 is 44.

```#include<iostream>

using namespace std;

int main()

{

int min, max, sum = 0;

cout << "Enter the min interger:";

cin >> min;

cout << "Enter the max interger:";

cin >> max;

for (int i = min; i <= max; i++)//for(int i =min;i < max + 1;i++)

sum += i;

cout << "Sum = " << sum << endl;

system("pause");

return 0;

}

//Enter the min interger : 2

//Enter the max interger : 9

//Sum = 44

//Please press any key to continue```

2. Rewrite listing 5.4 with array object (instead of array) and long double (instead of long long) and calculate 100! Value of.

```#include<iostream>

#include<array>

const int ArSize = 101;

using namespace std;

int main()

{

array<long double, ArSize> factorials;

factorials[1] = factorials[0] = 1;

for (int i = 2; i < ArSize; i++)

factorials[i] = i * factorials[i - 1];

for (int i = 0; i < ArSize; i++)

cout << i << " != " << factorials[i] << endl;

system("pause");

return 0;

}

//0 != 1

//1 != 1

//2 != 2

//3 != 6

//4 != 24

//5 != 120

//6 != 720

//7 != 5040

//8 != 40320

//9 != 362880

//10 != 3.6288e+06

//11 != 3.99168e+07

//12 != 4.79002e+08

//13 != 6.22702e+09

//14 != 8.71783e+10

//15 != 1.30767e+12

//16 != 2.09228e+13

//17 != 3.55687e+14

//18 != 6.40237e+15

//19 != 1.21645e+17

//20 != 2.4329e+18

//21 != 5.10909e+19

//22 != 1.124e+21

//23 != 2.5852e+22

//24 != 6.20448e+23

//25 != 1.55112e+25

//26 != 4.03291e+26

//27 != 1.08889e+28

//28 != 3.04888e+29

//29 != 8.84176e+30

//30 != 2.65253e+32

//31 != 8.22284e+33

//32 != 2.63131e+35

//33 != 8.68332e+36

//34 != 2.95233e+38

//35 != 1.03331e+40

//36 != 3.71993e+41

//37 != 1.37638e+43

//38 != 5.23023e+44

//39 != 2.03979e+46

//40 != 8.15915e+47

//41 != 3.34525e+49

//42 != 1.40501e+51

//43 != 6.04153e+52

//44 != 2.65827e+54

//45 != 1.19622e+56

//46 != 5.50262e+57

//47 != 2.58623e+59

//48 != 1.24139e+61

//49 != 6.08282e+62

//50 != 3.04141e+64

//51 != 1.55112e+66

//52 != 8.06582e+67

//53 != 4.27488e+69

//54 != 2.30844e+71

//55 != 1.26964e+73

//56 != 7.10999e+74

//57 != 4.05269e+76

//58 != 2.35056e+78

//59 != 1.38683e+80

//60 != 8.32099e+81

//61 != 5.0758e+83

//62 != 3.147e+85

//63 != 1.98261e+87

//64 != 1.26887e+89

//65 != 8.24765e+90

//66 != 5.44345e+92

//67 != 3.64711e+94

//68 != 2.48004e+96

//69 != 1.71122e+98

//70 != 1.19786e+100

//71 != 8.50479e+101

//72 != 6.12345e+103

//73 != 4.47012e+105

//74 != 3.30789e+107

//75 != 2.48091e+109

//76 != 1.88549e+111

//77 != 1.45183e+113

//78 != 1.13243e+115

//79 != 8.94618e+116

//80 != 7.15695e+118

//81 != 5.79713e+120

//82 != 4.75364e+122

//83 != 3.94552e+124

//84 != 3.31424e+126

//85 != 2.8171e+128

//86 != 2.42271e+130

//87 != 2.10776e+132

//88 != 1.85483e+134

//89 != 1.6508e+136

//90 != 1.48572e+138

//91 != 1.352e+140

//92 != 1.24384e+142

//93 != 1.15677e+144

//94 != 1.08737e+146

//95 != 1.033e+148

//96 != 9.91678e+149

//97 != 9.61928e+151

//98 != 9.42689e+153

//99 != 9.33262e+155

//100 != 9.33262e+157

//Please press any key to continue```

3. Write a program that requires users to enter numbers. After each input, the program will report the cumulative sum of all inputs so far. When the user enters 0, the program ends.

```#include<iostream>

using namespace std;

int main()

{

double n,sum = 0;

do

{

cout << "Please enter the digital:";

cin >> n;

sum += n;

} while (n != 0);

cout << "Sum = " << sum << endl;

system("pause");

return 0;

}

//Please enter the digital : 10

//Please enter the digital : 20

//Please enter the digital : 3.0

//Please enter the digital : 0

//Sum = 33

//Please press any key to continue
```

Cleo's profit from investing \$100 in the first year was 5% - it got \$105. The next year's profit is 5% of \$105 - \$5.25, and so on. Please write a program to calculate the number of years before Cleo's investment value can exceed Daphne's investment value, and display the investment value of two people at this time.

```#include<iostream>

using namespace std;

const int DEPOSIT_BASE = 100;

int main()

{

float daphne_deposit = DEPOSIT_BASE;

float cleo_deposit = DEPOSIT_BASE;

int year = 0;

while (daphne_deposit >= cleo_deposit)

{

cout << "In " << year++ << " Year: Daphbe = " << daphne_deposit << endl;

cout << "\tCleo = " << cleo_deposit << endl;

daphne_deposit += 0.1 * DEPOSIT_BASE;//Daphne's investment is a profit of \$10 a year

cleo_deposit += 0.05 * cleo_deposit;//Cleo's investment is each year based on the previous year plus 0.05 * current deposits

}

cout << "In " << year << " Year: Daphne = " << daphne_deposit << endl;

cout << "\tCleo = " << cleo_deposit << endl;

system("pause");

return 0;

}

//In 0 Year: Daphbe = 100

//Cleo = 100

//In 1 Year : Daphbe = 110

//Cleo = 105

//In 2 Year : Daphbe = 120

//Cleo = 110.25

//In 3 Year : Daphbe = 130

//Cleo = 115.762

//In 4 Year : Daphbe = 140

//Cleo = 121.551

//In 5 Year : Daphbe = 150

//Cleo = 127.628

//In 6 Year : Daphbe = 160

//Cleo = 134.01

//In 7 Year : Daphbe = 170

//Cleo = 140.71

//In 8 Year : Daphbe = 180

//Cleo = 147.746

//In 9 Year : Daphbe = 190

//Cleo = 155.133

//In 10 Year : Daphbe = 200

//Cleo = 162.889

//In 11 Year : Daphbe = 210

//Cleo = 171.034

//In 12 Year : Daphbe = 220

//Cleo = 179.586

//In 13 Year : Daphbe = 230

//Cleo = 188.565

//In 14 Year : Daphbe = 240

//Cleo = 197.993

//In 15 Year : Daphbe = 250

//Cleo = 207.893

//In 16 Year : Daphbe = 260

//Cleo = 218.287

//In 17 Year : Daphbe = 270

//Cleo = 229.202

//In 18 Year : Daphbe = 280

//Cleo = 240.662

//In 19 Year : Daphbe = 290

//Cleo = 252.695

//In 20 Year : Daphbe = 300

//Cleo = 265.33

//In 21 Year : Daphbe = 310

//Cleo = 278.596

//In 22 Year : Daphbe = 320

//Cleo = 292.526

//In 23 Year : Daphbe = 330

//Cleo = 307.152

//In 24 Year : Daphbe = 340

//Cleo = 322.51

//In 25 Year : Daphbe = 350

//Cleo = 338.635

//In 26 Year : Daphbe = 360

//Cleo = 355.567

//In 27 Year : Daphne = 370

//Cleo = 373.346

//Please press any key to continue```

5. Suppose you want to sell the book c + + for fouls. Please write a program to enter the sales volume (number of books, not sales) of each month in the whole year. The program prompts month by month using the char * array (or string object array) initialized as the month string through a loop, and stores the input data in an int array. Then, the program calculates the total number of elements in the array and reports the sales of the year.

```#include<iostream>

#include<string>

using namespace std;

int main()

{

const string Month[] = { "JAN","FEB","MAR", "APR", "MAY", "JUN", "JULY", "AUG", "SEP", "OCT", "NOV", "DEC" };

int sale_amount[12] = { };

unsigned int sum = 0;

for (int i = 0; i < 12; i++)

{

cout << "Please enter the sale amount of " << Month[i] << endl;

cin >> sale_amount[i];

}

cout << "Input done!" << endl;

for (int i = 0; i < 12; i++)

{

cout << Month[i] << " Sale: " << sale_amount[i] << endl;

sum += sale_amount[i];

cout << "Sale amount = " << sum << endl;

}

system("pause");

return 0;

}

//Please enter the sale amount of JAN

//100

//Please enter the sale amount of FEB

//200

//Please enter the sale amount of MAR

//100

//Please enter the sale amount of APR

//200

//Please enter the sale amount of MAY

//100

//Please enter the sale amount of JUN

//200

//Please enter the sale amount of JULY

//100

//Please enter the sale amount of AUG

//200

//Please enter the sale amount of SEP

//100

//Please enter the sale amount of OCT

//200

//Please enter the sale amount of NOV

//100

//Please enter the sale amount of DEC

//200

//Input done!

//JAN Sale : 100

//Sale amount = 100

//FEB Sale : 200

//Sale amount = 300

//MAR Sale : 100

//Sale amount = 400

//APR Sale : 200

//Sale amount = 600

//MAY Sale : 100

//Sale amount = 700

//JUN Sale : 200

//Sale amount = 900

//JULY Sale : 100

//Sale amount = 1000

//AUG Sale : 200

//Sale amount = 1200

//SEP Sale : 100

//Sale amount = 1300

//OCT Sale : 200

//Sale amount = 1500

//NOV Sale : 100

//Sale amount = 1600

//DEC Sale : 200

//Sale amount = 1800

//Please press any key to continue```

6. Complete programming exercise 5, but this time use a two-dimensional array to store the input - sales per month for 3 years. The program will report annual sales and total sales for three years.

```#include<iostream>

#include<string>

using namespace std;

const int months = 12;

const int years = 3;

int main()

{

const string Month[months] = { "JAN","FEB","MAR", "APR", "MAY", "JUN", "JULY", "AUG", "SEP", "OCT", "NOV", "DEC" };

int sale_amount[years][months] = { };

unsigned int sum = 0;

for (int i = 0; i < 3; i++)

{

cout << "In " << i+1 << " year`s sale amount "<<endl;

for (int j = 0; j < 12; j++)

{

cout << "Enter the sale amount of " << Month[j] << endl;

cin >> sale_amount[i][j];

}

cout << "End of " << i + 1 << " year`s data." << endl;

}

cout << "Input done!" << endl;

for (int i = 0; i < 3; i++)

{

for (int j = 0; j < 12; j++)

{

cout << Month[j] << " Sale: " << sale_amount[i][j] << endl;

sum += sale_amount[i][j];

cout << "Sale amount = " << sum << endl;

}

}

system("pause");

return 0;

}

//In 1 year`s sale amount

//Enter the sale amount of JAN

//100

//Enter the sale amount of FEB

//200

//Enter the sale amount of MAR

//100

//Enter the sale amount of APR

//200

//Enter the sale amount of MAY

//100

//Enter the sale amount of JUN

//200

//Enter the sale amount of JULY

//100

//Enter the sale amount of AUG

//200

//Enter the sale amount of SEP

//100

//Enter the sale amount of OCT

//200

//Enter the sale amount of NOV

//100

//Enter the sale amount of DEC

//

//100

//End of 1 year`s data.

//In 2 year`s sale amount

//Enter the sale amount of JAN

//123

//Enter the sale amount of FEB

//250

//Enter the sale amount of MAR

//100

//Enter the sale amount of APR

//200

//Enter the sale amount of MAY

//300

//Enter the sale amount of JUN

//100

//Enter the sale amount of JULY

//300

//Enter the sale amount of AUG

//500

//Enter the sale amount of SEP

//400

//Enter the sale amount of OCT

//100

//Enter the sale amount of NOV

//300

//Enter the sale amount of DEC

//200

//End of 2 year`s data.

//In 3 year`s sale amount

//Enter the sale amount of JAN

//100

//Enter the sale amount of FEB

//200

//Enter the sale amount of MAR

//50

//Enter the sale amount of APR

//300

//Enter the sale amount of MAY

//400

//Enter the sale amount of JUN

//600

//Enter the sale amount of JULY

//123

//Enter the sale amount of AUG

//120

//Enter the sale amount of SEP

//130

//Enter the sale amount of OCT

//150

//Enter the sale amount of NOV

//300

//Enter the sale amount of DEC

//198

//End of 3 year`s data.

//Input done!

//JAN Sale : 100

//Sale amount = 100

//FEB Sale : 200

//Sale amount = 300

//MAR Sale : 100

//Sale amount = 400

//APR Sale : 200

//Sale amount = 600

//MAY Sale : 100

//Sale amount = 700

//JUN Sale : 200

//Sale amount = 900

//JULY Sale : 100

//Sale amount = 1000

//AUG Sale : 200

//Sale amount = 1200

//SEP Sale : 100

//Sale amount = 1300

//OCT Sale : 200

//Sale amount = 1500

//NOV Sale : 100

//Sale amount = 1600

//DEC Sale : 100

//Sale amount = 1700

//JAN Sale : 123

//Sale amount = 1823

//FEB Sale : 250

//Sale amount = 2073

//MAR Sale : 100

//Sale amount = 2173

//APR Sale : 200

//Sale amount = 2373

//MAY Sale : 300

//Sale amount = 2673

//JUN Sale : 100

//Sale amount = 2773

//JULY Sale : 300

//Sale amount = 3073

//AUG Sale : 500

//Sale amount = 3573

//SEP Sale : 400

//Sale amount = 3973

//OCT Sale : 100

//Sale amount = 4073

//NOV Sale : 300

//Sale amount = 4373

//DEC Sale : 200

//Sale amount = 4573

//JAN Sale : 100

//Sale amount = 4673

//FEB Sale : 200

//Sale amount = 4873

//MAR Sale : 50

//Sale amount = 4923

//APR Sale : 300

//Sale amount = 5223

//MAY Sale : 400

//Sale amount = 5623

//JUN Sale : 600

//Sale amount = 6223

//JULY Sale : 123

//Sale amount = 6346

//AUG Sale : 120

//Sale amount = 6466

//SEP Sale : 130

//Sale amount = 6596

//OCT Sale : 150

//Sale amount = 6746

//NOV Sale : 300

//Sale amount = 7046

//DEC Sale : 198

//Sale amount = 7244

//Please press any key to continue```

1951 Kaiser

```#include<iostream>

#include<string>

using namespace std;

struct car_information

{

string manufacturer;

int date;

};

int main()

{

int car_number;

car_information *pcar;

cout << "How many cars do you wish to catlog?";

cin >> car_number;

cin.get();

pcar = new car_information[car_number];

for (int i = 0; i < car_number; i++)

{

cout << "Car # " << i + 1 << ":" << endl;

cout << "Please enter the make:";

getline(cin, pcar[i].manufacturer);

cin >> pcar[i].date;

cin.get();

}

cout << "Here is your collection:" << endl;

for (int i = 0; i < car_number; i++)

{

cout << pcar[i].date << " " << pcar[i].manufacturer << endl;

}

system("pause");

return 0;

}

//How many cars do you wish to catlog ? 2

//Car # 1:

//Please enter the make : Hodson Hornet

//Car # 2:

//Please enter the make : Kaiser

//1952 Hodson Hornet

//1951 Kaiser

//Please press any key to continue```

You should include the header file cstring in your program and use the function strcmp() for comparison testing.

```#include<iostream>

#include<cstring>

using namespace std;

const int SIZE = 20;

const char FINISHEED[] = "done";

int main()

{

int counter = 0;

char words[SIZE];

cout << "Enter words(to stop,type the word done):" << endl;

while (strcmp(FINISHEED, words) != 0)

{

counter++;

cin >> words;

cin.get();

}

cout << "You entered a total of " << counter - 1 << " words." << endl;

system("pause");

return 0;

}

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

//anteater birthday category dumpster envy finagle geometry done for sure

//You entered a total of 7 words.

//Please press any key to continue```

9. Write a program that meets the requirements described in the previous exercise, but uses a string object instead of a character array. Please include the header file string in the program and use relational operators for comparison test.

```#include<iostream>

#include<string>

using namespace std;

const char FINISHEED[] = "done";

int main()

{

int counter = 0;

string words;

cout << "Enter words(to stop,type the word done):" << endl;

while (words != FINISHEED)

{

counter++;

cin >> words;

cin.get();

}

cout << "You entered a total of " << counter - 1 << " words." << endl;

system("pause");

return 0;

}

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

//anteater birthday category dumpster envy finagle geometry done for sure

//You entered a total of 7 words.

//Please press any key to continue```

****

```#include<iostream>

using namespace std;

int main()

{

int line;

cout << "Enter number of words:";

cin >> line;

for (int i = 0; i < line; i++)

{

for (int j = 0; j < line - i - 1; j++)

{

cout << " .";

}

for (int j = 0; j < i; j++)

{

cout << " *";

}

cout << endl;

}

system("pause");

return 0;

}

//Enter number of words : 5

//. . . .

//. . . *

//. . * *

//. * * *

//* * * *

//Please press any key to continue

//Enter number of words : 10

//. . . . . . . . .

//. . . . . . . . *

//. . . . . . . * *

//. . . . . . * * *

//. . . . . * * * *

//. . . . * * * * *

//. . . * * * * * *

//. . * * * * * * *

//. * * * * * * * *

//* * * * * * * * *

//Please press any key to continue```

Tags: C++

Posted on Sat, 23 Oct 2021 02:11:06 -0400 by Tiigeress