Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Obenauf
- * 19-01-18
- */
- #include <iostream>
- #include <string>
- struct students
- {
- double score;
- std::string name;
- };
- void sort(struct students *studentsptr, int);
- void display(struct students *studentsptr, int);
- int main()
- {
- students *studentsptr = nullptr;
- int average, numscores, total = 0;
- std::cout << "How many tests to input? ";
- std::cin >> numscores;
- studentsptr = new students[numscores];
- std::cout << "Enter name and score seperated by a space. \n";
- for (int i = 0; i < numscores; i++)
- std::cin >> studentsptr[i].name >> studentsptr[i].score;
- sort(studentsptr, numscores);
- std::cout << "The sorted results are: \n";
- display(studentsptr, numscores);
- for (int j = 0; j < numscores; j++)
- total += studentsptr[j].score;
- average = total / numscores;
- std::cout << "The average score is: " << average << std::endl; //displays score average
- delete[]studentsptr;
- studentsptr = nullptr;
- return 0;
- }
- void sort(students *studentsptr, int n)
- {
- int i, j, minidx, tempi;
- std::string temps;
- for (i = 0; i < n - 1; i++)
- {
- minidx = i;
- for (j = i + 1; j < n; j++)
- if (studentsptr[j].score > studentsptr[minidx].score)
- minidx = j;
- //swaps score
- tempi = studentsptr[minidx].score;
- studentsptr[minidx].score = studentsptr[i].score;
- studentsptr[i].score = tempi;
- //swaps name
- temps = studentsptr[minidx].name;
- studentsptr[minidx].name = studentsptr[i].name;
- studentsptr[i].name = temps;
- }
- }
- void display(students *studentsptr, int n)
- {
- for (int i = 0; i < n; i++)
- std:: cout << studentsptr[i].name << " " << studentsptr[i].score << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement