c++ primer plus 第七章-编程题7.13.6《Fill_array,Show_array,Reverse_array》

来源:互联网 发布:淘宝网唯品会 编辑:程序博客网 时间:2024/06/05 10:23
/* Enter a double number: 1 2 3 4 5 q Input 5 number to d[dSzie]. all d[0] ~ d[10]: 1 2 3 4 5 all d[0] ~ d[10]: 5 4 3 2 1 */#include <iostream>#include <array>#include <vector>using namespace std;const int dSize =10;int Fill_array(array<double, dSize> *);void Show_array(array<double, dSize> *,int);void Reverse_array(array<double, dSize> *,int);int main() {    array<double, dSize> d;    int count = Fill_array(&d);    cout << "Input " << count << " number to d[dSzie]." << endl;    Show_array(&d, count);    Reverse_array(&d, count);    Show_array(&d, count);}int Fill_array(array<double, dSize> * pd) {    int i = 0, count =0;    cout << "Enter a double number: ";    while (i < dSize){        if (cin >> (*pd)[i]) {            count++;            i++;        }        else            break;    }    return count;}void Show_array(array<double, dSize> * pd,int count) {    int i = 0;    cout << "all d[" << i << "]" << " ~ d[" << dSize << "]:\n";    while (i < count) {        cout << (*pd)[i++] << " ";    }    cout << "\n";}void Reverse_array(array<double, dSize> * pd,int count) {    int end = count - 1, start = 0;    double tmp;    while (start != end) {        tmp = (*pd)[start];        (*pd)[start] = (*pd)[end];        (*pd)[end] = tmp;        start++;        end--;    }}
0 0