首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
阅读以下说明和C++代码,将解答写入对应栏内。 【说明】 请编写一个函数int SeqSearch(int list[],int start,int n,int key),该函数从start开始,在大小为n的数组list中查找key值,返回最先找到的
阅读以下说明和C++代码,将解答写入对应栏内。 【说明】 请编写一个函数int SeqSearch(int list[],int start,int n,int key),该函数从start开始,在大小为n的数组list中查找key值,返回最先找到的
admin
2012-12-10
27
问题
阅读以下说明和C++代码,将解答写入对应栏内。
【说明】
请编写一个函数int SeqSearch(int list[],int start,int n,int key),该函数从start开始,在大小为n的数组list中查找key值,返回最先找到的key值的位置,如果没有找到则返回-1。请修改程序中画线部分的错误并将不同情况下的输出结果补充完整。
【程序】
文件search.cpp的内容如下:
#include <iostream. h >
int SeqSearch( int list[ ] ,int start,int n,int key)
{
for(int i=start;i<=n;i++) //(1)
{
if( list
= key)//(2)
{
return i;
}
}
return -1;
}
void main( )
{
int A[10]
int key,count=0,pos;
cout <<" Enter a list of 10 integers:";
for(pos=0;pos<10;pos++)
{
cin >>A; //(3)
}
cout <<" Enter a key; ";
cin >> key;
pos=0;
while(( pos = SeqSearch ( A, pos, 10, key)) !=-1 )
{
count ++;
pos ++;
}
cout<<key<<"occurs" <<count<< (count!=1?" times":" time") <<" in the list,"
<< endl;
}
第一种情况:输入2 3 12 6 8 45 8 33 7输入key:8
输出:(4)
第二种情况:输入2 3 126 8 45 8 33 7输入k6y:9
输出:(5)
选项
答案
(1)for(int i=start;i<n;i++) (2)if(list[i]==key) (3)cin>>A[pos] (4)8 occurs 2 times in the list (5)9 occurs 0 time in the list
解析
数组下标从0开始,所以n次循环要在i=n-1时结束,而不是在i=n时结束。判断是否相等使用“==”,一个等号是赋值。将输入数字逐个赋给A的pos处的元素。8出现2次,注意结果中的 times。9出现0次,注意结果中的time。
转载请注明原文地址:https://kaotiyun.com/show/76jZ777K
本试题收录于:
程序员下午应用技术考试题库软考初级分类
0
程序员下午应用技术考试
软考初级
相关试题推荐
计算机受病毒感染主要是______。
数据录入工作有两个指标:录入速度和错误率。一般而言,数据录入员在录入大批数据时,录入速度会(65),错误率会(66)。66
下列传输协议中,采用了加密技术的是______。
在Excel的A2单元格中输入:“=1=2”,则显示的结果是______。
某信息处理技术员正在做大批数据处理的大项目时,领导又交宋了另一项临时性的紧迫工作,要求优先处理。该信息处理技术员需要转而做新的工作,他对原工作的处理方式应该是(69)。
许多书上都说,人一次只能记住或处理5~9(7±2)条信息。为了检验这个结论是否正确,宜采用()调查方法。经过多次调查统计研究发现,人一次平均只能记住或处理4条信息。经考证,原来7±2的说法只是一位专家在一个讲演稿中的估计,并不是真正的调研报告,但却
某公司下设4个分公司A、B、C、D,上月各分公司的销售额及其在总公司所占比例如下表所示。由于此表单受潮,有些数据看不清了,但还可以推算出来。根据推算, D公司上月的销售额为(68)万元。
在Word2010文档中,可通过()设置选定内容的行间距。
在Windows系统中,控制面板的功能不包括______。
该公司用一个C类地址块202.119.110.0/24组网,将下表中的(1)~(6)处空缺的主机地址或子网掩码填写在答题纸的相应位置。可以采用(10)方法防止IP地址被盗用。(10)A.IP地址与子网掩码进行绑定B.IP地址与MAC地址进行
随机试题
女患者,32岁,尿频、尿急、尿痛1d。化验:尿常规白细胞满视野。应选用何种方法治疗
患者,男,46岁。残根,牙周组织支持条件好,已做过根管治疗。最好的修复方法是
既能祛风解表,又能透疹疗疮的药物是
患者,女,44岁,急性重症胆管炎,急诊入院,体温39.4℃,血压90/50mmHg(12.0/6.7kPa),脉搏100次/分钟,右下腹压痛,反跳痛,肌紧张,意识不清,不能诉说其他不适术后回到ICU病房,T管的处理是
分项详细估算法估算流动资金适用于______。
未结清的债权债务原始凭证和涉及其他未了事项的原始凭证,应由档案部门保管到未了事项完结后才能销毁。
某大型连锁经营超市因加盟商越来越多,规模不断扩大,欲在原有管理的基础上建立一套适合本企业未来发展的管理信息系统,以加强企业的日常管理、提高本企业的综合管理水平效率和经济效益。经对本超市的经验情况进行现状分析,并借鉴同行业管理水平一流的超市的管理系统的经验,
蔡某聚众扰乱某高校秩序,影响恶劣,被公安机关处以15日拘留并罚款500元。同时,蔡某与该校教师发生冲突,造成教师林某轻微伤,被公安机关处以10日拘留并罚款500元。对这两项治安处罚,该如何执行?()
如图,A、B、C三个原料加工厂分别停着甲、乙、丙三辆汽车,各车速度依次是60、48、36千米/时,各厂间的距离如图所示(单位:千米),如果甲、丙车按箭头方向行驶,乙车反向行驶,每到一厂甲车停2分,乙车停3分,丙车停5分。那么,三车同时开动后何时首次同时相遇
计算行列式Dn=,其中a1a2…an≠0.
最新回复
(
0
)