2022-03-06 21:13

# 寻找数组最小值下标的函数中有一个语句不知道为什么无效(语言-c++)

``````#include<stdio.h>
void MAX_array(int arr[5]);
void MIN_array(int arr[5]);
int SUBSCRIPT_min(int arr[5]);
int SUBSCRIPT_max(int arr[5]);
int main()
{
int arr[5] = { 1,2,3,4,5 };
int i,s=0;
for (i = 0; i <5 ; i++)
{
scanf_s("%d", &arr[i]);
}
printf_s("Which number do you want,min or max?\n1)min      2)max(0 to quit)\n");
scanf_s("%d", &s);
while (s != 0)
{
switch (s)
{
case 1:MIN_array(arr);
printf_s("The subscript of the minimum is %d.\n", SUBSCRIPT_min(arr));
break;
case 2:MAX_array(arr);
break;
case 3:
default:printf_s("Error!\n");
break;
}
printf("Please enter 1 or 2.(0 to quit)\n");
scanf_s("%d", &s);
}
}
void MAX_array(int arr[5])
{
int i,max=0;
for ( i = 0; i < 4; i++)
{
if (arr[0]<arr[i+1])
{
arr[0] = arr[i + 1];
}
}
max = arr[0];
printf_s("The maximum is %d.\n", max);
}
void MIN_array(int arr[5])
{
int i, min = 0;
for (i = 0; i < 4; i++)
{
if (arr[0] > arr[i + 1])
{
arr[0] = arr[i + 1];
}
}
min = arr[0];
printf_s("The minimum is %d.\n", min);
}
int SUBSCRIPT_min(int arr[5])
{
int i, sub=0;
for (i = 0; i < 4; i++)
{
if (arr[0] > arr[i + 1])
{
arr[0] = arr[i + 1];
sub = i + 1;
}
}
return sub;
}

``````

#### 图片转代码服务由CSDN问答提供 功能建议

`````` GS C:USERS124208 DesktoplClmaxormin in the arraylx64\Debug\Project1.exe                                                                                                                                                   X
46
Whichumberdoyouwantminormax?
1)min              2MAX(0 TO QUIT)
THE MINiMUM IS 12.
The subscript of the minimum is 0.
PLEASE ENTER 1 OR 2.(0 TO quIT)``````
• 写回答
• 好问题 提建议
• 追加酬金
• 关注问题
• 邀请回答