1. Write a program that requires the user to enter two integers, and the giant program calculates and outputs the and output of all integers (including these two integers) between the two integers. This assumes that you first enter a smaller integer, for example, if the user enters 2 and 9, the program will point out that all integers between 2-9 and 44.
#include <iostream>
using namespace std;
int main ()
{
int x,y;
cin>>x;
cin>>y;
int sum=0;
for (int i=x;x<=y;x++)
{
sum=sum+x
}
cout<<sum<<endl;
return 0;
}
3. Write a request to enter the number of users of the program, each input, the program will report to date, all the input of the cumulative and, when the user input 0 o'clock, the program ends.
#include <iostream>
int main ()
{
using namespace std;
int sum=0;
int num;
cout<< "Enter the number; enter 0to quit:\n";
cin>>num;
while (num!=0)
{
sum +=num;
cout<<sum;
cin>>num;
}
cout<<endl;
cout<<sum<<endl;
return 0;
}
4.Daphne invested $100 in 10% of simple, each year earning 10% of its investment, or $10 a year. And Cleo invested 100 dollars in 5% of compound interest, the current deposit (including the interest earned) 5%,cleo in the first year to invest 100 dollars in the profit is 5% to get 105 dollars, the next year's profit is 105 dollars 5%, and so on to write a program, calculate how many years later, Cleo's investment value can exceed Daphne's investment value, and show two people's investment value.
#include <iostream>
int main ()
{
using namespace std;
Double daphne=100;
Double cleo=100;
int count=0;
while (Cleo<=daphne)
{
count++;
Daphne +=10;
Cleo *=1.05;
}
cout<<count<<endl;
cout<< "Daphne:" <<daphne<<endl;
cout<< "Cleo:" <<cleo<<endl;
return 0;
}
5. Suppose you want to sell the book "C++for Fool", please write a program to enter the annual sales volume, the number of books instead of sales
, the program uses a loop, a char* array that is initialized as a month string, or a string object array, a month-by-side input of data out of,, and un-miscellaneous 䘝int arrays, and then the program calculates the total number of elements in the array and reports the sales for the year.
#include <iostream>
int main ()
{
using namespace std;
int sales[12];
String month[12]={"Feb", "Mar", "APR", "may", "June", "may", "Aug", "Sept", "Oct", "Nov", "Dec"};
int sum=0;
for (int i=0;i<12;i++)
{
cout<< ' Please enter the sales of ' <<month[i]<< ' is: ';
cin>>sales[i];
Sum +=sales[i];
}
for (int i=0;i<+12;i++)
{
cout<< "the sales of <<month[i]<<" is: "<<sales[ i]<<endl;
}
cout<< "The sales of a" <<sum<<endl;
return 0;
}
6. Complete the programming exercise 5, but this time using a two-dimensional array to store input---3 years of sales per month. The program will report annual sales and total sales for three years.
#include <iostream> #include <string> int main () {using namespace std;
int sales[3][12];
String month[12]={"Feb", "Mar", "APR", "may", "June", "may", "Aug", "Sept", "Oct", "Nov", "Dec"};
int sum[3]={0,0,0}; for (int i=0;i<3;i++) {for (int j=0;j<12;j++) {cout<< ' Please enter the sales O
F "<<month[j]<<" in "<< (i+1) <<" year is: ";
cin>>sales[i][j];
Sum[i] +=sales[i][j]; } for (int i=0;i<=2;i++) {cout<< "the sales to" << (i+1) << "year is:" <<su
m[i]<<endl;
cout<< "The sales of three year is" << (sum[0]+sum[1]+sum[2]) <<endl;
return 0; }
7. Design a structure called car that stores the following information about the vehicle: the manufacturer (a string stored in a character array or a String object), the production year (integer). Write a program that asks the user how many cars there are. The program then uses new to create a dynamic array consisting of the corresponding number of car structures. Next, the program prompts the user to enter the manufacturer of each car (which may consist of multiple words) and year information. Note that this requires special care because it will alternately read values and strings (see chap. 4th). Finally, the program displays the contents of each structure.
#include <iostream> #include <string> struct car {char producer[20];//manufacturer int Pro_yea
r;//production year};
int main () {using namespace std;
cout<< "How many cars do your wish to catalog?"
int count;
(Cin>>count). get ();
Car *ca=new Car[count];
for (int i=1;i<=count;i++) {cout<< ' car # ' <<i<< ': ' <<endl;
cout<< ' Please enter the ' Make:;
Cin.getline (ca[i].producer,20);
Cin.getline (ca[i].producer,20);
cout<< "Please enter the year made:";
(cin>>ca[i].pro_year). get ();
} cout<< "Here is your collection:" <<endl;
for (int i=1;i<=count;i++) {cout<<ca[i].pro_year<< "" <<ca[i].producer<<endl;
} Delete []ca;
return 0; }
8. Write a program that uses a char array and loops to read one word at a time, until the user enters done. The program then indicates how many words the user has entered (excluding done). The following is the operation of the program: Enter words (to stop, type the word do): anteater birthday category dumpster Envy Finagle done For sure you entered a toal of 7 words. You should include the header file CString in your program and use the function strcmp () For comparison testing
#include <iostream>
#include <cstring>
#
int main ()
{
using namespace std;
int count=0;
Char word[20];
cout<< "Enter Worsds (to stop,type the word done):";
cin>>word;
while (strcmp (Word, "done"))
{
count++;
cin>>word;
}
cout<< "entered a total of" <<count<< "words" <<endl;
return 0;
}
9. Write a program that satisfies the one described in the previous exercise, but uses a string object instead of a character array. Include the header file string in your program, and use the relational operator for comparison testing.
#include <iostream>
#include <string>
int main ()
{
using namespace std;
int count=0;
string Word;
string c = "Done";
cout<< "Enter Worsds (to stop,type the word done):";
cin>>word;
while (Word!=c)
{
count++;
cin>>word;
}
cout<< "entered a total of" <<count<< "words" <<endl;
return 0;
}
10. Write a program that uses nested loops, requires the user to enter a value that indicates how many rows to display, and then the program will appear as an asterisk for the corresponding function, where the first row includes an asterisk, the second row includes two asterisks, and so on, each row contains a number of characters equal to the user-specified function, Precede the asterisk with a period.
Enter Number of Rows:5
....*
...**
.. ***
.****
*****
#include <iostream>
#include <string>
int main ()
{
using namespace std;
int count;
cout<< "Enter number of row:";
cin>>count;
for (int i=1;i<=count;i++)
{for
(int j=1;j<= (count-i); j + +)
{
cout<<. ";
}
for (int j=1;j<=i;j++)
cout<< "*";
cout<<endl;
}
return 0;
}