C++ 如何打印数组的最小数目及其索引(使用函数调用它们)?

C++ 如何打印数组的最小数目及其索引(使用函数调用它们)?,c++,arrays,function,C++,Arrays,Function,我知道要实现这个函数,我需要使用void type这是我的代码。请有人告诉我如何使用void type实现它,并在main中调用它。我尝试过这么做,但失败了。最近我遇到的一件事是只返回数组的最小数 #include <iostream> using namespace std; int min(int arr[], int size) { int small=arr[0]; for(int i=0; i<size; i++) if(arr[i]

我知道要实现这个函数,我需要使用void type这是我的代码。请有人告诉我如何使用void type实现它,并在main中调用它。我尝试过这么做,但失败了。最近我遇到的一件事是只返回数组的最小数

#include <iostream>
using namespace std;
int min(int arr[], int size)
{

    int small=arr[0];
    for(int i=0; i<size; i++)
        if(arr[i]<small)
            small=arr[i];
        return small;
}

int main()
{
    int size;
    cin>>size;
   int X[size];
   for(int i=0; i<size; i++)
    cin>>X[i];

 cout<<"Min num in the array = " << min(X,size) <<endl;


    return 0;
}
#包括
使用名称空间std;
最小整数(整数arr[],整数大小)
{
int small=arr[0];
对于(int i=0;isize;
int X[大小];
对于(int i=0;i>X[i];
主题1
在通过引用传递的参数中返回它

void min(int arr[], int& index)
{
   ...
}
并将该函数用作

int index = 0;
min(X, index);
cout << "The index of the min in the array = " << index << endl;
cout << "Min num in the array = " << X[index] << endl;
int index = min(X);
cout << "The index of the min in the array = " << index << endl;
cout << "Min num in the array = " << X[index] << endl;
并将该函数用作

int index = 0;
min(X, index);
cout << "The index of the min in the array = " << index << endl;
cout << "Min num in the array = " << X[index] << endl;
int index = min(X);
cout << "The index of the min in the array = " << index << endl;
cout << "Min num in the array = " << X[index] << endl;
int index=min(X);

如果函数无法返回值,您需要为此使用引用。
auto ptr=std::min_元素(X,X+size);std::cout请注意,在标准
c++
中,这是非法的:
int X[size];
因为
size
必须是编译时常量,