现在我的功能更好,但它没有返回最小数量,它返回第二大...任何人都可以帮助我吗?我找不到错误。我的功能总是返回一个巨大的数字
#include <iostream>
#include <cstdio>
#include <cstdlib>
void add_min(int*& a, int n){
int c;
for(int i = 0; i < n - 1; i++){
if(a[i + 1] < a[i]){
c = a[i + 1];
}
else{
c = a[i];
}
}
std::cout<< c <<std::endl;
for(int s = 0; s < n; s++){
a[s] += c;
}
for(int z = 0;z < n; z++){
std::cout<< a[z] <<std::endl;
}
}
int main(){
int n,i;
std::cout<< "please enter the dimention of the array" <<std::endl;
std::cin>> n;
int *arr = new int[n-1];
std::cout<< "please enter the integers in the array" <<std::endl;
for(i = 0;i < n; i++){
std::cin>>arr[i];
}
add_min(arr, n);
delete [] arr;
return 0;
}
这是这个程序的问题中最少的。我开始不传递数组并使用未初始化的数组。 – Hasturkun
是的,这个错误比右边多了。 –
从技术上讲,您没有有效的C++程序,因为C++(如C++ 11)没有[可变长度数组](http://en.wikipedia.org/wiki/Variable-length_array)。所以'add_min'中的声明'int a [n]'在技术上不是一个有效的声明。 –