Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- В един университет се прави малка програма за работа със студенти.
- Всеки студент се описва с име, факултетен номер, курс (число от 1 до 4)
- и (средно-аритметичен) успех.
- Изведете информация за:
- 1. всички студенти;
- 2. студенти, които имат успех под 3.50 от 2-ри курс;
- 3. студентите с максимален и минимален успех от 3-ти курс;
- 4. за всеки курс средните успехи.
- */
- // Вариант без функции
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- const int n = 50;
- /* Вариант с 4 отделни масива: - сложно и неудобно
- string Name[n];
- string FacNo[n];
- int Course[n];
- float Uspeh[n];
- */
- // Със структура е по-удобно и по-лесно
- struct Student {
- string Name;
- string FacNo;
- int Course;
- float Uspeh;
- };
- Student Students[n];
- int x = 5; // ще работим с 5, за по-бързо въвеждане
- for (int i = 0; i < x; i++)
- {
- cout << "Enter information for student " << i << endl;
- cout << " Name: ";
- cin.get(); // skip the buffer
- getline(cin, Students[i].Name);
- cout << " Fac number: ";
- getline(cin, Students[i].FacNo);
- cout << " Course: ";
- cin >> Students[i].Course;
- cout << " Uspeh: ";
- cin >> Students[i].Uspeh;
- }
- // 1. всички студенти
- cout << "1. Information for all students: " << endl;
- for (int i = 0; i < x; i++)
- {
- cout << "Student " << i << ":"
- << " Name: " << Students[i].Name
- << " Fac number: " << Students[i].FacNo
- << " Course: " << Students[i].Course
- << " Uspeh: " << Students[i].Uspeh << endl;
- }
- // 2. студенти, които имат успех под 3.50 от 2 - ри курс;
- cout << "2. Students with grade less than 3.50 from 2-nd course: " << endl;
- for (int i = 0; i < x; i++)
- {
- if (Students[i].Uspeh < 3.50 && Students[i].Course == 2)
- cout << "Student " << i << ":"
- << " Name: " << Students[i].Name
- << " Fac number: " << Students[i].FacNo
- << " Course: " << Students[i].Course
- << " Uspeh: " << Students[i].Uspeh << endl;
- }
- // 3. студентите с максимален и минимален успех от 3 - ти курс;
- float minUspeh3, maxUspeh3;
- minUspeh3 = 0; maxUspeh3 = 0;
- for (int i = 0; i < x; i++)
- {
- if (Students[i].Course == 3)
- {
- // ако е 1-вия от 3-ти курс - запомняме
- if (minUspeh3 == 0)
- minUspeh3 = Students[i].Uspeh;
- if (maxUspeh3 == 0)
- maxUspeh3 = Students[i].Uspeh;
- // проверяваме
- if (Students[i].Uspeh < minUspeh3)
- minUspeh3 = Students[i].Uspeh;
- if (Students[i].Uspeh > maxUspeh3)
- maxUspeh3 = Students[i].Uspeh;
- }
- }
- cout << "3.1. Students with minimal grade from 3-rd course: " << endl;
- for (int i = 0; i < x; i++)
- {
- if (Students[i].Uspeh == minUspeh3 && Students[i].Course == 3)
- cout << "Student " << i << ":"
- << " Name: " << Students[i].Name
- << " Fac number: " << Students[i].FacNo
- << " Course: " << Students[i].Course
- << " Uspeh: " << Students[i].Uspeh << endl;
- }
- cout << "3.2. Students with maximal grade from 3-rd course: " << endl;
- for (int i = 0; i < x; i++)
- {
- if (Students[i].Uspeh == maxUspeh3 && Students[i].Course == 3)
- cout << "Student " << i << ":"
- << " Name: " << Students[i].Name
- << " Fac number: " << Students[i].FacNo
- << " Course: " << Students[i].Course
- << " Uspeh: " << Students[i].Uspeh << endl;
- }
- // 4. за всеки курс средните успехи
- float sums[5]; // 1..4
- int count[5];
- for (int i = 1; i <= 4; i++)
- {
- count[i] = 0; sums[i] = 0;
- }
- for (int i = 0; i < x; i++)
- {
- sums[Students[i].Course] += Students[i].Uspeh;
- count[Students[i].Course]++;
- }
- cout << "4. Average grades for course: " << endl;
- for (int i = 1; i <= 4; i++)
- {
- if (count[i] > 0)
- cout << " Course " << i << ": " << sums[i] / count[i] << endl;
- else
- cout << " Course " << i << ": has no students " << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement