arrays: pow and sum

how can i raise each element in an array to it's specific location in the array using the pow function and then sum each raised element for one total figure. For example array[]= {5,4,3} and i want to find the sum of 5^1+4^2+3^3 which should equal 48.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <math.h>
#include <iomanip>
#include <string>
using namespace std;  
int main()
{   
    int sum = 0;

    int array[]={5,4,3};
    int n;
    for(int i = 0; i <= n; i++){
        sum += pow(array[i],n);
    }cout<<sum<<endl;


    return 0;
}
If you change line 13 to sum += pow(array[i], (i + 1)) , you should be able to get what you are looking for
5^1+4^2+3^3 which should equal 48..


P.S. I am not able to compile it, because I am at work.
Got it! I also needed to change i<=n to i<n
Topic archived. No new replies allowed.