enkov

Едномерен масив случайни числа, средна, мин и макс ст-ст

Oct 23rd, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include "stdafx.h"  // само при Visual Studio <= 2017
  2. #include <iostream>
  3. #include <cmath>
  4. #include <ctime> // заради time()
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     srand(static_cast<unsigned>(time(NULL))); // init random generator
  10.     const int n = 1000; // брой на оценките
  11.     double list[n]; // масив с оценки
  12.                     // генериране на случайни оценки
  13.     for (int i = 0; i<n; ++i)
  14.     {
  15.         // list[i] = rand() % 5 + 2 ; // 2-6 цяло число
  16.         list[i] = 2.0 + static_cast <double> (rand()) /
  17.         (static_cast <double> (RAND_MAX / (6.00 - 2.00)));
  18.         // закръгляме до втория знак
  19.         list[i] = list[i] * 100;
  20.         int x = list[i];
  21.         list[i] = x / 100.0;
  22.     }
  23.     // извеждане на списъка с оценки на екрана
  24.     cout << endl << "----- Results -----\n";
  25.     cout.setf(ios::fixed | ios::showpoint);
  26.     cout.precision(2);
  27.     for (int i = 0; i<n; ++i)
  28.         cout << "Student No " << i + 1 << " - "
  29.         << "evaluation: " << list[i] << endl;
  30.     cout << endl;
  31.     // намиране на средната оценка (среден успех)
  32.     double sum = 0;
  33.     for (int index = 0; index < n; index++)
  34.         sum = sum + list[index];
  35.     cout << "Sum of all evaluations is: " << sum << endl;
  36.     double average = sum / n;
  37.     cout << "Average evaluation is: " << average << endl;
  38.     // намиране на минимална оценка (стойност)
  39.     double min = list[0]; //  приемаме че 1-вият елемент е най-малък
  40.     for (int index = 1; index < n; index++)
  41.         if (list[index] < min)
  42.             min = list[index];
  43.     cout << "Minimal evaluation is: " << min << " and is at position(s): ";
  44.     // намиране на минимална оценка (позиции)
  45.     for (int index = 0; index < n; index++)
  46.         if (list[index] == min)
  47.             cout << index + 1 << " ";
  48.     cout << endl;
  49.     // намиране на максимална оценка (стойност)
  50.     double max = list[0]; //  приемаме че 1-вият елемент е най-голям
  51.     for (int index = 1; index < n; index++)
  52.         if (list[index] > max)
  53.             max = list[index];
  54.     cout << "Maximal evaluation is: " << max << " and is at position(s): ";
  55.     // намиране на максимална оценка (позиции)
  56.     for (int index = 0; index < n; index++)
  57.         if (list[index] == max)
  58.             cout << index + 1 << " ";
  59.     cout << endl;
  60.     return 0;
  61. }
Add Comment
Please, Sign In to add comment