C++ help Please. Complete this function that calculates the N-powers of the numb
ID: 3805984 • Letter: C
Question
C++ help Please.
Complete this function that calculates the N-powers of the numbers in an array "numbers" and put the results in the same numbers array. N is given as the exponent parameter. Function returns the sum of all the results. Assume that "cmath" is already included. Use only one loop in the function
In main()
This is given to you as a sample, do not write it:
double arr [5] = { 1.2, 3.4 , 5.6, 12.8, 1.7 };
double result = getPowers(arr, 3, 5); // result contains the sum of the result
array
// arr array will contain the cubic powers (power 3) of the numbers
Function definition:
double getPowers(double numbers[ ], int exponent, int size) {
//……………….
}
Explanation / Answer
#include<iostream>
#include<cmath>
using namespace std;
double getPowers(double numbers[ ], int exponent, int size) {
double sum = 0;
for(int i=0; i<size; i++) {
sum = sum + pow(numbers[i], exponent);
}
return sum;
}
int main(){
double arr [5] = { 1.2, 3.4 , 5.6, 12.8, 1.7 };
double result = getPowers(arr, 3, 5);
cout<<"Result: "<<result<<endl;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Result: 2318.71
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.