I finished the quiz in time!!

Well, maybe I took me more time because of writing the post, but!!! I did it! I’m proud of myself!

We had to do a program with two specific functions:

  • double square_root(double x) {} ; // returns the square root of x
  • double cube_root(double x) {} ; // returns the cube root of x

The program needs to ask what number do you want to be x and the program will respond with it’s square root and it’s cube root.

For this, we have to add a function to the program and that was the challenge, at least for me, because I didn’t knew how does a function works and even less how to add it to the code. That is why I ask for help to my classmate Sergio. He was very helpful, thank you Sergio!!

This is what I did:

quiz3

and here is outside the photo:

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

double square_root (double);
double cube_root (double);
int main () {
double x, s, c;
cout << “I will give you the square and cube root of x. What is x? “;
cin >> x;
s = square_root (x);
c = cube_root (x);
cout << “Square root: ” << s << endl;
cout << “Cube root: ” << c;
return 0;
}
double square_root (double x) {
return sqrt (x);
}
double cube_root (double x) {
return cbrt (x);
}

So, the first thing I always do is insert the <iostream> and the namspace std. This time I added the library of the math functions which is <cmath>, because we will need it to solve the square and cube roots. I also add the values of them: double square_root (double);
double cube_root (double). What I understood was that we need to add this so the program recognizes that  s = square_root (x) needs to go for the function that is out of the int main or below. We also need to add the functions (the ones at the end):  double square_root (double x) { return sqrt (x); }  and the one of the cube root, because with this the program will know that when it reaches s = square_root (x) it will go out of the main to the functions and you will notice that the functions also has return in it, which means that when it has the answer it will go back to the main to continue the process.

This was the result of the program:

I will give you the square and cube root of x. What is x?  7
Square root: 2.64575
Cube root: 1.91293

I hope my explanation helped you and was understandable, anyway, if you have some question, don’t hesitate and ask.

 

Leave a comment