阅读以下说明和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[i] = 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题:
阅读下列程序说明和C代码,将应填入(n)处的字句写在对应栏内。
[函数2.1说明]
函数void find(int *a, int n, int * max, int * min)的功能是在长度为n的整型数组a中,查找最大元素和最小元素的下标。main()中给出了调用find函数的一个实例。
[函数2.1]
include<stdio.h>
void find(int *a, int n,int *max,int * min)
{ int i;
*max =* min=0;
for(i=1;i<n;i+ +)
if(a[i]>a[* max]) (1);
else if(a[i]<a[*min]) (2);
return;
main()
{ int a[]={4,6,8,9,0,6},max,min;
find(a,6,(3));
printf("%5d%5d\n", max,min);
}
[函数2.2说明]
以下程序用来对从键盘上输入的两个字符串进行比较,然后输出两个字符串前端的公共部分。例如:输入的两个字符串分别是abcdefg和abceef,则输出为abc。
[函数2.2]
include <stdio.h>
main()
{ char str1[100],str2[100],str[100],c;
int i=0,s;
printf("\nInput string 1:");gets(str1);
printf("\nInput string 2:");gets(str2);
while(((4))&&(str1[i]!='\0')&&(str2[i]!='\0')){
(5);
i++;
}
printf("%s\n",str);
}
第2题:
第3题:
这道题是给使用C/C++语言的同学准备的。使用其他语言的同学,随便猜一项答案就好啦。 以下C/C++代码: void func(_______ , int); int main(){ int array[10][20]; int n; // 省略array和n的初始化 func(array, n); return 0; } 第1行的函数原型(函数声明)中,______部分的第一个参量应该是
A.int [ ][ ]
B.int **
C.int *[20]
D.int (*)[20]
第4题:
第5题: