Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <string>
- #include <stdlib.h>
- #include <sstream>
- using namespace std;
- const int n = 3, precision = 2, width = 20;
- int main()
- {
- // Tworzę rekord
- struct Student
- {
- string name;
- double grade = 0;
- };
- ifstream file;
- string fileName;
- cout << "Enter file name or path: ";
- cin >> fileName;
- // Otwieram plik i sprawdzam czy dany plik istnieje
- file.open(fileName, ios::in | ios::out);
- if (!file.good())
- {
- cout << "Error! File not found. Please use correct file name or path." << endl;
- return 404;
- }
- cout << endl;
- Student A[n][n];
- string line;
- // Wypełniam tablicę A danymi z pliku
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if (getline(file, line)) {
- string name;
- double grade = 0;
- stringstream stream(line);
- stream >> name;
- stream >> grade;
- Student newStudent;
- newStudent.name = name;
- newStudent.grade = grade;
- A[i][j] = newStudent;
- }
- }
- }
- // Drukuje tablice
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- cout << setw(width) << setprecision(precision) << fixed << A[i][j].name << " - " << A[i][j].grade;
- }
- cout << endl;
- }
- cout << endl;
- int gradeCounter = 0;
- double sumOfGrades = 0;
- // Sprawdzam liczby >= 3 dla imion kończących się na 'a' lub 'k' pod główną przekątną tablicy
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if (i > j)
- {
- Student currentStudent;
- currentStudent = A[i][j];
- int lastLetter = currentStudent.name.length() - 1;
- if ((currentStudent.grade >= 3.0) && (currentStudent.name[lastLetter] == 'a' || currentStudent.name[lastLetter] == 'k'))
- {
- gradeCounter++;
- sumOfGrades += currentStudent.grade;
- }
- }
- }
- }
- // Obliczam średnią ocen, które spełniły warunki. Jeżeli nie było takich ocen, średnia wynosi 0
- double averageFromGradesThatPassedTheTest = 0;
- averageFromGradesThatPassedTheTest = gradeCounter == 0 ? 0 : sumOfGrades / (double)gradeCounter;
- 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;
- cout << endl;
- // Sprawdzam ile jest kolumn, w których średnia ocen przekracza wartość zmiennej averageFromGradesThatPassedTheTest
- int columnCounter = 0;
- double columnGradeSum = 0;
- double averageInColumn = 0;
- for (int j = 0; j < n; j++)
- {
- for (int i = 0; i < n; i++)
- {
- columnGradeSum += A[i][j].grade;
- }
- if (columnGradeSum / (double)n > averageFromGradesThatPassedTheTest) {
- columnCounter++;
- }
- }
- cout << "Number of columns where grades average is bigger than average printed above: " << columnCounter << endl;
- file.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement