enkov

Едномерен масив, средна, мин и макс ст-ст

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