回 帖 发 新 帖 刷新版面

主题:帮我纠正错误,谢谢

源文件代码:

#include <iostream>

using namespace std;

double* Fill_array(double arr[], int);
void Reverse_array(double*, double*);


int main()
{
    const int ArSize=10;
    double* arrend;
    double arr[ArSize];
    
    arrend=Fill_array(arr, ArSize);
    Reverse_array(arr, arrend);
    
    cin.get();
    return 0;
}


double* Fill_array(double arr[], int size)
{
    int counts=0;    
    
    cout<<"请输入double型数据,以空格分开:\n ";
    while(counts<size && cin>>arr[counts])
        counts++;
        
    return &arr[counts];
}


void Reverse_array(double* start, double* end)  [color=FF0000]//这个函数里的代码有问题,无法正确输出反转后的数组,谢帮我改改,谢谢[/color]{
     double tmpdata;
     
     for(; start!=end; start++, end--)
     {
         tmpdata=*start;
         *start=*end;
         *end=tmpdata;
         cout<<*start<<endl;
     }
     
     cout<<"反转后的数据为: ";
     for(; start!=end; start++)
         cout<<*start<<"  ";
          
     cout<<"\n\n";
}             
         

回复列表 (共3个回复)

沙发

大致看了一下,start和end指向哪里好像没有说明...

板凳

简单看一下,严重逻辑错误

你自己单步调试一下,看看变量

3 楼

我帮你改了,自己看吧,主要是指针自加的时候要考虑一下
#include <iostream>

using namespace std;

double* Fill_array(double arr[], int);
void Reverse_array(double*, double*);


int main()
{
    const int ArSize=4;
    double* arrend;
    double arr[ArSize];
    arrend=Fill_array(arr, ArSize);
    Reverse_array(arr, arrend);
    cin.get();
    return 0;
}


double* Fill_array(double arr[], int size)
{
    int counts=0;
    cout<<"请输入double型数据,以空格分开:\n ";
    while(counts<size && cin>>arr[counts])
        counts++;
    return &arr[counts - 1];
}


void Reverse_array(double* start, double* end) {  //这个函数里的代码有问题,无法正确输出反转后的数组,谢帮我改改,谢谢{
     double tmpdata;
     double* p_start = start;
     double* p_end = end;
     for(; start<end; start++, end--)
     {
         tmpdata=*start;
         *start=*end;
         *end=tmpdata;
//         cout<<*start<<endl;
     }
     cout<<"反转后的数据为: ";
     for(; p_start<=p_end; p_start++)
         cout<<*p_start<<"  ";
cout<<"\n\n";
}
-----------------------------------------
请输入double型数据,以空格分开:
 2.1 3.2 78.9 9.3
反转后的数据为: 9.3  78.9  3.2  2.1  


我来回复

您尚未登录,请登录后再回复。点此登录或注册