How do you write a c plus plus program to calculate the sum of squares using a function?

1 answer

Answer

1187617

2026-05-14 02:35

+ Follow

/**recursive function to find square root of a double to a precision of

maxDepth = 10 decimal places

returns -1.0 when given a negative number*/

#define maxDepth 10

/*firstly find the rational part*/

double getSqrt(double numIn){

/*cant take the square root of a negitive,

and a square root should not be negative

so return -1*/

int candidate = 0;

if (numIn <0)

return -1.0;

/*try every integer until you get one whose square is higher than

the number required, and this clearly one more than the

integer part of your square root*/

do{

if (candidate *candidate *1.0 numIn)

return testVal;

if (testVal * testVal >numIn)

/*most square roots are irrational, and theirfore a maximum number of recursions

must be set, otherwise infinite recursion will occur*/

if (myDepth <maxDepth)

return getIrrational(numIn, testVal-1/10^myDepth, myDepth +1);

else

return testVal-1/10^myDepth;

}

//this can probably be improved on in terms of conciseness, but the logic is the only //way to find a sqrt without going into calculous

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.