c++ 将输入存储到数组,然后反转数组,最后输出
生活随笔
收集整理的這篇文章主要介紹了
c++ 将输入存储到数组,然后反转数组,最后输出
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
// 輸入一個包含多個double元素的數組,先打印結果,然后反轉出頭和尾元素之外的所有元素,最后再打印結果
#include <iostream>
using namespace std;
int fill_array(double arr[], int size);
void show_array(double arr[], int size);
void reverse_array(double arr[], int size);int main() {int size;int inputCount;cout << "Input size: ";cin >> size;double * arr = new double[size];inputCount = fill_array(arr, size);cout << "Entered count: " << inputCount << endl;cout << "Your input: ";show_array(arr, size);cout << "Reverse array (without first and last element)..." << endl;double first = arr[0];double last = arr[size - 1];reverse_array(arr, size);arr[0] = first;arr[size - 1] = last;cout << "The final: ";show_array(arr, size);delete[] arr;return 0;
}int fill_array(double arr[], int size) {int count = 0;for (int i = 0; i < size && cin >> arr[i]; i++) {count++;}return count;
}void show_array(double arr[], int size) {for (int i = 0; i < size; i++) {cout << arr[i];}cout << endl;
}void reverse_array(double arr[], int size) {double temp;int iterCount = size/2;--size;for (int i = 0; i < iterCount; i++, size--) {temp = arr[i];arr[i] = arr[size];arr[size] = temp;}
}
轉載于:https://www.cnblogs.com/ranwuer/p/9721366.html
總結
以上是生活随笔為你收集整理的c++ 将输入存储到数组,然后反转数组,最后输出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 可迭代对象、迭代器与生成器
- 下一篇: [洛谷P4726]【模板】多项式指数函数