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;
	}
}

  

posted @ 2018-09-28 22:45  ranwuer  阅读(1860)  评论(0编辑  收藏  举报