程序填空题:查找最大元素和最小元素的下标
查找一维数组 $$a$$ 的最大元素和最小元素的下标,分别存放在函数main()的maxsub和minsub变量中。
```c++
#include
void find(int *, int, int *, int *);
int main(void)
{
int maxsub, minsub, a[]={5, 3, 7, 9, 2, 0, 4, 1, 6, 8};
find(@@[a, 10, &maxsub, &minsub](1));
printf("%d,%d\n", maxsub, minsub);
return 0;
}
void find(int *a, int n, int *maxsub, int *minsub)
{
int i;
*maxsub = *minsub = 0;
for (i = 1; i < n; i++){
if (a[i] > a [*maxsub]){
@@[*maxsub = i](1) ;
}
if (a[i] < a [*minsub]){
@@[*minsub = i](1) ;
}
}
return;
}
```
答案:
第1空:a, 10, &maxsub, &minsub
第2空:*maxsub = i
第3空:*minsub = i
```c++
#include
void find(int *, int, int *, int *);
int main(void)
{
int maxsub, minsub, a[]={5, 3, 7, 9, 2, 0, 4, 1, 6, 8};
find(@@[a, 10, &maxsub, &minsub](1));
printf("%d,%d\n", maxsub, minsub);
return 0;
}
void find(int *a, int n, int *maxsub, int *minsub)
{
int i;
*maxsub = *minsub = 0;
for (i = 1; i < n; i++){
if (a[i] > a [*maxsub]){
@@[*maxsub = i](1) ;
}
if (a[i] < a [*minsub]){
@@[*minsub = i](1) ;
}
}
return;
}
```
答案:
第1空:a, 10, &maxsub, &minsub
第2空:*maxsub = i
第3空:*minsub = i