Exercise1.17
#include <iostream>
int main()
{
int limit = 0, k = 0, value = 0;
std::cout << "How many integers would you like to enter?";
std::cin >> limit;
// assume we don‘t know what is EOF(End-Of-File).
while (std::cin >> value && (--limit != 0))
if (value<0) {
k++;
}
std::cout << "the number of negative values is " << k<<std::endl;
return 0;
}
Exercise1.18
#include <iostream>
using namespace std;
int main()
{
cout<<"Enter two numbers:";
int v1,v2;
cin>>v1>>v2;
int lower,upper;
if (v1>=v2) {
upper=v1;lower=v2;
}
else { upper=v2;lower=v1;
}
cout<<"Value of "<<lower<<" to "<<upper<<" inclusive are: "<<endl;
for (int value=lower;value<=upper;value++)
cout<<value<<" ";
return 0;
}
Exercise1.19
#include <iostream>
using namespace std;
int main()
{
cout<<"Enter two numbers:";
int v1,v2;
cin>>v1>>v2;
int lower,upper;
if (v1>=v2) {
upper=v1;lower=v2;
}
else { upper=v2;lower=v1;
}
cout<<"Value of "<<lower<<" to "<<upper<<" inclusive are: "<<endl;
for (int value=lower,count=1;value<=upper;value++,count++) {
cout<<value<<" ";
if (count%10==0)
cout<<endl;
}
return 0;
}
添加的变量count是为了记录已输出数的个数,若count的值是10的整数倍,则输出一个换行符。