SUM = 0
REPEAT B TIMES
{
SUM = SUM + (A * A) //add our current square to the total
A = A + C // increase our square length by the increment
}
RETURN SUM
public static double calculateArea(double startLength, int squareCount, double increment)
{
double totalArea = 0;
double length = startLength;
for (int i = 0; i < squareCount; i++, length += increment)
{
totalArea += length * length;
}
return totalArea;
}
Well I was going to edit it and put a more thorough explanation of how it'd be done in but realised the post above had done that.Welshy said:Give him the answer why dont you...
Or even:Inquisitor said:Something along these lines?
public static double calculateArea(double start, double step, int N)
{
return N*(start*(start+step*(N-1))+step*step*(N-1)*(2*N-1)/6);
}
It could still be a part of an assignment or something, I always think pseudo code is far more appropriate if people are only asking for pointers.Inquisitor said:It didn't seem like a "homework question", so I saw no need to cover up the actual answer![]()
Inquisitor said:Something along these lines?
Code:public static double calculateArea(double startLength, int squareCount, double increment) { double totalArea = 0; double length = startLength; for (int i = 0; i < squareCount; i++, length += increment) { totalArea += length * length; } return totalArea; }
Works fine for me when I copy the method into EclipseNazbit said:I have been having a go with that one, cant quite get it to work though. the return totalarea part is showing errors in the compiler at the moment. But I will keep trying.
Fourstar said:Works fine for me when I copy the method into Eclipse![]()