//5-1
#include<iostream>
using namespace std;
int main()
{
int a, b;
int sum = 0;
cout << "Please enter two integers: ";
cin >> a >> b;
for (int i = a;i <= b; i++)
sum += i;
cout << "The sum of " << a << " and " << b << " is " << sum << endl;
return 0;
}
//5-2
#include<iostream>
#include<array>
using namespace std;
int main()
{
array<long double,101> factorials;
factorials[0] = factorials[1] = 1;
for (int i = 2; i < 101; i++)
factorials[i] = i * factorials[i - 1];
for (int i = 0; i < 101; i++)
cout << i << "! =" << factorials[i] << endl;
return 0;
}
//5-3
#include<iostream>
using namespace std;
int main()
{
int num;
int sum = 0;
cout << "please enter an number: ";
cin >> num;
while (num != 0)
{
sum += num;
cout << "the sum of all numbers is " << sum << endl;
cout << "please enter again: ";
cin >> num;
}
cout << "bye";
return 0;
}
//5-4
#include <iostream>
const double R1=0.1;
const double R2=0.05;
const double MONEY=100.0;
using namespace std;
int main()
{
double money1,money2;
int count=0;
money1=money2=MONEY;
while(money2<=money1)
{
money1+=MONEY*R1;
money2=money2*(1+R2);
count++;
cout<<"IN year#"<<count<<" money1: "<<money1<<" and money2: "<<money2<<endl;
}
cout<<"\nafter "<<count<<" years "<<"cleo's money > daphne's money "<<endl;
cout<<"cleo: "<<money2<<" and daphne: "<<money1<<endl;
return 0;
}
//5-5
#include <iostream>
using namespace std;
int main()
{
char * month[12]={
"January","February","March",
"April","May","June",
"July","August","September",
"October","November","December"};
int sale[12];
int sum=0;
cout<<"enter the sales of 12 months"<<endl;
for(int i=0;i<12;i++)
{
cout<<month[i]<<":";
cin>>sale[i];
sum+=sale[i];
}
cout<<"the sales of all year is "<<sum<<endl;
return 0;
}
//5-6
#include <iostream>
using namespace std;
const int YEAR=3;
const int MONTH=12;
int main()
{
int sale[YEAR][MONTH]={0};
int sum[YEAR]={0,0,0};
cout<<"Please enter sales of three years."<<endl;
for(int i=0;i<YEAR;i++)
{
cout<<"year#"<<i+1<<": ";
for(int j=0;j<MONTH;j++)
{
cin>>sale[i][j];
sum[i]+=sale[i][j];
}
cout<<"the sales of year#"<<i+1<<" is "<<sum[i]<<endl;
}
cout<<"the sales of all year is "<<sum[0]+sum[1]+sum[2]<<endl;
return 0;
}
//5-7
#include <iostream>
#include<string>
using namespace std;
struct car
{
string make;
int year;
};
int main()
{
int num;
cout << "How many cars do you wish to catalog? ";
cin >> num;
cin.get();
car* pt = new car[num];
for (int i = 0; i < num; i++)
{
cout << "Car #" << i + 1 << ":" << endl;
cout << "please enter the make: ";
getline(cin, pt[i].make);
cout << "please enter the year made: ";
cin >> pt[i].year;
cin.get();
}
cout << "Here is your collection:" << endl;
for (int i = 0; i < num; i++)
{
cout << pt[i].year << " " << pt[i].make<<endl;
}
return 0;
}
//5-8
#include <iostream>
#include<cstring>
const int Wsize = 80;
using namespace std;
int main()
{
char word[Wsize];
int count = 0;
cout << "Enter words (to stop,tpye the word done)" << endl;
cin >> word;
while (strcmp(word, "done"))
{
count++;
cin >> word;
}
cout << "You entered a total of " << count << " word." <<endl;
return 0;
}
//5-9
#include <iostream>
#include<string>
using namespace std;
int main()
{
string word;
int count = 0;
cout << "Enter words (to stop,tpye the word done)" << endl;
cin >> word;
while (word!="done")
{
count++;
cin >> word;
}
cout << "You entered a total of " << count << " word." <<endl;
return 0;
}
//5-10
#include <iostream>
using namespace std;
int main()
{
int row;
cout << "Enter number of rows: ";
cin >> row;
for (int i = 0; i < row; i++)
{
for (int j = 0; j <( row-(i+1)); j++)
cout << ". ";
for (int k = 0; k < (i + 1); k++)
cout << "* ";
cout << endl;
}
return 0;
}