# Surprise surprise… Quiz#3 (corrected)

(This post is edited and has the correct and ultimate version of the code at the very end, with a new entry)

Do you feel like you want to burn your computer to ashes each time your code is not compiling because of a simple “;”? Am I the only one, for real?

Yes, today we had a quiz (and I wasn’t there, to make it worse). The quiz consists on making a program that will give you the square and cubic root of any number you plug into it. Pretty easy concept…  if you have read at least to chapter 3 in your book.

I’m presenting this quiz at this hour because I know that I acquired the knowledge required, and to me it’s all that matters. I did not just copied a piece of code from another partner.

After a freaking lot of few mistakes, my program runs correctly, so here it goes, photo and text code:

#include<iostream>

#include <cmath>//this for including the math library for functions
using namespace std; //automatically gives a space after finishing each line
//You have to tell the computer what are the variables(declare them) and assign an operation
double square_root(double number)
{
return sqrt(number);
}
double cubic_ro

ot(double number)
{
return cbrt(number);
}
//Once you’ve declared all variables, you start writing the “visible code” (term made up by myself)
int main ()
{
int number;
double resquare, rescubic; //Name the variables you declared before
cout<<“I will give you the square and cubic root of any positive number you give me:”<<endl; cin>>number;
if(number>0) //here we set the condition a number must accomplish in order to be solved
{
resquare – square_root(number);
cout<<“The square root of that number is ” <<resquare<< cout<<“.”<< endl; //nice and tidy
rescubic – cubic_root(number);
cout<<“The cubic root of that number is ” <<rescubic<< cout<<“.”<< endl;
//if the number is positive, the program ends here
}
else
{ //if the number does not fits the condition, the computer moves automatically over here
cout<<“You have to give me a number bigger than cero in order to get a real product from the square root.”<< endl;
}
}

# ****Corrected version****

So this morning I opened the program again because I wanted to do some modifications, and discovered that I did the whole thing wrong. Yep, I misplaced the whole code and the results the computer was giving me were totally crazy. Way to go, right?

Thus, I decided to do some research and so I did. First of all, I noticed that many of my partners made a mistake; there is cubic root of negative numbers. Reason for that, is because if you multiply a negative number by a negative it goes positive, but if you multiply it again it stays negative. Anyway, after placing things on its right place I finally got this code (I still don’t know why my computer ignores the spaces between the variable and text because I tried several things and none of them worked).

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

double square_root(double x);
double cube_root(double x);

int main()
{
double n1, n2, n3, n4;
cout<<“Write a number and I’ll give you the square root:”<<endl; cin>>n1;
cout<<“Write a number and I’ll give you the cubic root:”<<endl; cin>>n2;
if(n1<0)
{
cout<<“The number you gave me is negative.”<<endl;
cout<<“I will convert your number into its absolute value and give you the answer.”<<endl;
n1=n1*(-1);
n3=sqrt(n1);
}
else
{
n3=sqrt(n1);
n4=cbrt(n2);
}
cout<<“The square root of “;
cout<<n1;

cout<<” is “;

cout<<n3;
cout<<“.”<<endl;
cout<<“The cubic root of ”
cout<<n2;

cout<<” is “;

cout<<n4;
cout<<“.”<<endl;
return 0;
}