Modify this lab Code create an array of players 2 or 3 and then put it in a loop
ID: 3570418 • Letter: M
Question
Modify this lab
Code create an array of players 2 or 3 and then put it in a loop and get each players name and average and print it in a loop.
#include <iostream>
#include <cmath>
using namespace std;
struct Player
{
char Name[50];
double average;
};
void printPlayerAverage(Player p);
void getPlayerAverage(Player * plr);
int main()
{
Player p, p1;
getPlayerAverage(&p);
p1 = p;
printPlayerAverage(p1);
return 0;
}
void getPlayerAverage(Player * plr)
{
cout << "Enter the players name" << endl;
cin >> plr->Name;
cout << "Enter the players average " << endl;
cin >> plr->average;
}
void printPlayerAverage(Player p)
{
cout << p.Name << " is hitting " << p.average << endl;
}
#include <iostream>
#include <cmath>
using namespace std;
struct Player
{
char Name[50];
double average;
};
void printPlayerAverage(Player p);
void getPlayerAverage(Player * plr);
int main()
{
Player p, p1;
getPlayerAverage(&p);
p1 = p;
printPlayerAverage(p1);
return 0;
}
void getPlayerAverage(Player * plr)
{
cout << "Enter the players name" << endl;
cin >> plr->Name;
cout << "Enter the players average " << endl;
cin >> plr->average;
}
void printPlayerAverage(Player p)
{
cout << p.Name << " is hitting " << p.average << endl;
}
Explanation / Answer
#include <iostream>
#include <cmath>
using namespace std;
struct Player
{
char Name[50];
double average;
};
void printPlayerAverage(Player p[], int size);
void getPlayerAverage(Player p[], int size);
int main()
{
Player p[3], p1;
getPlayerAverage(p, 3);
//p1 = p;
printPlayerAverage(p,3);
return 0;
}
void getPlayerAverage(Player p[], int size)
{
for(int i=0; i<size;i++){
cout << "Enter the players name" << endl;
cin >> p[i].Name;
cout << "Enter the players average " << endl;
cin >> p[i].average;
}
}
void printPlayerAverage(Player p[], int size)
{
for(int i=0; i<size;i++){
cout << p[i].Name << " is hitting " << p[i].average << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.