输入N个浮点数,输出其中第m个~第n个数,满足m<n。 ----------------------------------------------------------------------------- 你需要编写以下4个函数,完成这个程序: double* allocate(int n),在动态内存上分配n个元素的double型数组,并返回其首地址。 void input(double* p, int n),向p中输入n个double型数值。 void output(double* p, int n),把长度为n的数组p中符合条件的第m个~第n个元素输出,m和n从标准输入读取。 void release(double* p),释放p所指的动态内存空间。 函数的调用格式见“Append Code”。
时间: 2023-06-03 18:02:11 浏览: 45
抱歉,我可以回答您的问题。以下是代码实现:
#include <iostream>
using namespace std;
double* allocate(int n) {
double* arr = new double[n];
return arr;
}
void input(double* p, int n) {
cout << "Please enter " << n << " floating point numbers:" << endl;
for (int i = 0; i < n; i++) {
cin >> *(p + i);
}
}
void output(double* p, int n) {
int m, k;
cout << "Please enter m and n (m < n, 1 <= m, n <= " << n << "):" << endl;
cin >> m >> k;
cout << "Elements from index " << m << " to " << k << ":" << endl;
for (int i = m - 1; i < k; i++) {
cout << *(p + i) << " ";
}
cout << endl;
}
void release(double* p) {
delete[] p;
}
int main() {
int n;
cout << "Please enter the number of elements:" << endl;
cin >> n;
double* arr = allocate(n);
input(arr, n);
output(arr, n);
release(arr);
return 0;
}
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)