Quiz 6. Euclid.

Euclid was one of the great ancient greek guys. Among the stuff he invented was the algorithm bearing his own name.

This is a common algorithm used in computing. It is used to find the greatest common denominator of two numbers (GCD). If you want to learn more about this, you should totally check the explanation at Khan academy´s post.

Orlando´s code helped me a lot. You should totally take a look. First I tried to make the code using a do while loop. It was becoming really hard, until I saw Orlando´s code using recursion. It was way easier to code it this way .

Here you can see a picture of the code (anyways you can see it on github Here):

code_euclid

code_euclid

Link to my code on github.

Quiz 5

 

Finally, I switched from CodeBlocks to Cygwin and Notepad++.

For this quiz, we were required to make two programs. One had to verify if a word was a palindrome or not, and the other had to make the summatory  of all the numbers evenly divisible by three, from a list of numbers provided by the user.

The second problem was way easier for me than the first one, since I had never used strings in my life. After doing some research on strings, I tried my first version, which did not work. Today, Salvador Ibarra´s blog was updated with a really helpful video on the subject, in Spanish. Also, his code on github helped me a lot. So basically, thank you Salvador, I owe you one.

But the thing is not only to copy, but to understand, so here I´ll explain you my version of the palindrome program.

First of all, a palindrome is a word or a number that can be read normally or backwards and yoy get the same word. For example Anna, read it backward and it is the same word. 111 is a palindrome number.

In order to make a program to capable of deciding if a word is a palindrome or not, I had to use strings in C++. I made four strings and a string function.  Two were really important, string palabra and string backwards. On string palabra the program stores the word provided by the user, and on string backwards it stores it backwards.

The process made to store the word backwards was done with a for loop. The parameter to continue the for loop was the length of the string palabra, which I stored on an integer variable called largo. To get that value, you just write the name of the

for loop

Continue reading “Quiz 5”

Quiz 03

Today we had to do Quiz number 3. Problem number one was about the distance between two points on a cartesian plane. Number two was way harder. We had to make a program that would calculate the Fibonacci series of numbers till the number that the user inputs.

Problem number one was easy for me. I just used the equation of the distance between two points, which is the Pitagorean Theorem applied.

distance-between-two-points-formula

My translation to C++ language was:  d = sqrt(pow((c-a),2)+ pow((d-b),2)), a, b, c and d being parameters of my function, input by the user.

The second problem was the one that troubled me. The most important thing while solving any kind of problem, be it C++ or physics is to really understand what you are being asked. So, that was my frst step, to understand what a Fibonnacci series is. It is the sum of a number plus the next one, starting on 0. So the series would look like this:

0,1,1,2,3,5,8,13…

0+1 = 1, 1+1 = 2, 2+1 = 3, 3+2 = 5, 5+3 = 8, 8+5 = 13, 13+8 = 21;

To translate this to a command in C++ was hard for me. But I did it the next way:

#include <iostream>

using namespace std;

int n, fI = 0, fII = 1, f;

int fibonacci(int n){
for (int t = 1; t < n ; t++){
f = fI + fII ;
fI = fII;
fII = f;
}
return f;
}
int main(){
cout << “Provide the nth number of the Fibonacci list that you want to print: “<< endl;
cin >> n;
cout << “Your number is: ” << fibonacci(n);
}

I created a function called fibonacci, with 1 parameter (the number of the series that the user wanted to output). Inside the

Quiz03.jpg

Continue reading “Quiz 03”

Quizz 2

The image on top of this text has nothing to do with this quizz. Actually it has to do with the first quizz! I just tought that it looked cool. It is a screenshot of one of my assignments. Feel free to use it.

The time for quizz number two arrived today. What the proffesor intends to do with this quizzes is that his students get to know in what subjects they are not doing good, so they can get o study them!

This quizz was a little bit more challenging for me. I guess it´s normal, since I think difficulty should increase as time progresses. But anyway, this quizz was about Loops, basically. Here you can look at what we were asked to do:

quizz

In problem number one I had a little bit more difficulty solving it. I decided to use a For Loop inside a function called “superpower”, but I just couldn´t figure out what the operation had to be. I knew I had to multiply b times a*a. So what I basically did was that I created an int variable called “o” and I initialized it on 1. o = 1. Then, inside the for loop, I wrote o = 0 * a. Everything will make sense if you look at the code:

#include <iostream>

//Program1
//Ernesto Sánchez Bernal

using namespace std;

int x, y, o = 1;
int superpower (int a, int b){

for (int t = 1; t <= b; t++ ){

o = o * a;
}
return o;
}

int main(){

cout << “Provide the number you want to elevate to the power of another number: ” << endl;
cin >> x;

cout << “Provide the value of the power: ” << endl;
cin >> y;
cout << “The answer is: ” <<

Continue reading “Quizz 2”

Quiz 1

Hello dear readers! Hope you are having a great day. Here I´ll explain all about the first Quizz of the semester, so if you are still interested in knowing about it keep reading!

So for the first quizz, we were asked to solve some pretty easy tasks with a C++ program. The first task was to make a program capable of solving the volume of a cylinder. The machine would get the radius and height of any cylinder as paramaters and solve for the volume.  The equation for the volume of a cylinder is V = 3.14 * H * R. H is equal to the height and R is equal to the radius of the cylinder.

For this program, I made a function called “volume” with two float variables as parameters, wich were the height and radius respectly. Here you can have a quick look at the code:

#include <iostream>
#define pi = 3.1415

using namespace std;

float r, h; 

float volume(float r, float h){

float v = 3.1415 *(r*r)*h;

return v;
}

int main(){

cout << “Provide the radius of the cylinder: ” << endl;
cin >> r;

cout << Provide the height of the cylinder: ” << endl;
cin >> h;
cout << “The volume of your cylinder is equal to: ” << volume(r,h) << endl;
}

Pretty simple huh? Well that was just the first part of the quizz. On the next two parts, we were asked to make a program capable of solving basic arithmetic operations (addition, divition, multiplication, substraction). The only difference between the two of them was that the first one was supposed to be done using float variables, so you would be able to get the remainder of a division. To get the remainder, I used mod (%), wich

Continue reading “Quiz 1”