Using C++, Code two functions to fill an array with the names of every World Series-winning team from 1903 to 2020, then output each World Series winner with the number of times the team won the championship as well as the years they won them. The input file is attached, along with the main function and screenprint. Please note team names that include two words, such as Red Sox, have an underscore in place of the space. This enables you to use the extraction operator with a single string variable. The following resources are included: Here is main. #include #include #include using namespace std; // Add function declarations and documentation here void fill(string teams[], int size); void findWinner(string teams[], int size); int main() { const int SIZE = 118; int lastIndex; string team[SIZE]; fill(team, SIZE); findWinner(team, SIZE); return 0; } // Add function definitions here WorldSeriesChampions.txt Americans No_Series Giants White_Sox Cubs Cubs Pirates Athletics Athletics Red_Sox Athletics Braves Red_Sox Red_Sox White_Sox Red_Sox Reds Indians Giants Giants Yankees Senators Pirates Cardinals Yankees Yankees Athletics Athletics Cardinals Yankees Giants Cardinals Tigers Yankees Yankees Yankees Yankees Reds Yankees Cardinals Yankees Cardinals Tigers Cardinals Yankees Indians Yankees Yankees Yankees Yankees Yankees Giants Dodgers Yankees Braves Yankees Dodgers Pirates Yankees Yankees Dodgers Cardinals Dodgers Orioles Cardinals Tigers Mets Orioles Pirates Athletics Athletics Athletics Reds Reds Yankees Yankees Pirates Phillies Dodgers Cardinals Orioles Tigers Royals Mets Twins Dodgers Athletics Reds Twins Blue_Jays Blue_Jays Strike_cancelled_series Braves Yankees Marlins Yankees Yankees Yankees Diamondbacks Angels Marlins Red_Sox White_Sox Cardinals Red_Sox Phillies Yankees Giants Cardinals Giants Red_Sox Giants Royals Cubs Astros Red_Sox Nationals Dodger
Using C++, Code two functions to fill an array with the names of every World Series-winning team from 1903 to 2020, then output each World Series winner with the number of times the team won the championship as well as the years they won them. The input file is attached, along with the main function and screenprint. Please note team names that include two words, such as Red Sox, have an underscore in place of the space. This enables you to use the extraction operator with a single string variable.
The following resources are included:
Here is main.
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
// Add function declarations and documentation here
void fill(string teams[], int size);
void findWinner(string teams[], int size);
int main()
{
const int SIZE = 118;
int lastIndex;
string team[SIZE];
fill(team, SIZE);
findWinner(team, SIZE);
return 0;
}
// Add function definitions here
WorldSeriesChampions.txt
Americans
No_Series
Giants
White_Sox
Cubs
Cubs
Pirates
Athletics
Athletics
Red_Sox
Athletics
Braves
Red_Sox
Red_Sox
White_Sox
Red_Sox
Reds
Indians
Giants
Giants
Yankees
Senators
Pirates
Cardinals
Yankees
Yankees
Athletics
Athletics
Cardinals
Yankees
Giants
Cardinals
Tigers
Yankees
Yankees
Yankees
Yankees
Reds
Yankees
Cardinals
Yankees
Cardinals
Tigers
Cardinals
Yankees
Indians
Yankees
Yankees
Yankees
Yankees
Yankees
Giants
Dodgers
Yankees
Braves
Yankees
Dodgers
Pirates
Yankees
Yankees
Dodgers
Cardinals
Dodgers
Orioles
Cardinals
Tigers
Mets
Orioles
Pirates
Athletics
Athletics
Athletics
Reds
Reds
Yankees
Yankees
Pirates
Phillies
Dodgers
Cardinals
Orioles
Tigers
Royals
Mets
Twins
Dodgers
Athletics
Reds
Twins
Blue_Jays
Blue_Jays
Strike_cancelled_series
Braves
Yankees
Marlins
Yankees
Yankees
Yankees
Diamondbacks
Angels
Marlins
Red_Sox
White_Sox
Cardinals
Red_Sox
Phillies
Yankees
Giants
Cardinals
Giants
Red_Sox
Giants
Royals
Cubs
Astros
Red_Sox
Nationals
Dodgers
Actually, string is a sequence of characters.
SOURCE CODE
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
// Add function declarations and documentation here
void fill(string teams[], int size);
void findWinner(string teams[], int size);
int main(){
// Total Size of Array Value i.e 1903 to 2020.
const int SIZE = 118;
char ch; // To take choice for continue searching
string team[SIZE]; // String array to hold values team names
fill(team, SIZE); // Function to read names from file.
// Loop to continue searching
do
{
findWinner(team, SIZE); // Search the team winning in which year
cout<<"Run another search?(y/n):";
cin>>ch;
}while(ch!='n' && ch!='N');
}
// Function to read names from file.
//the file name reads line by line optionally editing underscore with a blank space.
void fill(string team[], int SIZE)
{
int i=0;
fstream newfile;
newfile.open("WorldSeriesChampions.txt",ios::in); // perform read operation and open a file using file object
if (newfile.is_open()){ // whether the file is open
string readVal;
while(getline(newfile, readVal)){ //read data from file object and put it into string.
replace( readVal.begin(), readVal.end(), '_', ' ');
team[i++]=readVal; //Copying to array
}
newfile.close(); //close the file.
}
}
// function to find Winner given team name on which years
void findWinner(string team[], int SIZE)
{
int i=0;
string text; // Input to be searched team name
cout<< "Enter team:";
cin>>text;
int flag=0; // Indicate whether a value is found or not
while(i<SIZE) // Looping till Total Elements
{
int x= team[i].compare(text); // Compare whether found in array or not
if(x==0) // Suceess found
{
cout<< text <<" won the World Series in "<< 1903+i<< endl;
flag=1; // Updating to denote atleast one record in array was found
}
i++;
}
if(flag==0)
cout<< text <<" never won a World Series" << endl;
}
Step by step
Solved in 4 steps with 5 images