Quiz week 3

--Originally published at The Clueless Programmer

This post has the answer to the problem we have to do for the quiz, which asked us to make a couple of functions that would give the square and the cube root of a number given by the user. So if you are struggling with the problem in class right now (I see a bunch of you) you can just copy mine.

imagen1

 

#include <iostream>
#include <cmath>
using namespace std;
double square_root (double x, double square)
{
square=sqrt(x);
cout<<“The square root of “<<x<<” is “<<square<<endl;
}
double cube_root (double x, double cube)
{
cube=cbrt(x);
cout<<“The cube root of “<<x<<” is “<<cube<<endl;
}
int main()
{
double num, raiz=0, cubica=0;
cout<<“Write the number you want to know the square and the cube root of: “;
cin>>num;
if (num>=0)
{
square_root(num, raiz);
cube_root(num, cubica);
}
else
{
cout<<“That number has imaginary square roots”<<endl;
cube_root(num, cubica);
}
}

There you go. I have to give an explanation but I still don´t quite know what I did, so I´ll do my best:

I include the libraries (the math one has the roots), then I create the functions, I tell them what I want them to print and then I start the program (If you think my function explanation is vague it is because I don´t know how to explain it). You can make you function in a different way, mine is just an example. And then the program should be fairly easy by now. I just add an “if” so if they write a negative number it tell them that it does not have a square root, but it does have a cube root.

So yeah that´s it, thanks for reading, have fun.