One function to read the number of strokes and the number of holes, the create a
ID: 3561670 • Letter: O
Question
One function to read the number of strokes and the number of holes, the create and return a dynamically allocated array of arrays representing a score card (this should be declared as an int ** or a pointer to a pointer.)
One function to read the pars for each hole (between 2 and 5) and a number of strokes for each player on each hole (between 1 and 6) and store the difference into the score card array.
One function to compute the total score and the distribution of scores for a single player according to the following scheme:
Bogey: 1 over par
Even: even with par
Birdie: 1 over par
Eagle: 2 over par
This function should not print anything
One function that displays the total score and number of bogeys, evens, birdies, and eagles for a single player.
All function parameters for these four functions must be pointers.
Your main() should
Declare all necessary local variables.
Use your four functions
Not print anything
Explanation / Answer
#include <iostream>
using namespace std;
int** readGame(int* numPlayers, int* numHoles);
void readScore(int** data, int* numPlayers, int* numHoles);
void calcScore(int** data, int* player, int* numHoles,
int* total, int* bogey, int* even, int* birdie, int* eagle);
void printScore(int* player, int* total, int* bogey, int* even,
int* birdie, int* eagle);
int main()
{
int** data;
int numPlayers;
int numHoles;
int total, bogey, even, birdie, eagle;
int i;
data = readGame(&numPlayers, &numHoles);
readScore(data, &numPlayers, &numHoles);
for (i = 0; i < numPlayers; i++)
{
calcScore(data, &i, &numHoles, &total,
&bogey, &even, &birdie, &eagle);
printScore(&i, &total, &bogey, &even, &birdie, &eagle);
}
return 0;
}
int** readGame(int* numPlayers, int* numHoles)
{
int** data;
int i;
cout << "Enter number of players: ";
cin >> (*numPlayers);
cout << "Enter number of holes: ";
cin >> (*numHoles);
data = (int**)new int*[*numPlayers];
for (i = 0; i < (*numPlayers); i++)
data[i] = new int[*numHoles];
return data;
}
void readScore(int** data, int* numPlayers, int* numHoles)
{
int i, j;
int par, stroke;
for (i = 0; i < (*numHoles); i++)
{
cout << "Enter par for hole " << (i+1) << "(2-5): ";
cin >> par;
for (j = 0; j < (*numPlayers); j++)
{
cout << "Enter number of strokes for player "
<< (j + 1) << " on hole " << (i + 1) << "(1-6): ";
cin >> stroke;
data[j][i] = (stroke - par);
}
}
}
void calcScore(int** data, int* player, int* numHoles,
int* total, int* bogey, int* even, int* birdie, int* eagle)
{
int i;
int sc;
*total = *bogey = *even = *birdie = *eagle = 0;
for (i = 0; i < (*numHoles); i++)
{
sc = data[*player][i];
*total += sc;
if (sc == 1)
*bogey += 1;
else if (sc == 0)
*even += 1;
else if (sc == -1)
*birdie += 1;
else if (sc == -2)
*eagle += 1;
}
}
void printScore(int* player, int* total, int* bogey, int* even,
int* birdie, int* eagle)
{
cout << " Total Score of Player " << (*player + 1)
<< ": " << (*total) << " ";
cout << "Bogey: " << (*bogey) << " ";
cout << "Even: " << (*even) << " ";
cout << "Birdie: " << (*birdie) << " ";
cout << "Eagle: " << (*eagle) << " ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.