请使用VC6或使用【答题】菜单打开考生文件夹pmj3下的工程proj3,其中包含源程序文件main.cpp和用户定义的头文件Array.h,整个程序包含有类Array的定义和主函数main的定义。请把主程序文件中的Array类的成员函数MinTwo()的定

admin2021-05-06  19

问题 请使用VC6或使用【答题】菜单打开考生文件夹pmj3下的工程proj3,其中包含源程序文件main.cpp和用户定义的头文件Array.h,整个程序包含有类Array的定义和主函数main的定义。请把主程序文件中的Array类的成员函数MinTwo()的定义补充完整,经修改后运行程序,得到的输出结果应为:
8
29,20,33,12,18,66,25,14
12.14
注意:只允许在“//**********333**********”和“//,********************666**********”之间填写内容,不允许修改其他任何地方的内容。
//Arty.h
#include
#incllude
usingnamespacestcl;
template
classArray{//数组类
public:
Array(Typeb[],intmm):size(mm)
{//构造函数
if(size<2){cout<<"数组长度太小,退出运行!";exit(1);}
a=newType[size];
for(inti=0;ia=b
}
~Array(){delete[]a;}//析构函数
voidMinTwo(Type&xl,Type&x2)
const;//由x1和x2带回数组a中最小的两个值
intLength()const{returnsize;}
//返回数组长度
Typeoperator[](inti)const{
//下标运算符重载为成员函数if(i<0||i>=size){cout<<"]:
标越界!"<returna
}
private:
Type*a;
intsize;
};
voidwriteToFile(constchar*);
//不用考虑此语句的作用
//main.cpp
#include"Array.h"
//由a和b带回数组a中最小的两个值
template
voidArray::MinTwo(Type&x1,Type&x2)const{
//补充完整函数体的内容
a[0]<=a[1]?(x1=a[0],x2=a[1]):
(x1=a[1],x2=a[0]);
//********333********
//********666********
}
intmain(){
ints1[8]={29,20,33,12,18,66,25,14);
Arrayd1(s1,8);
inti,a,b;
d1.MinTwo(a,b);
cout<for(i=0;i<7;i++)cout<<<",";tout<cout<writeToFile("");
//不用考虑此语句的作用
return0;
}

选项

答案for (int i = 2; i < size; i++) if (x2 > a[i]) if (x1 > a[i]) { x2 = x1; x1 = a[i]; } else { x2 = a[i]; }

解析 主要考查考生对数组的掌握,函数要求由a和b带回数组a中最小的两个值。使用for循环遍历数组,使用条件语句对数组元素进行比较操作,并把最小值赋给a和b。
转载请注明原文地址:https://kaotiyun.com/show/JLfp777K
0

相关试题推荐
最新回复(0)