Okay, here's the challenge, I know its probably pretty easy, but i'm just starting to learn and I have the challenge problem that I can't figure out, any help/guidance would be appreciated!

This program will accept two numbers from the user and display the sum of the square roots of the numbers. It will do this by calling a function called "mySquareRoot", which you will write. If the number passed to the function is negative, the function will return the negative of the square root of the positive number. The program will stop when the user enters a zero for either number. The "main" function is already written; you just need to write the "mySquareRoot" function and make any other changes necessary to get the program to work properly. You should use
the "sqrt" function in "cmath". To save yourself time, copy
the code below into your project.

// ******************** I M P O R T A N T ! ! ! *************
// Do not change ANYTHING in main!!
// *****************************************************
int main()
{
    double x, y; 
       do
       {
        cout << "Please enter the first number: ";
        cin >> x;
        if (x != 0)
	{
	double answer1 = mySquareRootFunction(x);
	cout << "The first square root is " << answer1 << endl;
	cout << "Please enter the second number: ";
	cin >> y;
          if (y != 0)
           {
           double answer2 = mySquareRootFunction(y);
           cout << "The second square root is " << answer2 << endl;    
           cout << "The sum of " << answer1 << " and " << answer2;
           cout << " is " << (answer1+answer2) << endl;
	}
	}
	} while ((x != 0) && (y != 0));

}

Thanks!

> You just copied your homework assignment and pasted it into this thread, so what's your question ?

slore>If the number passed to the function is negative, the function will return the negative of the square root of the positive number.
>>>>>> Actually that's not true math :P

> Please post using code tags !

> You can take a look here, to see how the sqrt function works ...

My question is how to do the assignment

My question is how to do the assignment

> That's not a specific question, we aren't doing your homework, sure we can, but you don't have to pass with our work, we don't have to get your grade :P

> Please ask specific questions about what you don't understand ...

> You're already on the right way, your teacher gave you already a bunch of code you don't have to write yourself, just look up how to use the sqrt function and all what you have to do then is wrap a single function around it :) ...

edit> I hope this is not another please-do-my-homework request, if it is, you definitly didn't read the forum rules !

Its not a please do my homework request, its a please point me in the right direction.

Here's what I did so far, but its def not right and i'm not sure what i need to do. This part goes above the "main":

#include <iostream>
#include <cmath>
using namespace std;

void mySquareRootFunction (double x)
{
double z = sqrt(x);

}


I've done very well in this, this is just our last assignment and the teacher made it extra hard, at least I think it is.

> First: Please post using code tags, I'm not saying this a third time :) ...

> void mySquareRootFunction (double x) , you declared your function to return a value of type void , why did you do that? Your function has to return something from type double ...

> double z = sqrt(x); Before you take the square root from a number you should first check whether it's negative :) ...

A quick revision:

> Make the function returning a variable of type double (just replace the void keyword by double ) ...

> You'll have to return variable z 's value, you can do this using the instruction return , e.g.: return z; > Check the number before you calculate the square root of it, if the number is negative you have to make it positive and then you'll have to calculate the square root of it, you can do this as follows:

>> First you check whether the number is positive or negative (you store this in a bool variable called negative , if the number passed to the function is negative you set the boolean variable negative to true, (in the declaration you set it to false by default), you take the absolute value of the number where you want to calculate the square root of and at the end of the function (just before it's returning it's value, you check whether you'll have to return the negative/positive of the square root (use the bool variable negative !)

I hope this explanation helps you to solve this problem :) !

Thanks!! This helps! I'm going to play around with this and see if I can get it to work.

Thanks again!

> If you've other questions (regarding this assignment) please feel free to ask !

The logic behind the problem(which is described) is rubbish.

If the user entered number is negative, the problem is should be dealt with the concept of Complex Numbers.

I think you have misunderstood the problem. 'Cause no fool will give an assignment to sum the squareroot of two negative numbers and ask you to omit the negative sign while summing it.

The logic behind the problem(which is described) is rubbish.

If the user entered number is negative, the problem is should be dealt with the concept of Complex Numbers.

I think you have misunderstood the problem. 'Cause no fool will give an assignment to sum the squareroot of two negative numbers and ask you to omit the negative sign while summing it.

> I know but that's his assignment, probably because the teacher wants him to use a control structure like the if-statement ...

I know it's late and you probably won't get this til the morning, but here's what I got so far. I know i'm close, just messing something up with the abs. Should I be using fabs? Or do I need an If true statement or maybe z2 = abs(x);? I tried all these so I know they are not right, but maybe a slight change is needed to one of them...

#include <iostream>
#include <cmath>
using namespace std;

double mySquareRootFunction (double x)
{
bool negitive = false;
 

double z = sqrt(x);
negitive = true;

if (negitive == false)
{
	z = abs(x);
	negitive = true;
}

return z;

}

I Know I'm so close, but I can't figure out what I'm doing wrong. Its something with the abs. Should I be using fabs instead? or do i need to make another variable such as z2 = abs(x);? I 've tried both these so i'm thinkin its something small i'm missing. Please advise

Thank you!

#include <iostream>
#include <cmath>
using namespace std;

double mySquareRootFunction (double x)
{
bool negitive = false;
 

double z = sqrt(x);
negitive = true;

if (negitive == false)
{
	z = abs(x);
	negitive = true;
}


return z;

}

Sorry for the double post, didn't realize there was a second page on this thread and thought maybe i forgot to hit submit last night.

> if (negitive == false) you can also just write if (!negitive) > The following code is not fully correct:

double z = sqrt(x);
negitive = true;

if (negitive == false)
{
	z = abs(x);
	negitive = true;
}

>> You first have to check whether the number is positive or negative, before calculating the square root of it:

>>> So put the following code in your function before double z = sqrt(x);

if(x<0)
{
     // It's a negative number
     x = -x; // Turn the negative number into a positive one
     negitive = true;
}

>> Before you calculate the square root of a number you should ensure yourself that it isn't negative, so double z = sqrt(x); becomes double z = sqrt(abs(x)); >> At the end of the function you just put the following instruction: return negitive?-z:z;

new question, very similar though.

This program will accept two numbers from the user
and display the sum of the rounded numbers. It will
do this by calling a function called "myRounder",
which you will write. If the number passed to the
function is negative, round the number down (further
from zero); if the number is positive, round the
number up. The program will stop
when the user enters a zero for either number. The
"main" function is already written; you just need to
write the "myRounder" function and make any other
changes necessary to get the program to work properly.

so far i've got:

int myRounder(int x)
{
int z = x;
}

// ******************** I M P O R T A N T ! ! ! ***********************
// Do not change ANYTHING in main or you will get a zero
// ********************************************************************
int main()
{
double x, y;
do
{
cout << "Please enter the first number: ";
cin >> x;
if (x != 0)
{
int answer1 = myRounder(x);
cout << "The first rounded number is " << answer1 << endl;
cout << "Please enter the second number: ";
cin >> y;
if (y != 0)
{
int answer2 = myRounder(y);
cout << "The second rounded number is " << answer2 << endl;
cout << "The sum of " << answer1 << " and " << answer2;
cout << " is " << (answer1+answer2) << endl;
}
}
} while ((x != 0) && (y != 0));
}

i know int z = x; isn't right, but i'm not sure how to do the rounding part.

Nevermind I figured out what I needed to do one this new one. BTW Thank you very much for your help explaining the first problem! I appreciate it!

commented: Well, I'm glad you could do it on your own :) +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.