我的C ++代码有什么问题 [英] What is wrong in my C++ code
本文介绍了我的C ++代码有什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码中有编译错误
_array bound不是']'之前的整数常量
_ARRAY bound不是']'令牌之前的整数常量
我尝试了什么:
There is compilation error in my code
_array bound is not an integer constant before']' token
_ARRAY bound is not an integer constant before ']' token
What I have tried:
#include<iostream>
using namespace std;
int n;
int _array[n];
int _ARRAY[n-1];
swap(int *i,int *k)
{int j;// js any temperory element
j=*i;
*i=*k;
*k=j;
}
void sort(int _array[],int n )
{int hole;
int i;
hole = i;
for(int hole=1;hole<n;hole++)>
{
if( hole>0 && _array[hole-1]>_array[hole])
{swap(_array[hole],_array[hole-1]);
hole=hole-1;
}
}
}
int Findmindifference(){
int _array[n];
int _ARRAY[n-1];
sort(_array,n);
for(int i=0;i<n-1;i++)>
{
_ARRAY[i]=_array[i+1]-_array[i];
}
sort (_ARRAY,n-1);
return _ARRAY[0];
}
int main()
{
int t;
cin >> t;
for(int i=0;i<t;i++)>
{
cin >> n;
for(int j=0;j<n;j++)>
{
cin >>_array[j];
}
}
cout << Findmindifference();
}</iostream>
推荐答案
对于数组的声明,你必须指定一个常数。
for the declaration of an array in that way you MUST specify a constant number.
const int n = XXX; //your count goes here
int _array[n];
int _ARRAY[n-1];
替代动态分配:
alternativly the dynamic allocation:
int *_array = 0;//declare a pointer to int (can be used for array
//in some function
_array = new int[n];//n can be variant
//at the end: cleanup
delete _array;
一些罚款教程。
这篇关于我的C ++代码有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文