Code in c++ please! Momentum is defined as the product of an item’s mass and its
ID: 3690454 • Letter: C
Question
Code in c++ please!
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.
Explanation / Answer
#include<iostream.h>
#include<stdlib.h>
double momentum(double velocity[], int n, double mass)
{
double m=0;
for(int i=0;i<n;i++)
{
m=m+velocity[i]*mass;
}
return m;
}
void main()
{
double v[3], mass,m;
for(int i=0;i<3;i++)
{
cout<<"Enter velocity";
cin>>v[i];
}
cout<<"Enter mass";
cin>>mass;
m=momentum(v, 3, mass);
cout<<"Momentum is "<<m<<endl;
system("pause");
}