Need Help for the C++: The explicit memory management capability of C/C++ is one
ID: 3776223 • Letter: N
Question
Need Help for the C++:
The explicit memory management capability of C/C++ is one reason that the language remains popular nearly half a century after its creation. Using dynamic memory requires an understanding of indirection: the idea of manipulating a variable (pointer variable) that refers to another variable. We say that the pointer variable points to the dynamically created memory. In this lab exercise we begin our exploration of dynamic memory management.
Q1.
Momentum is defined as the product of an item’s mass and its velocity. Mass is a scalar value, whereas velocity is generally expressed as a vector quantity with three components. The product of the scalar mass and the velocity yields momentum as a vector quantity.
Write a function named momentum that will accept as arguments a (i) one-dimensional velocity array with three values (type double) (i.e. a 3d vector) and (ii) a mass (type double), and return a dynamically allocated array representing the momentum. Note the momentum is determined by multiplying the scalar mass by each element of the vector array.
Test your momentum function by constructing a short main program that will ask the user to input values for the velocity and mass from the console, and then display the momentum.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Q2. With the code from Q1, write a program to determine the average momentum of a collection of items with random velocities and masses. Do this using the following outline:
1. Construct a function named randVec that will take no arguments and return a dynamically allocated 3-element array of doubles. Each element in the array should be a randomly generated value in the range -100.0 through +100.0.
2. Using randVec and your momentum function from the previous part, generate momentum vectors for 1000 items, each of which has a random velocity (as described above) and a randomly generated mass in the range 1.0 through 10.0. Save the momentum vectors using a suitable array of pointers.
3. Determine and display the average momentum vector of the items using a for loop. [Hint: the average should be done component by component.]
Explanation / Answer
Below is the code for the question. Output is also shown . Please don't forget to rate the answer if it helped. Thank you very much.
answer for Q1.
#include <iostream>
using namespace std;
double* momentum(int mass, double velocity[])
{
double *mm = new double[3]; //allocate a pointer to double to hold 3 elementsß
for (int i = 0; i < 3; ++i)
mm[i] = mass * velocity[i];
return mm;
}
int main()
{
int mass;
double velocity[3];
double *mom ;
cout << "Enter mass: ";
cin >> mass;
cout << "Enter the 3 components of velocity(separated by space): ";
cin >> velocity[0] >> velocity[1] >> velocity[2];
mom = momentum(mass, velocity);
cout<<"The momentum vector is [" << mom[0] << ", " << mom[1] << ", " << mom[2] << "]" << endl;
delete []mom; //free this as its dynamically allocated
}
output
Enter mass: 5
Enter the 3 components of velocity(separated by space): 2.5 3.3 4
The momentum vector is [12.5, 16.5, 20]
answer for Q2:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
double* momentum(int mass, double velocity[])
{
double *mm = new double[3]; //allocate a pointer to double to hold 3 elementsß
for (int i = 0; i < 3; ++i)
mm[i] = mass * velocity[i];
return mm;
}
double* randVec()
{
int MIN_VAL = -100 , RANGE = 201;
double *vel = new double[3];
for(int i = 0; i < 3 ; ++i)
{
vel[i] = MIN_VAL + rand() % RANGE; //generate random number in the range -100 to 100 both inclusive
}
return vel;
}
int main()
{
int mass;
double *velocity;
double *mom[1000];
srand(time(0));
for(int i = 0 ; i < 1000; ++i)
{
velocity = randVec();
mass = 1 + rand() % 10;//generate mass in range 1-10 both inclusive
mom[i] = momentum(mass, velocity);
delete []velocity; //deallocate
}
//calculate the average of momentum
double avg[3]={0};
//first calcualate total
for(int i = 0; i < 1000; ++i)
{
for(int j = 0; j < 3; ++j)
avg[j] += mom[i][j];
}
for(int i = 0; i < 3; ++i)
avg[i] /= 1000; //divide the total by number of elements to get average
cout << "The average momentum is [" << avg[0] << ", " << avg[1] << ", " << avg[2] << " ]" <<endl;
}
output
The average momentum is [-4.011, 24.448, 10.938 ]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.