7.6来自C ++ Primer

时间:2015-04-30 23:48:44

标签: c++

int main()
{

    cout << "The maximum number of elements in an array is 100. " << endl;
    const int MAX = 100;
    double myarray[MAX];
    int arraysize = Fill_Array(myarray, MAX);
    Show_Array(myarray, arraysize);
    Reverse_Array(myarray, arraysize);
    Show_Array(myarray, arraysize);
    Reverse_Positions(myarray, arraysize);
    Show_Array(myarray, arraysize);
    return 0;
}

int Fill_Array(double ar[], int size)
{
    int i = 0;
    cout << "\nEnter a double value (Enter q to quit): ";
    while (cin >> ar[i] && i < size)
    {
        i++;
        cout << "Enter next value: ";
    }
    return i;
}

void Show_Array(const double ar[], int size)
{
    cout << "\nHere is your array: " << endl << endl;
    for (int i = 0; i < size; ++i)
        cout <<"\#"<<i+1<<" is "<<ar[i] << endl;
}

void Reverse_Array(double ar[], int size)
{
    cout << "Now we reverse the whole array: " << endl;
    for (int i = 0; i != size / 2; ++i)
    {
        double temp;
        temp = ar[i];
        ar[i] = ar[size - 1 - i];
        ar[size - 1 - i] = temp;
    }
}

void Reverse_Positions(double ar[], int size)
{
    cout << "Enter two positions: " << endl;
    int a, b;
    if (cin >> a >> b)
    {
        double temp = ar[b - 1];
        ar[b - 1] = ar[a - 1];
        ar[a - 1] = temp;
    }
}

reverse_position函数有问题:它只显示&#34;输入两个位置&#34;然后函数终止。我没有机会输入整数a和b。我怀疑输入队列有什么问题,但我不确定。

0 个答案:

没有答案