Using pow with arrays

I just completed my first semester of c++ class, and I'm going back and redoing my midterm for shits and giggles (can't sleep).

Well, I'm a bit stuck. The midterm had us use the <cmath> pow operator to take a radius and square it...however I am having some issues using an array / for loop to iterate and work; here's my code: (considerably snipped, just to illustrate the parts that I'm confused about)

const int arrayset=3;
int total,
i=0,
g[arrayset], // Radius
a[arrayset]; // Area

double pi=3.1415;

----- Later

for (i=0; i<arrayset; i++){
a[i]= pi*(pow(g[i],2)); // This line is the problem line!
}


By my logic the line reads as follows:
a[x] = pi times g[x] raised to the second power. The error I'm getting is that I overloaded the operator... which seems a little off.

I have found a (much less desirable) workaround, I can simply use a[i]= pi*(g[i]*g[i]); but that just doesn't feel like I'm doing the right thing.

Thanks.
You did not specify how did you declare g[] as.
By the error, i assume that it was declared a int. Try declaring it as float or double.

The trouble was probably caused because pow does not accept int arguments.

OR

Use static cast.

 
a[i] = pi*(pow(static_cast<float>(g[i]),2));
Damn, you hit the nail on the head, G is indeed an array (as is a/i) . Thank you so much for your help!
For anyone interested, this is the entire code (I had the lamest midterm ever, hahaha);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* Intro to CPP
Midtem Lab */
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std; 

int main()
{
	const int arrayset=3;
int	total,
		i=0,
		g[arrayset],
	    a[arrayset];
	while (i<arrayset){
	cout << "what is the radius of garden " << i+1 << " ?> "<< endl;
	cin >> g[i];
	i++;
	}

	float pi=3.1415;
	for (i=0; i<=arrayset; i++){
		a[i]= pi*(pow(static_cast<float>(g[i]),2));
	}

	cout << "Job name "<< setw(15) << "Radius(ft) " << setw(15) << "Fence Len.(ft)" << setw(20) << "   Garden Size(Sq ft.)\n\n";
	cout << setprecision(1) << setiosflags(ios::showpoint) << setiosflags(ios::fixed);
	for (i=0; i<arrayset; i++){
		cout << "Garden " << i+1 << setw(15) << g[i] << setw(15) << (2*pi*g[i]) << setw(20) <<a[i]<<endl<<endl;
	}

	cin >> pi;
	return 0;
}
Topic archived. No new replies allowed.