Quiz #4

--Originally published at my programming blog

Today I did the quiz #4in which I needed to do a program that asked the user for 3 integers and then returned the smallest integer and the sum of squares of the three of them.

First I did the function to find the smallest value, what I did was import a library called “algorithm”, where I could use the function “min” that returns the smaller value between two values, but because I had three values I used the “min” function two times as you can see in the code (to get the min between two numbers and then get the min between the other number and the result of the first min.). I learned this method because I googled it, thanks to this page:

http://stackoverflow.com/questions/9424173/c-find-the-smallest-amongst-3-numbers

The I imported another library called “cmath” that allows me to use the function “pow”, so in the second function to calculate the sum of squares I wrote pow 2 for each value (x, y and z) and then I added the results of the pow.

Here’s my code:

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int minimumThree(int x, int y, int z){
return min(x, min(y, z));
}
int sumSquares( int x, int y, int z){
return pow(x,2)+ pow(y,2)+ pow(z,2);
}

int main ()
{
int x, y, z;
cout<< “I’m going to calculate the sum of squares of three numbers.”<<endl;
cout<< “And I’ll tell you which one of those three numbers is the smallest.”<<endl;
cout<<“Please give me the first number:”<<endl;
cin>> x;
cout<< “Please give me the second number:”<<endl;
cin>> y;
cout<<“Please give me the third number:”<<endl;
cin>> z;
cout<< “The minimum is: “<< minimumThree(x, y, z)<<endl;
cout<< “The sum of squares is: “<<sumSquares(x, y, z)<<endl;

return 0;
}

screen-shot-2017-02-02-at-3-23-27-pmscreen-shot-2017-02-02-at-3-24-22-pm


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 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