C++ PRIMER PLUS第六版课后编程答案5.1–5.5


5.1

#include <iostream>
#include<array>
using namespace std;

void  main51()
{
	int min,max;
	cout<<"Enter the min:";
	cin>>min;
	cout<<endl<<"Enter the max:";
	cin>>max;
	int sum=0;
	for(int i=min;i<=max;i++)
		sum+=i;
	cout<<"\nsum="<<sum<<endl;
	cin.get();

	// int ArrSize=max-min+1;
	//array<int,ArrSize> arr;




}

5.2

#include <iostream>
#include <array>
using namespace std;
const int ArrSize=101;
void main52()
{
	array<long double,ArrSize> arr;
	arr[0]=arr[1]=1L;
	for(int i=2;i<ArrSize;i++)
		arr[i]=arr[i-1]*i;
	for(int i=0;i<ArrSize;i++)
		cout<<i<<"!="<<arr[i]<<endl;
	cin.get();



}

 

5.3

#include <iostream>

void main53()
{
	using namespace std;
	cout<<"Plese Enter the number ,if is 0,for end:";
	double iput,sum=0;
	cin>>iput;
	while(iput)
	{
		sum+=iput;
		cout<<"\nnow sum is "<<sum<<" \nEnter a another number:";
		cin>>iput;
	}
	cin.get();
	


}

 

5.4

#include <iostream>
using namespace std;
void main54()
{
	double Dsum=10;
	double Csum=10;
	int i=0;
	while (Csum<=Dsum)
	{
		Dsum+=10;
		Csum+=Csum*0.05;
		i++;	
	}
	cout<<"After "<<i<<" year, Csum is bigger than Dsum"<<endl;
	cout<<"Dsum="<<Dsum<<"   Csum="<<Csum;
	cin.get();



}

 

5.5

#include <iostream>
void main55()
{
	using namespace std;
	char arr[15]={'1','2','3','4','5','6','7','8','9','10','11','12'};
	char *c=arr;
	
	
	int num[15];
	int sum=0;
	for(int i=0;i<12;i++,c++)
	{
		//void *c1=c;
		cout<<"The "<<*c<<" month number is ";
		cin>>num[i];
		sum+=num[i];
		cout<<"\n now sum is "<<sum<<endl;
	
	}
	cout<<"This year num is  "<<sum<<endl;
	cin.get();



}

 

 


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注