Write C++ Program and Test the following function:
void multiply (float a[ ], int n, float b[]);
The function multiplies the first n elements of a by the corresponding first n elements of b.
For example, if a is the array {2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9} and b is the array
{4.0,–3.0, 2.0,–1.0,0.0, 0.0}, then the call multiply(a,5,b) would transform a into the
array {8.8,–9.9,8.8,–5.5, 0.0,7.7,8.8,9.9}.
#include <iostream>
void multiply (float a[], int n, float b[])
{
for(int i=0; i<n; ++i)
{
a[i] *= b[i];
}
}
int main()
{
float a[] = { 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9 };
float b[] = { 4.0,-3.0, 2.0,-1.0, 0.0, 0.0 };
//Number of elements in arrays
int countA = sizeof(a) / sizeof(a[0]);
int countB = sizeof(b) / sizeof(b[0]);
int n = countB;
multiply(a, n, b);
std::cout << "Result of 'multiply' execution:\n";
for(int i=0; i < countA; ++i)
{
std::cout << a[i] << " ";
}
std::cout << "\n";
return 0;
}
Comments
Leave a comment