Please write this in c++. Some number of teams are participating in a race. You
ID: 3668995 • Letter: P
Question
Please write this in c++.
Some number of teams are participating in a race. You are not told how many teams are participating but you do know that: Each team has a name, which is one of the uppercase letters A-Z. No two teams have the same name, so there are a maximum number of 26 teams. Each team has the same number of members. No two runners cross the finish line at the same time - i.e. there are no ties. At the end of the race we can write the results as a string of characters indicating the order in which runners crossed the finish line. For example: ZZAZAA We can see there were two teams: A and Z. Team A's runners finished in 3^rd, 5^th and 6^th place. Team Z's runners finished in 1^st, 2^nd and 4^th place. Scoring the race Each runner is assigned a score equal to their finishing place. In the example above team Z's runners achieved scores of 1, 2 and 4. Team A's runners scores were 3, 5, and 6 respectively. The team's score is the sum of the members score divided by the number of people on the team. So team A's score is (3+5+6)/3 = 14/3=4.66 and team Z's score is (l+2+4)/3=7/3=2.33 The Problem Write a program to score races as described above. Input Your program will ask the user to input a string of uppercase characters indicating the outcome of a race. Output Your program will output: The number of teams. The number of runners on a team. The names of the teams - in alphabetical order - together with the team score. The name and score of the winning team. So for the example above the program will print: There are 2 teams. Each team has 3 runners. The winning team is team Z with a score of 2.33. Your program should loop asking for input processing the data until the user Your program should detect the error condition where teams do not have the that case it should print an error message, and resume by requesting the next input.Explanation / Answer
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
string input;
int NumOfLetters;
void findWinner(double score[],int size,char team[]){
double min=score[0];
int winningTeam=0;
for(int i=0;i<size;i++)
if(min>score[i]){
min=score[i];
winningTeam=i;
}
cout<<"The winning team is team "<<team[winningTeam]<<" with a score of "<<min<<". ";
}
void Sort(char *array, int size){ //bubble sort
char temp;
for(int i=0;i<size;i++)
for(int j=0;j<size-1;j++)
if(array[j]>array[i]){
temp = array[i];
array[i]=array[j];
array[j]=temp;
}
}
//check if everything is uppercase;
bool checkUpper(string input){
for(int i=0;i<input.length();i++)
if(!isupper(input[i])) return false;
return true;
}
//check to see if there are the same number of runners for each team;
//It does the checking and everything else!
bool checkTeam(string input){
char* Letters= new char[input.length()]();//worst case, all letters are used once;
NumOfLetters=0;
bool flag=true;
for(int i=0; i<input.length();i++){ //get same letters once to an array;
for(int j=0;j<NumOfLetters;j++){
if(Letters[j]==input[i]){
flag=false;
break;
}
else flag=true;
}
if(flag){
Letters[NumOfLetters]=input[i];
NumOfLetters++;
}
}
int *CountLetters= new int[NumOfLetters]();
double *Scores=new double[NumOfLetters]();
for(int i=0; i<NumOfLetters;i++){
for(int j=0;j<input.length();j++)
if(Letters[i]==input[j])CountLetters[i]++;
}
for(int i=0; i<NumOfLetters;i++){ //check to see if there are equal number of letters;
if(CountLetters[i]!=CountLetters[0]){
delete[] Scores;
delete[] Letters;
delete[] CountLetters;
return false;
}
}
Sort(Letters,NumOfLetters);
for(int i=0;i<input.length();i++)
for(int j=0;j<NumOfLetters;j++)
if(input[i]==Letters[j])Scores[j]+=i+1;
cout<<" There are "<<NumOfLetters<<" teams. ";
cout<<"Each team has "<<CountLetters[0]<<" runners. ";
cout<<"Team Score ";
for(int i=0;i<NumOfLetters;i++)
cout<<Letters[i]<<" "<<(Scores[i]=Scores[i]/CountLetters[0])<<endl;
findWinner(Scores,NumOfLetters,Letters);
delete[] Scores;
delete[] Letters;
delete[] CountLetters;
return true;
}
int main(){
while(true){
cout<<" Enter a string of uppercase characters to indicate places(enter done to terminate):";
cin>>input;
if(input=="done") break;
if(!checkUpper(input)){
cout<<"Uppercase letters ONLY! try again. ";
continue;
}
if(!checkTeam(input)){ //checks and do all the rest;
cout<<"Each team must have the same number of runners! try again. ";
}
}//while
return 0;
}
sample output
Enter a string of uppercase characters to indicate places(enter done to terminate):A Z
There are 1 teams.
Each team has 1 runners.
Team Score
A 1
The winning team is team A with a score of 1.
Enter a string of uppercase characters to indicate places(enter done to terminate):
There are 1 teams.
Team Score
A 1
The winning team is team A with a score of 1.
Enter a string of uppercase characters to indicate places(enter done to terminate):
There are 1 teams.
Each team has 1 runners.
Team Score
Z 1
The winning team is team Z with a score of 1.
Enter a string of uppercase characters to indicate places(enter done to terminate):