Advertisement
Guest User

Zad7

a guest
Jan 18th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <fstream>
  5. #include <string>
  6. #include <stdlib.h>  
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11. const int n = 3, precision = 2, width = 20;
  12.  
  13. int main()
  14.  
  15. {
  16.     // Tworzę rekord
  17.     struct Student
  18.     {
  19.         string name;
  20.         double grade = 0;
  21.     };
  22.  
  23.     ifstream file;
  24.     string fileName;
  25.  
  26.     cout << "Enter file name or path: ";
  27.     cin >> fileName;
  28.  
  29.     // Otwieram plik i sprawdzam czy dany plik istnieje
  30.     file.open(fileName, ios::in | ios::out);
  31.  
  32.     if (!file.good())
  33.     {
  34.         cout << "Error! File not found. Please use correct file name or path." << endl;
  35.         return 404;
  36.     }
  37.  
  38.     cout << endl;
  39.  
  40.     Student A[n][n];
  41.     string line;
  42.  
  43.     // Wypełniam tablicę A danymi z pliku
  44.     for (int i = 0; i < n; i++)
  45.     {
  46.         for (int j = 0; j < n; j++)
  47.         {
  48.             if (getline(file, line)) {
  49.  
  50.                 string name;
  51.                 double grade = 0;
  52.  
  53.                 stringstream stream(line);
  54.                 stream >> name;
  55.                 stream >> grade;
  56.  
  57.                 Student newStudent;
  58.  
  59.                 newStudent.name = name;
  60.                 newStudent.grade = grade;
  61.  
  62.                 A[i][j] = newStudent;
  63.             }
  64.         }
  65.     }
  66.  
  67.     // Drukuje tablice
  68.     for (int i = 0; i < n; i++)
  69.     {
  70.         for (int j = 0; j < n; j++)
  71.         {
  72.             cout << setw(width) << setprecision(precision) << fixed << A[i][j].name << " - " << A[i][j].grade;
  73.         }
  74.  
  75.         cout << endl;
  76.     }
  77.  
  78.     cout << endl;
  79.  
  80.     int gradeCounter = 0;
  81.     double sumOfGrades = 0;
  82.  
  83.     // Sprawdzam liczby >= 3 dla imion kończących się na 'a' lub 'k' pod główną przekątną tablicy
  84.     for (int i = 0; i < n; i++)
  85.     {
  86.         for (int j = 0; j < n; j++)
  87.         {
  88.             if (i > j)
  89.             {
  90.                 Student currentStudent;
  91.                 currentStudent = A[i][j];
  92.                 int lastLetter = currentStudent.name.length() - 1;
  93.  
  94.                 if ((currentStudent.grade >= 3.0) && (currentStudent.name[lastLetter] == 'a' || currentStudent.name[lastLetter] == 'k'))
  95.                 {
  96.                     gradeCounter++;
  97.                     sumOfGrades += currentStudent.grade;
  98.                 }
  99.             }
  100.         }
  101.     }
  102.  
  103.     // Obliczam średnią ocen, które spełniły warunki. Jeżeli nie było takich ocen, średnia wynosi 0
  104.     double averageFromGradesThatPassedTheTest = 0;
  105.  
  106.     averageFromGradesThatPassedTheTest = gradeCounter == 0 ? 0 : sumOfGrades / (double)gradeCounter;
  107.  
  108.     cout << "Average from grades that are above 3.0, for students that name ends with 'a' or 'k' and were placed under main diagonal: " << averageFromGradesThatPassedTheTest;
  109.     cout << endl;
  110.  
  111.     // Sprawdzam ile jest kolumn, w których średnia ocen przekracza wartość zmiennej averageFromGradesThatPassedTheTest
  112.     int columnCounter = 0;
  113.     double columnGradeSum = 0;
  114.     double averageInColumn = 0;
  115.  
  116.     for (int j = 0; j < n; j++)
  117.     {
  118.         for (int i = 0; i < n; i++)
  119.         {
  120.             columnGradeSum += A[i][j].grade;
  121.         }
  122.  
  123.         if (columnGradeSum / (double)n > averageFromGradesThatPassedTheTest) {
  124.             columnCounter++;
  125.         }
  126.     }
  127.  
  128.     cout << "Number of columns where grades average is bigger than average printed above: " << columnCounter << endl;
  129.  
  130.     file.close();
  131.  
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement