answersLogoWhite

0


Best Answer

/**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

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

Use a template function to calculate the square of any value, then sum the squares. The following example demonstrates how to sum the squares of integers 3 and 5.

#include<iostream>

template <class T>

T sq(T& value)

{

return( value*value );

}

int main()

{

#using namespace std;

int x=3, y=5;

cout<<"The sum of "<<x<<" squared plus "<<y<<" squared is "

<<sq(x)+sq(y)<<endl;

return(0);

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

#include<iostream>

template<typename T>

struct foo

{

T data;

foo (const T num): data (num) {}

T square() const { return data * data; }

T cube() const { return data * square(); }

};

int main()

{

foo<int> a {3};

foo<double> b {3.14};

std::cout << a.data << " squared = " << a.square() << " and cubed = " << a.cube() << std::endl;

std::cout << b.data << " squared = " << b.square() << " and cubed = " << b.cube() << std::endl;

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

#include <iostream>

#include <cmath>

using namespace std;

int main()

{

int x;

cout << "Enter a number: ";

cin >> x;

cout << "The squareroot of " << x << " is " << sqrt(x) << endl;

char wait;

cin >> wait;

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

#include <iostream>

using namespace std;

#include <stdlib.h>

int main()

{

double a;

double b;

double c;

cout << "Enter number for a: ";

cin >> a;

cout << "Enter number for b: ";

cin >> b;

cout << "Enter number for c: ";

cin >> c;

if (a 0)

{

cerr << "0 is an invalid input" << endl;

}

else

{

double root_1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);

double root_2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);

cout << "Root 1: " << root_1 << endl;

cout << "Root 2: " << root_2 << endl << endl;

}

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

#include <iostream>

#include <cmath>

using namespace std;

int main()

{

int num = 0;

cout << "Enter a number: ";

cin >> num;

if(num % 2 != 0)

{

cout << "Number is odd and the squareroot is: " << sqrt(num);

}

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

#include

#include

#include

int main()

{

using std::string;

using std::cin;

using std::cout;

using std::endl;

using std::sqrt;

using std::getline;

using std::stringstream;

double input;

string str = "";

while (true)

{

cout << "Enter a positive decimal number: ";

getline (cin, str);

stringstream ss (str);

if (ss >> input && input > 0)

break;

cout << "Invalid input, please try again..." << endl;

}

double squared = input * input;

double square_root = sqrt (input);

cout << input << " squared is " << squared << endl;

cout << "The square root of " << input << " is " << square_root << endl;

}

Example output

Enter a positive decimal number: 1.234

1.234 squared is 1.52276

The square root of 1.234 is 1.11086

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

That's what function sqrt is good for, prototype in math.h.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a c plus plus program to calculate the sum of squares using a function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a program to calculate the area of a cylinder using a function?

give an example of calculation of mathematics


Write a C program to calculate the sum of squares of numbers from 1 to N?

#include &lt;iostream&gt; using namespace std; int main() { int i,sum; // variables sum = 0; // initialize sum /* recursive addition of squares */ for (i = 1; i &lt;= 30; i++) sum = sum + (i * i); cout &lt;&lt; sum &lt;&lt;" is the sum of the first 30 squares." &lt;&lt; endl; return 0; }


How do you write a c program to calculate income tax using a function?

float income_tax (float income, float tax_percent) { return income * tax_percent / 100; }


What is the use of function in c?

using function we can call the function in the program any where. by using functions we can reduce the code redundancy


How to calculate cubic function using point of inflection and extrema?

cubic function cubic function


How do you calculate sine value without using math function?

Using its Taylor-series.


Write a program that read phrase and print the number of lower-case letter in it using function of counting?

write a program that reads a phrase and prints the number of lowercase latters in it using a function for counting? in C program


Write a c program for matrix addition using function?

#include&lt;


How can you use Excel to calculate the remainder of a division question?

By using the =MOD function.


How do I calculate compound average growth rate using excel?

use the rate function


What is the importance of using functions in a c program?

C programs do not function without functions.


A c program to call a function without using function name?

It can be done via its address, for example: void function (void (*callback)(void)) { (*callback)(); }