double calculateAverage(double *a);
int main(){
double *dArray;
int n, m;
cout<<"\nplease enter the size of the array: ";
cin>>n;
dArray = new double[n];
for(m=0; m<n; m++){
cout<<"\nplease enter the number: ";
cin>>dArray[m];
}
*(dArray+m+1) = NULL; // i add this, so for the address after m+1 to be NULL
cout<<"\n the average of the array is : "<<calculateAverage(dArray)<<endl;
system("pause");
return 0;
}
double calculateAverage(double *a){
double total = 0;
int iCounter = 0;
while(*a!=NULL){ // here is the problem!! why it can't be NULL or 0 or empty? // edit from a to *a
total = total+*a;
a++;
iCounter++;
}
return total/(iCounter-1); //here edit, from iCounter to iCounter-1
}
我想知道为什么指针不指向 NULL ?谁能告诉我代码哪里出错了?
谢谢你。
一个错字,在分配循环块的数组中应该是“n”而不是“nn”。