33,311
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<cctype>
using namespace std;
const int MaxSize =5;
int Fill_array(double*, int);
void Show_array(const double*, int);
void Reverse_array(double*, int);
int main()
{
double array[MaxSize];
int n = Fill_array(array, MaxSize);
cout << n << " elements wan entered!" << endl;
Show_array(array, n);
Reverse_array(array, n);
Show_array(array,n);
if (n <= 2)
cout << "The element is less than 3,we can not reverse it without the first and the last one." << endl;
else
{
Reverse_array(array + 1, n - 2);
Show_array(array, n);
}
//system("pause");
return 0;
}
int Fill_array(double*array, int num)
{
int i;
cout << "Please enter numbers(q to quit): ";
for (i = 0; i < num; ++i)
{
if (!(cin >> array[i]))
break;
}
//array[i] = '\0'; /*这是做什么?array是double,不是char* 类型*/
return i;
}
void Show_array(const double *array, int num)
{
for (int i = 0; i < num; ++i)
cout << array[i] << " ";
cout << endl;
}
void Reverse_array(double *array, int num)
{
double temp;
for (int i = 0, j = num-1; i<j; ++i, j--)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
哪儿有内存溢出的,没发现;
正常运行;
有一处,array是double类型,不是char*类型,所以没必要将'\0'赋值给array[i]