第九周2-我的数组类
来源:互联网 发布:游戏制作软件下载 编辑:程序博客网 时间:2024/06/05 01:13
#include<iostream>
using namespace std;
class MyArray
{
private:
int *arrayAddr; //保存一个有len个整型元素的数组的首地址
int len; //记录动态数组的长度
int max; //动态数组中的最大值(并非动态数组中必须要的数据成员)
public:
MyArray(int *a, int n);
~MyArray();
int getValue(int i); //获得数组中下标为i的元素的值
int getLen(); //返回数组长度
int getMax( ); //返回数组中的最大值
};
MyArray::MyArray(int *a,int n)
{
len=n;
arrayAddr=new int [n];
max=a[0];
int i;
for(i=0;i<n;i++)
{
arrayAddr[i]=a[i];
if(max<a[i])
max=a[i];
}
}
MyArray::~MyArray()
{
delete[]arrayAddr;
}
int MyArray::getValue(int i)
{
return arrayAddr[i];
}
int MyArray::getLen()
{
return len;
}
int MyArray::getMax( )
{
return max;
}
int main()
{
int b[10]= {75, 99, 90, 93, 38, 15, 5, 7, 52, 4};
MyArray r1(b,10);
cout<<"最大值:"<<r1.getMax()<<endl;
int c[15] = {18,68,10,52,3,19,12,100,56,96,95,97,1,4,93};
MyArray r2(c,15);
int i,s=0;
for(i=0; i<r2.getLen(); i++)
s+=r2.getValue(i);
cout<<"所有元素的和为:"<<s<<endl;
return 0;
}
using namespace std;
class MyArray
{
private:
int *arrayAddr; //保存一个有len个整型元素的数组的首地址
int len; //记录动态数组的长度
int max; //动态数组中的最大值(并非动态数组中必须要的数据成员)
public:
MyArray(int *a, int n);
~MyArray();
int getValue(int i); //获得数组中下标为i的元素的值
int getLen(); //返回数组长度
int getMax( ); //返回数组中的最大值
};
MyArray::MyArray(int *a,int n)
{
len=n;
arrayAddr=new int [n];
max=a[0];
int i;
for(i=0;i<n;i++)
{
arrayAddr[i]=a[i];
if(max<a[i])
max=a[i];
}
}
MyArray::~MyArray()
{
delete[]arrayAddr;
}
int MyArray::getValue(int i)
{
return arrayAddr[i];
}
int MyArray::getLen()
{
return len;
}
int MyArray::getMax( )
{
return max;
}
int main()
{
int b[10]= {75, 99, 90, 93, 38, 15, 5, 7, 52, 4};
MyArray r1(b,10);
cout<<"最大值:"<<r1.getMax()<<endl;
int c[15] = {18,68,10,52,3,19,12,100,56,96,95,97,1,4,93};
MyArray r2(c,15);
int i,s=0;
for(i=0; i<r2.getLen(); i++)
s+=r2.getValue(i);
cout<<"所有元素的和为:"<<s<<endl;
return 0;
}
0 0
- 第九周2-我的数组类
- 第九周-我的数组类
- 第九周项目2 我的数组类
- 第九周项目2-我的数组类
- 第九周项目2—我的数组类
- 第九周项目2 我的数组类
- 第九周项目2-我的数组类
- 第九周项目2——我的数组类
- 第九周【项目2-我的数组类】
- 第九周实践项目2-我的数组类
- 第九周项目2—我的数组类
- 第九周项目2-我的数组类
- 第九周项目(2)—我的数组类
- 第九周上机实践项目2:我的数组类
- 第九周项目2-我的数组类
- 第九周项目2-我的数组类
- 第九周项目2—我的数组类
- 第九周项目2-我的数组类
- 易經大意 441_460
- 【项目管理和构建】——Maven下载、安装和配置
- 一个简单的MVC实例
- 编程小练习
- 第9周项目1:深复制体验(2)
- 第九周2-我的数组类
- 指针逆序输出
- 第九周——动态分配,深复制;
- hive on spark安装
- 第八周上机实践项目3:指向学生类的指针
- Hadoop单机模式,并执行WordCount
- 第九周-工资
- 第八周项目2:用对象数组操作长方柱类
- spring mvc aop配置事务的相关配置