getting the square root added

#include <iostream>
#include <cmath>
using namespace std;
int cubeAndSquare( int, int *);
int main (void){
int number = 5, cube, square;
int *ptr;
ptr = &square;



cout<< "The original value of number is "<< number<<endl;
cube = cubeAndSquare(number, ptr);
cout <<"The square of number "<< number<<" is " << *ptr <<endl;

cout <<"The cube of number "<< number<< " is"<<cube<<endl;
return 0;}

int cubeAndSquare(int n, int *square) {
*square= n * n;
return n * n * n;}
Where is the question?
Use [code][/code] tags when posting code
Read http://www.cplusplus.com/forum/articles/1295/
How to add the function to find the square root of a double and get it displayed?
You can use the std double sqrt(double n) function or the double pow (double,double).
In this last case cout<<double pow(4.0,0.5); would produce 2.0.
rgds & hth`s
/*

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cmath>
using namespace std;
int cubeAndSquare( int, int *);

int main (void){
int number = 5, cube, square;
int *ptr;
ptr = &square;
cout<< "The original value of number is "<< number<<endl;
cube = cubeAndSquare(number, ptr);
cout <<"The square of number "<< number<<" is " << *ptr <<endl;
cout <<"The cube of number "<< number<< " is"<<cube<<endl;
cout<<"The squareroot of number "<<sqrt(number)<<endl;
getchar();
return 0;}

int cubeAndSquare(int n, int *square) {
*square= n * n;
return n * n * n;} 

/*
The original value of number is 5
The square of number 5 is 25
The cube of number 5 is125
The squareroot of number 2.23607 <---*/
Last edited on
Thanks for the help but I recieved this error on build:
line 14 c:\users\p\documents\visual studio 2008\projects\square root1\square root1\sqrt1.cpp(15) : error C2668: 'sqrt' : ambiguous call to overloaded function

do i need sqrt = n*n*n*n on the return

getchar();Why do i need getchar();
sqrt() does not take an int as an argument, you need to change this to a double, or float.

getchar(); holds the console open at the end of execution but you should use
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); instead.

[Edit]
Also, you need to include <limits> for that code
Last edited on
#include <iostream>
#include <cmath>
using namespace std;

int cubeAndSquare( int, int *);


int main (void){
int number = 5, cube, square;
int *ptr;
ptr = &square;

cout<< "The original value of number is "<< number<<endl;
cube = cubeAndSquare(number, ptr);

cout <<"The square of number "<< number <<" is " << *ptr <<endl;

cout <<"The cube of number "<< number<< " is "<< cube <<endl;

return 0;}

int cubeAndSquare(int n, int *square) {
*square= n * n;

return n * n * n;}

how do i return the square root of cubeAndSquare with a reference call and the sqrt() function call
I don't completely understand what you mean. Do you mean like

1
2
3
4
squareRoot(int &number) // call by reference
{
     return sqrt(number);
}


???

Remember, number must be a double for sqrt() to work. If you wanted to keep it as int you could technically cast it.
mcleano, I think you typed that too fast.

1
2
3
4
void squareroot(double &number) {
    number = sqrt(number);
    return;
}
Last edited on
how do i return the square root of cubeAndSquare with a reference call and the sqrt() function call

instead of calling the cubeAndSquare () function by name assign its return value to some declared variable.
e.g double x= void squareroot(int n, int * square)
On return x will contain the (double) type value which you want the square root of.
Now call the squareroot function on x.
mcleano, I think you typed that too fast.
1
2
3
4
void squareroot(double &number) {
    number = sqrt(number);
    return;
}


Cheers firedraco :)
Topic archived. No new replies allowed.