-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScore the Race.cpp
More file actions
86 lines (72 loc) · 2.04 KB
/
Score the Race.cpp
File metadata and controls
86 lines (72 loc) · 2.04 KB
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
//Mohammed Chowdhury
//Assignment #3 - Score the Race
#include<iostream>
#include<iomanip>
#include <algorithm>
using namespace std;
bool valid (string results); //Checks if result entered is valid.
int main(){
string results;
char winnerOfRace=' ';
double scores[26]={0};
int AmountOfMembers[26]={0};
int teams=0,runners=0,score=0;
Begin:
cout<<"What is the result of the race? Enter 'done' when you have finished: ";
cin>>results;
if (results == "done"){
cout << "Ending Program..." << endl;
return 0;
}
if(valid(results)){
for(int i=0;i<results.size();i++){
AmountOfMembers[(int)results[i]-65]++; //this will put each team into one element of an array and add one when there is a new member
scores[(int)results[i]-65]+=i+1; //adds points for each team depeding on their positions
}
}
else cout << "INVALID\n";
score=scores[0];
for(int i=0;i<26;i++){
if(AmountOfMembers[i]>0){ //goes through the alphabet and tells c++ which teams are contending in the race.
teams++;
runners=AmountOfMembers[i];
}
}
for(int i=0;i<26;i++){
if(scores[i]>0){
scores[i]/=runners;
if(scores[i]<score){
score=scores[i];
winnerOfRace=(char)(i+65);
}
}
}
cout<<"Number of teams: "<<teams<< endl<<endl;
cout<<"Numver of Runners per team: "<<runners<<endl<<endl;
cout<<"Team:\t"<<"Score:"<<endl;
for(int i=0;i<26;i++){
if(AmountOfMembers[i]>0){
cout<<(char)(i+65)<<"\t"<<"\t"<<setprecision(2)<<fixed<<scores[i]<<endl;
}
}
cout<<"Team "<<winnerOfRace<<" won with a score of "<<setprecision(2)<<fixed<<scores[(int)(winnerOfRace)-65]<<"."<<endl;
goto Begin;
return 0;
}
bool valid (string results){
int a[1000] = {};
int *end = a + results.size();
bool validity = false;
int counter = 0;
for (int i = 0; i < results.size(); i++){
a[i] = results[i];
}
sort(a,end);
for (int k=0; k < results.size(); k++){
for (int j = 0; j < results.size(); j++){
if (a[k] == a[j]) counter++;
}
}
if (counter > results.size()) validity = true;
return validity;
}