- #include <iostream>
- #include <cstdlib>
- #include <cassert>
- using namespace std;
- //获得要输入的double类型的个数
- void allocate_double(double*& p, size_t& n);
- //将获取的输入存进数组
- void fill_array(double data[], size_t n);
- //求数组的平均数
- double average(const double data[], size_t n);
- //将数组各个数和平均数进行比较
- void compare(const double data[], size_t n, double value);
- int main()
- {
- double *numbers;
- size_t array_size;
- double mean_value;
- cout << "this program will comput the aerage of some numbers.the\\n";
- cout << "numbers will be stored in an array of doubles that I allocae.\\n";
- allocate_double(numbers, array_size);
- fill_array(numbers, array_size);
- mean_value = average(numbers, array_size);
- cout << "the average is:" << mean_value << endl;
- compare(numbers, array_size, mean_value);
- cout << "this was a mean program.";
- return EXIT_SUCCESS;
- }
- void allocate_double(double*& p, size_t& n)
- {
- cout << "how many double should I callocate?" << endl;
- cout << "Please type a positive integer answer:" << endl;
- cin >> n;
- p = new double[n];
- }
- void fill_array(double data[], size_t n)
- {
- size_t i;
- cout << "please type " << n << " double numbers:" << endl;
- for(i = 0; i < n; ++i)
- {
- cin >> data[i];
- }
- }
- void compare(const double data[], size_t n, double value)
- {
- size_t i;
- for(i = 0; i < n; ++i)
- {
- cout << data[i];
- if(data[i] < value)
- {
- cout << "is less then " ;
- }
- else if(data[i] > value)
- {
- cout << "is large then " ;
- }
- else
- {
- cout << "is equal to ";
- }
- cout << value << endl;
- }
- }
- double average(const double data[], size_t n)
- {
- size_t i;
- double sum;
- assert(n > 0);
- sum = 0;
- for(i = 0; i < n; ++ i)
- {
- sum += data[i];
- }
- return sum/n;
- }
- //该片段来自于http://www.codesnippet.cn/detail/020720149885.html
来源: http://www.codesnippet.cn/detail/020720149885.html