Week 3 quiz, the one about functions

--Originally published at World's Fanciest Cursors

800px-computer_mouse_trap
Mousetrap? by Flickr user mllerustad. Shared under a CreativeCommons (BY) license.

Today’s quiz involved a bit of learning.  Besides creating and executing functions we neede to check for correct user input.

The first thing checked is whether the input is actually a number and not some other kind of symbol. To do that I used some code I found that checks for input call failures here. An input call failure usually happens when you try to enter the wrong type of data to a variable, like trying to enter a letter into an “int” variable. This code checks for such a failure and evaluates to true if it exists. We can use this flag to alter the flow of execution to a loop that clears the error, skips the problem data and asks again for input.

The second thing to check is for negative numbers. As we know, negative numbers cannot be used in root operations as that would result in an imaginary number. We need to make sure the user enters positive values. This can be accomplished with a simple if statement that evaluates if the input is greater or equal to zero. If that is false we can set a loop to ask again for input over and over again until an appropriate answer is given. Learn more about operators here.

It works!:

working

Here, enjoy some spaghetti-code:

#include <iostream>
#include <cmath>

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

int main ()
{
  double a;
  std::cout << "Give me a positive number and I'll get its square and cubic roots.\nEnter your number:" << std::endl;   while(1)   {     std::cin >> a;
    while(1) // Loop that cheacks for wrong input types
    {
      if (std::cin.fail()) // Detects a failure of the input function
      {
        std::cin.clear(); // Clears the error
        std::cin.ignore(); // 
20160917_143539
Continue reading "Week 3 quiz, the one about functions"

Cómo quiere su raíz

--Originally published at Adal´s Blog

La actividad dice así:


Para poder usar este programa necesitamos acudir a expresiones matemáticas alternas, como por ejemplo, expresar la raíz cuadrada en forma de potencia y utilizar nuevas librerías (en este caso <cmath>)

Y así es como queda el código:


Pagina de ayuda;

Quiz 03

--Originally published at Programming the city

Today I did the quiz number 3, it was a little bit difficult at the begining but with the help of my classmates and with teacher, it became easier.

First I added the two libraries, one of these is necessary for you to do the square root of a number and also all math operations.

Secondly , I added the functions of the square and the cube root with the commands I will show you.

And finally there is the whole code.

———————————————————-

#include <iostream>
#include <math.h>

using namespace std;
double square_root(double num) {
num=sqrt(num);
return num;
} ;
double cube_root(double num) {
num=cbrt(num);
return num;
} ;
int main()
{
double num;

cout<<“Give me a number, please “;
cin>>num;
if(num>=0){
cout<<“The square root of “<<num<<” is “<<square_root(num)<<endl;
}
else{
cout<<“You can’t get the square root of a number if it is negative, sorry”<<endl;
}

cout<<“The cube root of “<<num<<” is “<<cube_root(num)<<endl;

return 0;
}


quiz03

And there is the program……..

#TC1017

#Quiz03

 


#Quiz03

--Originally published at Solving Problems with Programming

PICTURE OF AUTOR

THIS IS THE #QUIZ03 WHOSE OBJECTIVE IS CREATE AND CALL FUNCTIONS TO DO DIFFERENT TASKS AT DIFFERENT TIMES. COVERING #MASTERYTOPIC06 #MASTERYTOPIC07 .

This #QUIZ03 makes a main routine that asks the user for a number and then calls your functions to calculate the square and cube roots of that number and prints them out. And if the number given is negative. It is going to print the cube root number but in the square root it will tell the user that the number given for that root is IMAGINARY! (#Mastery11).

FIRST TO DO THIS #QUIZ03 I HAVE TO USE THE LIBRARY FOR THE COMMANDS OF MATH THAT WOULD BE SQRT FOR SQUARE ROOT AND CBRT FOR THE CUBE ROOT AND I FOUND THE NAME OF THE LIBRARY IN THE BOOK OF THE COURSE THAT IT IS CALLED #include<cmath>:

library

Link of the book that told me the name of the math library: How to Think Like a Computer Scientist C++ Version by Allen B. Downey.

And also for the name of the commands i used for information this link: function <cmath> cbrt and this link: function <cmath> sqrt

cube2

PICTURE OF AUTOR

funx

PICTURE OF AUTOR

SO AT FIRST THING YOU DO THE SAME AS OTHER PREVIEW CODES DECLARATE YOUR TYPE OF VARIABLE AND YOUR VARIABLE AND ASK THE USER TO GIVE YOU A NUMBER.

AFTER THAT YOU HAVE TO CALL YOUR CREATED FUNCTIONS THAT WERE:

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

YOU HAVE TO KNOW THAT USING THESE 2 FUNCTIONS IS OTHER EXTERNAL FUNCTIONS FROM THE MAIN THAT HAS A DOUBLE FUNCTION VALUE OF 64 BITS AND DECLARING A DOUBLE VARIABLE X INSTEAD OF USING THE VOID MAIN THAT HAS 0 BITS.

TO CALL THE FUNCTION IN THE MAIN

new
Continue reading "#Quiz03"

I finished the quiz in time!!

--Originally published at Programming Path

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

Continue reading "I finished the quiz in time!!"

Quiz #3

--Originally published at my programming blog

Today I did the Quiz #3, at first I was lost I didn’t understand very well how to make a function, but I asked for help and my classmates and my teacher helped me.

Once you understand it it’s not that hard.

What I needed to do was ask the user for a number and the calculate the square and cube root of that number, also I needed to include what happens if the user enters a negative number.

First I included the library cmath so I could use math functions in my program, after that I created my two functions, one for the square root and the other for the cube root and I specified what the function needed to do. And then started with the ” int main ()” and all my basic program and at the end I added an “if” to give the option of when the user entered a negative number.

So this is my code:

screen-shot-2017-01-26-at-10-15-19-am

#include
#include
using namespace std;

double square_root(double x){
return pow (x, 0.5);
}
double cube_root(double x){
return pow (x, 0.333);
}

int main(){
double x;
cout<<“Let’s find the square and cube root of a number!”<<endl;
cout<< “Give me a number:”<<endl; cin>>x;
if (x<0){
cout<<“Error! The number you entered is negative.”<<endl;
} else {
cout<< “The square root is: “<<square_root(x)<<endl;
cout<< “The cube root is: “<<cube_root(x)<<endl;
return 0;
}
}

And this is the program when I run it:

screen-shot-2017-01-26-at-10-20-35-amscreen-shot-2017-01-26-at-10-19-59-am


Quiz Week 3: Challenged with functions

--Originally published at Let&#039;s CODE

Today the whole classroom were ordered to write a code as a quiz that could calculate square and cube roots in a uncommon way where we used functions. At the first of the class many of us didn’t know what to do, so we started to search information on the web. Finally, I asked for … Continue reading Quiz Week 3: Challenged with functions

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.


Quiz 03

--Originally published at Ken&#039;s Disciple 01

Picture by Unsplash Unsplash

 

In this Quiz we had to make a main routine that asked for a number and showed the square and cubic roots of that number and printed them. We had to be careful, cause we had to give a message in our square root if the number given was negative.

This is my code: (also see it on GitHub)

Captura de pantalla 2017-01-30 a la(s) 21.50.36.png

There are several things we had to do in this quiz: (there are different ways we could do this, I made it this way)

  • We had to include our library cmath, for our functions square_root and cubic_root, and of course they had to return both sqrt(x) and cbrt(x).
  • I made if’s functions, if the number given was smaller than cero, it will give the advice that de square root couldn’t be done because the number was negative, it would give the cubic root and also gives the opportunity to give another number, a positive one (that was made with a do-while function. If the number was bigger than cero, we were good to go and it would give us all the answers and it would close.

If you have any questions feel free to ask.

 

L.out