In: Computer Science
Create the function named multiplyByAverage used below. The multiplyByAverage function is passed a float array and its size and multiplies each element of the passed array by the array's average. in C++.
const size_t SIZE = 4;
float floatArray[SIZE] = { 42.5, 74.2, 12.4, 24.3 };
multiplyByAverage(floatArray, SIZE);
//floatArray is now: { 1629.9, 2845.6, 475.5, 931.9 }
code and output
code for copying
#include <iostream>
#include<iomanip>
using namespace std;
void multiplyByAverage(float f[],int size)
{
float arr[4];
float sum=0;
float average =0;
for(int i=0;i<4;i++)
{
sum=sum + f[i];
}
average=sum/size;
for(int i=0;i<4;i++)
{
arr[i]=f[i]*average;
cout << fixed<<setprecision(1)<<(arr[i])<<"
";
}
}
int main()
{
const size_t SIZE =4;
float floatArray[SIZE] = { 42.5, 74.2, 12.4, 24.3 };
multiplyByAverage(floatArray,SIZE);
return 0;
}