Forked from anonymous/gist:c8dcc6447abc69c226f9db7f28435369
Last active
March 22, 2017 03:50
-
-
Save ColeTownsend/510848f7e89120f1fbba26bf5b4428d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
struct StudentType | |
{ | |
string name; | |
int score; | |
char grade; | |
}; | |
const int size = 20; | |
void sort(StudentType students[], int number_used); | |
//Precondition: number_used <= declared size of the array a. | |
//The array elements a[0] through a[number_used - 1] have values. | |
//Postcondition: The values of a[0] through a[number_used - 1] have | |
//been rearranged so that a[0] <= a[1] <= ... <= a[number_used - 1]. | |
void swap_values(StudentType& v1, StudentType& v2); | |
//Interchanges the values of v1 and v2. | |
int index_of_smallest(StudentType students[], int start_index, int number_used); | |
//Precondition: 0 <= start_index < number_used. Referenced array elements have | |
//values. | |
//Returns the index i such that a[i] is the smallest of the values | |
//a[start_index], a[start_index + 1], ..., a[number_used - 1]. | |
int main() | |
{ | |
int n; | |
char yes_no; | |
do | |
{ | |
cout<<"How many students (between 1 and 20) will you be entering?"<<endl; // the number entered is no more than 20 students | |
cin>>n; | |
cin.ignore(); | |
StudentType students[size]; //this array holds the given number of students | |
for (int i=0;i<n;i++) | |
{ | |
cout<<"What is the name and score (between 1 and 100) of the student?"<<endl; | |
getline(cin,students[i].name); | |
cin>>students[i].score; | |
cin.ignore(); | |
if (students[i].score<0||students[i].score>=100) | |
{ | |
} | |
switch (students[i].score/10) //this returns the letter grade | |
{ | |
case 1: | |
case 2: | |
case 3: | |
case 4: | |
case 5: | |
students[i].grade='F'; | |
break; | |
case 6: | |
students[i].grade='D'; | |
break; | |
case 7: | |
students[i].grade='C'; | |
break; | |
case 8: | |
students[i].grade='B'; | |
break; | |
case 9: | |
students[i].grade='A'; | |
break; | |
default: | |
cout<< "This is not a valid grade.\n"; | |
students[i].grade=' '; | |
} | |
if (students[i].score>0&&students[i].score<=100) | |
{ | |
cout<<students[i].name<<"'s score: "<<students[i].score<<endl; | |
cout<<"grade: "<<students[i].grade<<endl<<endl; | |
} | |
} | |
int hs=students[0].score, index=0; //this finds the highest grade | |
for (int i=1;i<n;i++) | |
{ | |
if(students[i].score>hs) | |
{ | |
hs=students[i].score; | |
} | |
} | |
cout<<"This/these student(s) received the highest score of " <<hs<<": "<<endl; //this outputs the highest score | |
int i; | |
for (i=0;i<n;i++) //this returns the students who scored the highest score | |
{ | |
if(students[i].score==hs) | |
{ | |
cout<<students[i].name<<endl; | |
} | |
} | |
cout<<endl; | |
cout <<"In alphabetical order, the names of the students are: "<<endl<<endl; | |
sort(students, n); | |
for (int index = 0; index < n; index++) | |
{ | |
cout <<students[index].name<<endl; | |
} | |
cout<<endl; | |
cout<<"Do you want to run this program again? Y or N.\n"; | |
cin>>yes_no; | |
}while(yes_no=='y' || yes_no=='Y'); //allows the user to repeat the function | |
return 0; | |
} | |
void sort(StudentType students[], int number_used) | |
{ | |
int index_of_next_smallest; | |
for (int index = 0; index < number_used-1; index++) | |
{//Place the correct value in students[index]: | |
index_of_next_smallest = index_of_smallest(students, index, number_used); | |
swap_values(students[index], students[index_of_next_smallest]); | |
//students[0] <= students[1] <=...<= students[index] are the smallest of the original array | |
//elements. The rest of the elements are in the remaining positions. | |
} | |
} | |
void swap_values(StudentType& v1, StudentType& v2) | |
{ | |
StudentType temp; | |
temp = v1; | |
v1 = v2; | |
v2 = temp; | |
} | |
int index_of_smallest(StudentType students[], int start_index, int number_used) | |
{ | |
string min = students[start_index].name; //this variable stores the min values | |
int index_of_min = start_index; //this variable stores the index of the min element | |
for (int index = start_index + 1; index < number_used; index++) | |
{ | |
if (students[index].name < min) | |
{ | |
min = students[index].name; | |
index_of_min = index; | |
//min is the smallest of students[start_index] through students[index] | |
} | |
} | |
return index_of_min; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment