1) Declare a double pointer. Allocate new memory for it and assign it a value of
ID: 3749889 • Letter: 1
Question
1) Declare a double pointer. Allocate new memory for it and assign it a value of 36.7. Remember, you are assigning a value to the memory location, don't assign 36.7 as the address!
2) Complete the following program as instructed in the comments
int kidsAge=11, *papaAge, *familyAges[2];
//a) dynamically allocate an int and assign the location to papaAge
papaAge= __________________________________________;
//b) Assign 35 to papaAge (the value 35, NOT the memory location 35!)
___________________ = 35;
//c) Assign element 0 of familyAges to papaAge
____________________________ = papaAge;
//d) Assign element 1 of familyAges to address of kidsAge
__________________________________________________;
//e) Write code to efficiently add 5 to every element of familyAges (that is, the value of kidsAge should be 16, and papaAge 40.
_____________________________________________
_____________________________________________
_____________________________________________
_____________________________________________
3) Make a proper function call using only the variables defined below as arguments. Note that you may need to modify the variables with the operators used in processing pointers, but you must use one of the 3 variables listed below. Also note that you should not try to guess at the implementation of the functions, all you are doing is calling the function properly.
int i=6;
double *doub = new double;
char* exclaim="I've got game!";
a) void yourGame(char );
b) void myScore(double *, int);
c) void myGoodness(char *);
d) void earlyTime(int);
e) void lateTime(int*, char*, double, double* );
f) void e(double);
4) Create the following class. You will NOT need setters and getters for this. There will however be 1 constructor as indicated.
class BallGame
{
public:
string homeTeam;
string awayTeam;
int homeScore;
int awayScore;
BallGame(string hmTeam, string awTeam, int hmSc, int awSc);
bool didHomeTeamWin();
};
You must implement the constructor and didHomeTeamWin() method. The method must return whether the home team has the larger score. (Note, a tie is not a win)
In main:
Allocate memory for three separate BallGame instances and point to that memory with 3 separate pointer variables, game1, game2, and game3. Make each pointer represent the following data:
"Mystics", "Fire", 100,92
"Capitals", "Lightning", 2,3
"Ravens", "Colts",35,35
5)
Write a FUNCTION, NOT a method that will accept a pointer to a BallGame instance as shown in number 4.
The function must return void and output a message accoding to the parameter.
if the parameter's didHomeTeamWin() returns true, output the message:
Home team won against away team
If the parameter's didHomeTeamWin() returns false, output:
Home team lost against away team
where home team and away team are the values held in the instance.
The function must be named showResults.
6) In main, call the showResults() function for game1, game2, and game3 one at a time like:
showResults(game1);
showResults(game2);
showResults(game3);
Deallocate memore from game1, game2, game3
See template below:
#include<iostream
using namespace std;
//Create your class
class BallGame
{
public:
string homeTeam;
string awayTeam;
int homeScore;
int awayScore;
//You must implement the constructor and the method.
//You may implement inline
BallGame(string ht, string at, int hmSc, int awSc);
bool didHomeTeamWin();
};
//You can implement constructor and method here if you like
//Implement showResults() function here.
int main()
{
//Declare and instantiate game1, game2, and game3
//Call the function once's for each BallGame instance
showResults(game1);
showResults(game2);
showResults(game3);
//deallocate the memory for game1, game2, game3
return 0;
}
Explanation / Answer
Answer 1)
double *dptr = new double;
*dptr = 36.7;
======================================================================
Answer 2)
int kidsAge=11, *papaAge, *familyAges[2];
//a) dynamically allocate an int and assign the location to papaAge
papaAge= new int;
//b) Assign 35 to papaAge (the value 35, NOT the memory location 35!)
*papaAge = 35;
//c) Assign element 0 of familyAges to papaAge
familyAges[0] = papaAge;
//d) Assign element 1 of familyAges to address of kidsAge
familyAges[1] = &kidsAge;
//e) Write code to efficiently add 5 to every element of familyAges (that is, the value of kidsAge should be 16, and papaAge 40.
for(int i =0 ; i < 2; i++)
*familyAges[i] = *familyAges[i] + 5;
======================================================================
Answer 3)
int i=6;
double *doub = new double;
char* exclaim="I've got game!";
a) void yourGame(char );
yourGame(*exclaim);
b) void myScore(double *, int);
myScore(doub, i);
c) void myGoodness(char *);
myGoodness(exclaim);
d) void earlyTime(int);
earlyTime(i);
e) void lateTime(int*, char*, double, double* );
lateTime(&i, exclaim, *doub, doub);
f) void e(double);
e(*doub);
======================================================================
Answer 4)
class BallGame
{
public:
string homeTeam;
string awayTeam;
int homeScore;
int awayScore;
BallGame(string hmTeam, string awTeam, int hmSc, int awSc)
{
homeTeam = hmTeam;
awayTeam = awTeam;
homeScore = hmSc;
awayScore = awSc;
}
bool didHomeTeamWin()
{
if(homeScore > awayScore)
return true;
else
return false;
}
};
int main()
{
BallGame *game1 = new BallGame("Mystics", "Fire", 100,92);
BallGame *game2 = new BallGame("Capitals", "Lightning", 2,3);
BallGame *game3 = new BallGame("Ravens", "Colts",35,35);
}
======================================================================
Answer 5)
void showResults(BallGame *game)
{
if(game->didHomeTeamWin())
cout << game->homeTeam << " won against " << game->awayTeam << endl;
else
cout << game->homeTeam << " lost against " << game->awayTeam << endl;
}
======================================================================
Answer 6)
#include<iostream
using namespace std;
//Create your class
class BallGame
{
public:
string homeTeam;
string awayTeam;
int homeScore;
int awayScore;
//You must implement the constructor and the method.
//You may implement inline
BallGame(string hmTeam, string awTeam, int hmSc, int awSc)
{
homeTeam = hmTeam;
awayTeam = awTeam;
homeScore = hmSc;
awayScore = awSc;
}
bool didHomeTeamWin()
{
if(homeScore > awayScore)
return true;
else
return false;
}
};
//Implement showResults() function here.
void showResults(BallGame *game)
{
if(game->didHomeTeamWin())
cout << game->homeTeam << " won against " << game->awayTeam << endl;
else
cout << game->homeTeam << " lost against " << game->awayTeam << endl;
}
int main()
{
//Declare and instantiate game1, game2, and game3
BallGame *game1 = new BallGame("Mystics", "Fire", 100,92);
BallGame *game2 = new BallGame("Capitals", "Lightning", 2,3);
BallGame *game3 = new BallGame("Ravens", "Colts",35,35);
//Call the function once's for each BallGame instance
showResults(game1);
showResults(game2);
showResults(game3);
//deallocate the memory for game1, game2, game3
delete game1;
delete game2;
delete game3;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.