Using C++, create a dynamically allocated set of 9 inning instances in an old st
ID: 3908683 • Letter: U
Question
Using C++, create a dynamically allocated set of 9 inning instances in an old style array structure. It will ask in turn the away runs, hits, and errors (and then the same for the home team) for each inning. If, after the 9th inning the score is tied, keep on adding new inning instances to the end of your data structure until the score is not tied. Print out the line score (with total run/hits/errors at the end) for that game after the input is completed.
Note a couple things about the design: You need to create the new array every time a new inning past the 9th is needed; it has to be dynamically allocated and the old data copied into it. Make sure to delete any unused dynamic memory.
Explanation / Answer
#include<iostream>
using namespace std;
struct inning{
int aruns;
int hits;
int errors;
int haruns;
int hhits;
int herrors;
};
int main(){
inning *data;
data = new inning[9];
int count = 0;
int hscore;
int tscore;
int max = 9;
while (true){
cout << "Enter run hits errors for the team:";
cin >> data[count].aruns >> data[count].hits >> data[count].errors;
cout << "Enter run hits errors for the home team:";
cin >> data[count].haruns >> data[count].hhits >> data[count].herrors;
hscore = data[count].haruns + data[count].hhits - data[count].herrors;
tscore = data[count].aruns + data[count].hits - data[count].errors;
count ++;
if (count % 9 == 0){
if (hscore != tscore){
if (hscore > tscore){
cout << "Home team is winner ";
}
else {
cout << "Other team is winner ";
}
}
else {
inning *data1;
data1 = new inning[max * 2];
for (int i = 0; i<max; i++){
data1[i].aruns = data[i].aruns;
data1[i].hits = data[i].hits;
data1[i].errors = data[i].errors;
data1[i].haruns = data[i].haruns;
data1[i].hhits = data[i].hhits;
data1[i].herrors = data[i].herrors;
}
data = data1;
}
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.