Suppose that you have the partial C++ program shown below. Declare pointers of t
ID: 3729423 • Letter: S
Question
Suppose that you have the partial C++ program shown below.
Declare pointers of the appropriate type for all variables.Initialize the pointers to the addresses of the respective variables.Complete the memory map by filling in the contents and address values.Add the pointers to the memory map filling in all three columns. Assume that the first pointer address is 4096.Using pointer notation compute volume and pressure.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main(void)
{
unsigned int age = 21, number = 9;
int height = 8, width = 12, length = 6;
float temperature = 75.2, pressure = 31.5;
double volume = 85.74, energy = 18.5;
string title = “UAH”;
// 1. Declare pointers to the variables shown above
// 2. Initialize the pointers
// 3. Complete the memory map showing the simple variables
VARIABLE
CONTENTS
ADDRESS
age
21
2048
number
height
length
width
temperature
pressure
volume
energy
title
// 4. Complete the memory map by adding in the pointer information.
POINTER
CONTENTS
ADDRESS
ptr_age
2048
4096
// 5. Write C++ statements for the following two equations using pointer notation.
volume = height * width * length;
pressure = number * energy * temperature / volume;
// 5. Show same calculations using pointer notation here
cout << “Volume = “ << volume << endl;
cout << “Pressure = “ << setprecision(3) << pressure << endl;
system(“pause”);
return 0;
}
Explanation / Answer
Please find my answer for Declaration and Initialization: 1, 2 and 5
Please repost for Q2 and 3. this Question is long.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main(void)
{
unsigned int age = 21, number = 9;
int height = 8, width = 12, length = 6;
float temperature = 75.2, pressure = 31.5;
double volume = 85.74, energy = 18.5;
string title = "UAH";
// 1. Declare pointers to the variables shown above
unsigned int* agePtr, *numberPtr;
int* heightPtr, *widthPtr, *lengthPtr;
float* temperaturePtr, *pressurePtr;
double* volumePtr, *energyPtr;
string* titlePtr;
// 2. Initialize the pointers
agePtr = &age;
numberPtr = &number;
heightPtr = &height;
widthPtr = &width;
lengthPtr = &length;
temperaturePtr= &temperature;
pressurePtr = &pressure;
volumePtr = &volume;
energyPtr = &energy;
titlePtr = &title;
// 5. Write C++ statements for the following two equations using pointer notation.
volume = height * width * length;
*volumePtr = *heightPtr * *widthPtr * *lengthPtr;
pressure = number * energy * temperature / volume;
*pressurePtr = *numberPtr * *energyPtr * *temperaturePtr/ *volumePtr;
// 5. Show same calculations using pointer notation here
cout << "Volume = " << volume << endl;
cout << "Pressure = " << setprecision(3) << pressure << endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.