编写一个本地c++程序,允许无限输入值,并将它们储存在空闲存储器中分配的数组中。然后,该程序应该5个一行输出这些值,之后输出输入值的平均值。最初的数组应该包含5个元素。程序应该在必需时创建比旧数组中的值复制到新数组中。
2)用指针怎么做
编写一个本地c++程序,允许无限输入值
答案:2 悬赏:40 手机版
解决时间 2021-01-23 10:08
- 提问者网友:佞臣
- 2021-01-22 09:55
最佳答案
- 五星知识达人网友:酒安江南
- 2021-01-22 10:01
//#include "stdafx.h"//vc++6.0加上这一行.
#include
using namespace std;
void main(void){
int *p,*pt,*px,ln,i,j,k,sum;
cout << "Please input data(Any letter end)...\n";
if((p=new int[5])==NULL){
cout << "Application memory failure...\n";
exit(0);
}
i=0;
ln=5;
while(cin >> p[i]){
if(++i==ln){
if((pt=new int[ln+5])==NULL){
cout << "Continue to apply for memory failure...\n";
exit(0);
}
memcpy(pt,p,sizeof(int)*ln);
px=p;
p=pt;
delete [ln]px;
ln+=5;
}
}
for(sum=k=j=0;j cout << p[j] << (++k%5 ? ' ' : '\n');
cout << "Their average is " << sum/(i+0.0) << endl;
delete [ln]p;
}
#include
using namespace std;
void main(void){
int *p,*pt,*px,ln,i,j,k,sum;
cout << "Please input data(Any letter end)...\n";
if((p=new int[5])==NULL){
cout << "Application memory failure...\n";
exit(0);
}
i=0;
ln=5;
while(cin >> p[i]){
if(++i==ln){
if((pt=new int[ln+5])==NULL){
cout << "Continue to apply for memory failure...\n";
exit(0);
}
memcpy(pt,p,sizeof(int)*ln);
px=p;
p=pt;
delete [ln]px;
ln+=5;
}
}
for(sum=k=j=0;j cout << p[j] << (++k%5 ? ' ' : '\n');
cout << "Their average is " << sum/(i+0.0) << endl;
delete [ln]p;
}
全部回答
- 1楼网友:蕴藏春秋
- 2021-01-22 11:09
class array{
public:
array(){
parr = new int[5];
size = 5;
top = 0;
sum = 0;
avg = 0;
}
array(const array& a){
size = a.size;
top = a.top;
parr = new int[size];
for(int i=0; i
-->
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
正方形一边上任一点到这个正方形两条对角线的 |
阴历怎么看 ? |