Advertisement
Nexon

Untitled

Nov 11th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <math.h>
  4.  
  5. double fill_array(double *array);
  6. double nonzero_geometric_mean(double *array);
  7. double find_min_value_custom(double *array);
  8. double sort_min_value_custom(double *array);
  9. int record_to_file(double *array);
  10.  
  11. void main() {
  12.     setlocale(LC_ALL, "Russian");
  13.  
  14.     unsigned int array_count;
  15.    
  16.     std::cout<<"Введите кол-во элементов массива: ";
  17.     std::cin>>array_count;
  18.     std::cout<<std::endl;
  19.  
  20.     double *numbers = new double[array_count];
  21.     numbers[0] = array_count;
  22.  
  23.     *numbers = fill_array(numbers);
  24.     nonzero_geometric_mean(numbers);
  25.     find_min_value_custom(numbers);
  26.     *numbers = sort_min_value_custom(numbers);
  27.     record_to_file(numbers);
  28.  
  29.     system("PAUSE");
  30. }
  31.  
  32. double fill_array(double *array) {
  33.     double n = 1.0;
  34.  
  35.     std::cout<<"Введите n: ";
  36.     std::cin>>n;
  37.     std::cout<<std::endl;
  38.  
  39.     for(int i=1;i<=array[0];i++){
  40.         array[i] = (sin(5.0*i)+cos(10.0*i))/(tan(n*i)+5);
  41.     }
  42.  
  43.     return *array;
  44. }
  45.  
  46. double nonzero_geometric_mean(double *array) {
  47.     double mult_numbers = 1.0;
  48.     double nonzero_count = 0.0;
  49.     for(int i=1;i<=array[0];i++){
  50.         if(array[i] != 0.0){
  51.             nonzero_count++;
  52.             mult_numbers*=array[i];
  53.         }
  54.     }
  55.  
  56.     return pow(mult_numbers, 1/nonzero_count);
  57. }
  58.  
  59. double find_min_value_custom(double *array) {
  60.     double min = 1.0;
  61.  
  62.     for(int i=2*array[0]/3+1;i<=array[0];i++) {
  63.         if(fmod(i, 2.0) != 0){
  64.             if(min == 1.0)min = array[i];
  65.             if(array[i] < min)min = array[i];
  66.         }
  67.     }
  68.  
  69.     return min;
  70. }
  71.  
  72. double sort_min_value_custom(double *array) {
  73.     int done = 0;
  74.     double tmp;
  75.     int max = array[0]-2;
  76.  
  77.     while(done == 0){
  78.         done = 1;
  79.         for(int i=array[0]-array[0]/4+1;i<=max;i++) {
  80.             if(fmod(i, 2.0) != 0){
  81.                 if(pow(array[i], 2.0) < pow(array[i+2], 2.0)){
  82.                     tmp = array[i];
  83.                     array[i] = array[i+2];
  84.                     array[i+2] = tmp;
  85.                     tmp = 0.0;
  86.                     done = 0;
  87.                 }
  88.             }
  89.         }
  90.     }
  91.     return *array;
  92. }
  93.  
  94. int record_to_file(double *array) {
  95.     FILE *file = fopen("array.txt", "w");
  96.  
  97.     for(int i=1;i<=array[0];i++){
  98.         fprintf(file, "%.10f\n", array[i]);
  99.     }
  100.  
  101.     return fclose(file);
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement