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.
//myPractice501.cpp -- outputs the sum of all integers between the two integers (including the two integers) according to the two integers entered by the user #include <iostream> int main() { using namespace std; int num_1, num_2, sum = 0; cout << "Please enter the first integer:"; cin >> num_1; cout << "Please enter the second integer:"; cin >> num_2; for (int i = num_1; i <= num_2; ++i) { sum += i; } cout << num_1 << " reach " << num_2 << " The sum of all integers between is equal to " << sum << endl; return 0; }
two Write a loop using array objects (not arrays) and long double (not long long) to calculate and store the first 101 factorials.
//myPractice502.cpp -- write the first 101 factorials of the loop calculation using the array object and the long double type #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 << "!\t= " << factorials[i] << endl; return 0; }
3. Write a program that requires users to input numbers. After each input, the program will report the cumulative sum of all inputs so far. When the user enters 0, the program ends.
//myPractice503.cpp -- displays the sum of each number entered by the user. When the user enters 0, the program ends #include <iostream> using namespace std; int main() { //for loop double num_1, sum_1 = 0; cout << "Please enter a number:"; cin >> num_1; for (; num_1 != 0; ) { sum_1 += num_1; cout << "So far, the cumulative sum of all inputs is equal to " << sum_1 << endl; cout << "Please enter a number:"; cin >> num_1; } cout << "Enter 0, for End of cycle.\n\n"; //while Loop double num_2, sum_2 = 0; cout << "Please enter a number:"; cin >> num_2; while (num_2 != 0) { sum_2 += num_2; cout << "So far, the cumulative sum of all inputs is equal to " << sum_2 << endl; cout << "Please enter a number:"; cin >> num_2; } cout << "Enter 0, while End of cycle.\n\n"; //do while loop double num_3, sum_3 = 0; do { cout << "Please enter a number:"; cin >> num_3; sum_3 += num_3; cout << "So far, the cumulative sum of all inputs is equal to " << sum_3 << endl; } while (num_3 != 0); cout << "Enter 0, do while End of cycle.\n"; return 0; }
4. Daphne invested US $100 with 10% simple interest, that is, the profit of each year is 10% of the investment, that is, US $10 per year:
Interest = 0.1 × Original deposit
Cleo invested $100 at 5% compound interest, that is, the interest is 5% of the current deposit (including the interest obtained):
Interest = 0.05 × Current deposit
Cleo's profit from investing $100 in the first year is 5% - $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.
//myPractice504.cpp -- using cycle to judge the investment value of simple interest and compound interest #include <iostream> const double Interest_Daphne = 0.1; const double Interest_Cleo = 0.05; const int Capital_Base = 100; using namespace std; int main() { double capital_Daphne = Capital_Base; double capital_Cleo = Capital_Base; int year = 0; while (capital_Cleo <= capital_Daphne) { cout << "The first " << year++ << " In,\nDaphne Your deposits are:" << capital_Daphne << "\tCleo Your deposits are:" << capital_Cleo << endl; capital_Daphne += Interest_Daphne * Capital_Base; capital_Cleo += Interest_Cleo * capital_Cleo; } cout << endl << year << " Years later, Cleo The investment value finally exceeded Daphne!\n Now? Cleo Your deposits are:" << capital_Cleo << "\tDaphne Your deposits are:" << capital_Daphne << endl; return 0; }
5. Suppose you want to sell the book c + + for folders, please write a program to input the sales volume of each month in the whole year (the number of books, not the sales volume). The program will prompt month by month through a cycle using the char * array (or string object array) initialized as the month string. The input data is stored in an int array, and then the program calculates the total number of elements in the array and reports the sales of this year.
//myPractice505.cpp -- using circular access to the month information in the char * array or string object array, prompt the user to enter the monthly sales quantity and store it in the int array #include <iostream> #include <string> const int Months = 12; using namespace std; int main() { char * month_char[Months] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; string month_str[Months] = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" }; int quantity[12] = {}; unsigned int sum = 0; for (int i = 0; i < Months; i++) { cout << "Please enter" << month_char[i] << "Sales volume:"; cin >> quantity[i]; } cout << "Input complete. The monthly sales of the whole year are as follows:\n"; for (int i = 0; i < Months; i++) { cout << month_str[i] << ": \t" << quantity[i] << endl; sum += quantity[i]; } cout << "The total annual sales volume is:" << sum << endl; return 0; }
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.
//myPractice506.cpp -- Practice of using nested loops and two-dimensional arrays #include <iostream> #include <string> const int Months = 12; const int Years = 3; using namespace std; int main() { const char * month_char[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; const string month_str[] = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" }; int quantity[Years][Months] = {}; unsigned int sum_year[Years] = {}; unsigned int sum_years = 0; for (int i = 0; i < Years; i++) { for (int j = 0; j < Months; j++) { cout << "Please enter page " << i+1 << " Mid year" << month_char[j] << "Sales volume:"; cin >> quantity[i][j]; } } cout << "\n Input complete. The sales of each month in the three years are as follows:\n\n"; for (int i = 0; i < Years; i++) { cout << "The first " << i + 1 << " Year:\n"; for (int j = 0; j < Months; j++) { cout << month_str[j] << ": " << quantity[i][j] << " "; sum_year[i] += quantity[i][j]; } cout << endl; sum_years += sum_year[i]; } for (int i = 0; i < Years; i++) cout << "The first " << i + 1 << " The total sales volume in is:" << sum_year[i] << endl; cout << "The total sales volume in the three years is:" << sum_years << endl; return 0; }
7. Design a structure called car to store the following information about the car: Manufacturer (string stored in character array or string object), production year (integer). Write a program to ask the user how many cars there are, and then the program uses new to create a dynamic array composed of a corresponding number of car structures. Next, the program prompts the user to enter the manufacturer (possibly composed of multiple words) and year information of each vehicle. Note that this requires special care because it reads strings and numbers alternately. Finally, the program will display the contents of each structure. The operation of the program is as follows:
How many cars dou 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
//myPractice507.cpp -- use of structures, dynamic arrays, and loops #include <iostream> #include <string> using namespace std; struct car { string make; unsigned int year; }; int main() { unsigned int quantity; cout << "Please enter the number of recorded vehicles:"; cin >> quantity; cin.get(); car * collection = new car[quantity]; for (int i = 0; i < quantity; i++) { cout << "Car #" << i + 1 << ":\n"; cout << "Please enter the vehicle manufacturer's name:"; getline(cin, collection[i].make); cout << "Please enter the year of manufacture of the vehicle:"; cin >> collection[i].year; cin.get(); } cout << "\n Input completed. The vehicle information included this time is as follows:\n"; for (int i = 0; i < quantity; i++) cout << collection[i].year << " " << collection[i].make << endl; return 0; }
8. Write a program that uses a char array and loop to read one word at a time until the user inputs done. Then, the program indicates how many words the user inputs (excluding done). The following is the operation of the program:
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.
//myPractice508.cpp -- loop to read the input of string array #include <iostream> #include <cstring> const int Size = 20; const char DONE[] = "done"; using namespace std; int main() { int counter = 0; char words[Size]; cout << "Enter words (to stop, type the word done):\n"; while ( strcmp(DONE,words) !=0 ) { counter++; cin >> words; cin.get(); } cout << "You entered a total of " << counter - 1 << " words.\n"; return 0; }
9. Write a program that meets the requirements described in the previous exercise , But use string objects instead of character arrays. Please include the header file string in the program and use relational operators for comparison test.
//myPractice509.cpp -- loop through the input of the string object #include <iostream> #include <string> using namespace std; const string DONE = "done"; int main() { int counter = 0; string words; cout << "Enter words (to stop, type the word done):\n"; while ( words != DONE) { counter++; cin >> words; cin.get(); } cout << "You entered a total of " << counter - 1 << " words.\n"; return 0; }
10. Write a program that uses nested loops. Ask the user to enter a value to indicate how many lines to display. Then, the program will display the asterisk of the corresponding number of lines, in which the first line includes an asterisk, the second line includes two models, and so on. The number of characters in each line is equal to the number of lines specified by the user. If the asterisk is not enough, add a period before the asterisk. The operation of the program is as follows:
Enter number of rows: 5
....*
...**
..***
.****
*****
//myPractice510.cpp -- use of nested loops #include <iostream> using namespace std; int main() { int row; cout << "Please enter the number of lines to print:"; cin >> row; for (int i = 0; i < row; i++) //Overall controls the number of lines printed { for (int j = 0; j < row - i -1; j++) //Determine the number of periods according to the number of lines and asterisks cout << "."; for (int j = 0; j <= i; j++) //Determine the number of asterisks printed according to the number of lines cout << "*"; cout << endl; } return 0; }