码迷,mamicode.com
首页 > 编程语言 > 详细

c++ 将输入存储到数组,然后反转数组,最后输出

时间:2018-09-28 23:01:16      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:reverse   --   结果   ble   enter   div   fir   str   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;
	}
}

  

c++ 将输入存储到数组,然后反转数组,最后输出

标签:reverse   --   结果   ble   enter   div   fir   str   c++   

原文地址:https://www.cnblogs.com/ranwuer/p/9721366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!