1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void calcdata(int [], float [] , char [], string []);
void selectionSort(string [], char [], float []);
int binarySearch( string [], int, string);
void displayName(string[] , char [], int);
int main ()
{
int grade [5]= { 0 };
float avg [4] = { 0 }; //# of tests
char calif[5]={0};
int sum=0;
string name[5];
char again;
for (int j=0; j<5; j++){
cout<<"Enter name of student " <<j+1 <<": " <<"\n";
cin >> name[j];
cout<<": " <<j+1 <<"Enter students scores \n";
for (int i=0;i<4; i++){
cout<<"The grade " << i+1 << "es : " << "\n";
cin >>grade[i] ;
sum=sum + grade[i];
avg[j]=sum/4;
}
sum=0;
}
calcdata(grade,avg,calif,name);
//orginize calcdata
selectionSort(name, calif, avg);
do{
string studName; // To hold students name
int index; // To hold search results
cout<<"What name do you want?\n";
cin>>studName;
// Search for the name.
index = binarySearch(name,5, studName);
while(index == -1){
cout << "Error: Este nombre no esta en la base de datos. Re-entre: ";
cin >> studName;
index = binarySearch(name,5, studName);
}
displayName(name, calif, index);
// Does the user want to do this again?
cout << "Would you like to look up another name? (y/n) ";
cin >> again;
} while (again == 'y' || again == 'Y');
return 0;
}
void calcdata(int grade[], float avg[], char calif[], string name[])
{
for (int j=0;j<5; j++) {
if(avg[j]<=100 && avg[j]>=90)
calif[j]='A';
else
if(avg[j]<=89 && avg[j]>=80)
calif[j]='B';
else
if(avg[j]<=79 && avg[j]>=70)
calif[j]='C';
else
if(avg[j]<=69 && avg[j]>=60)
calif[j]='D';
else
calif[j]='F';}
for(int j=0;j<5;j++){
cout << "Name: " << name[j]
<<" Average: " << avg[j]
<<" Grade: " << calif[j]
<< endl;
}
}
void selectionSort(char calif[], string name[], float avg[])
{
for (int startIndex = 0; startIndex < 5; ++startIndex)
{
int smallestIndex = startIndex;
for (int currentIndex = startIndex + 1; currentIndex < 5; ++currentIndex)
{
if (name[currentIndex] < name[smallestIndex])
smallestIndex = currentIndex;
}
std::swap(name[startIndex], name[smallestIndex]);
std::swap(calif[startIndex], calif[smallestIndex]);
std::swap(avg[startIndex],avg[smallestIndex]);
}
}
int binarySearch(string array[], int numElems, string value)
{
int first = 0;
int last = numElems − 1;
int middle;
int position = −1;
bool found = false;
while (!found && first <= last){
middle = (first + last) / 2;
if (array[middle] == value)
{
found = true;
position = middle;
}
else
if (array[middle] > value)
last = middle − 1;
else
first = middle + 1;
}
return position;
}
void displayName( string name[], char calif[], int index)
{
cout << "Name: " << name[index] << endl;
cout << "Grade: " << calif[index] << endl;
}
|