On to functions

So this has been the easiest task and blogpost so far! They say that if you work hard at the beggining it will help you at the end, and it sure is paying off.

I mentioned all that because this program is exactly the same I had to do on #WSQ03, but now using functions. The cool thing is, when I did #WSQ03 I used functions in order to practice! So basically you´ll see the same program here. BTW, the image above is how this program looks when you run it.

Functions are really cool tools that you can use in order to put your program together in a more organized way. In order to use them, you have to build them first. You usually build them before the code inside main that “calls” the funtion, and before the main, which is a function as well actually.

To call the function, you just have to write the function name with a parenteses. Inside the parenteses you have to write the parameters, which are the values or information that the function is going to work with.

Here you can have a look at my code:

#include <iostream>

using namespace std;

int x, y;

int multiplication(int z, int p){

int operation = z * p;

return operation;
}

int divition (int z, int p){

int operation = z / p;

return operation;

}

int sum (int z, int p){

int operation = z + p;

return operation;

}

int subs (int z, int p){

int operation = z – p;

return operation;

}

int remainder (int z, int p){

int operation = z % p;

return operation;

}

int main(){

cout << “This program will do some basic operations with two numbers. Please provide the first number” << endl;
cin >> x;

code2#WSQ03
code1#WSQ03

Continue reading “On to functions”

Sum of numbers

The next task we were asked to perform was a program capable of adding the numbers inside a user specified range. The user provides the first value of the range and the last value of the range. We were asked to preferably use loops to solve this task.

This program was a bigger challenge for me. It helped me to really understand the way a counter works. It is kind of strange and hard to undestand how a variable is initializaed under the value of another one, and then inside the loop you initialize it to the value of itself plus one.

First I thought I had to use a for loop to run this program. But it was getting to hard for me and I could not do it. My fellow classmate and friend Alberto Rodriguez gave me a general idea of how it should work, using a do while loop.

So how this basically works is that the sum varable is initialized to the value of the first number of the range, before the for loop starts working. Inside the for loop, you intialize the sum funtcion again to the value of itself (which is now the value of the first number of the range) plus one. Then you make the actual operation, which is to add the value of the first number of the range plus the variable sum.

The program will keep on doing the operation while the value of sum is less than the last value of the range.

#include <iostream>

using namespace std;

int x, y, sum;

int main (){

cout << “Provide the first number of the range: ” << endl;
cin >> x;

cout << “Provide the last number of the range: ” << endl;
cin >> y;

sum = x;

do{

Continue reading “Sum of numbers”

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”

Random number generator

Our next task was to build a random number generator wth a C++ program. This was the first time I ever did something like that. I tried unsuccesfully to do it by myself at first, but I just couldn´t figure out what to do. So I decided to check what the Oracle of Google had for me. I came up with this information, basically the first webpage on the list. I also checked out a video from my  Yankee tutor on Youtube, thenewboston. 

The program basically works with a do while loop, so you can keep guessing until you hit the correct number. Also, I you miss, it is programmed to tell you wether you gave a higher number or a lower number. I did this with an If cycle.

  • About the random number, it works with the function rand, contained in the header #include <stdlib.h>
  • In order fot the numbers to never be predictive, I used the function time, as a paramater of srand. As a paramater of time  I used 0, as thenewboston explained, this would make the time to use he number of seconds since it was developed, probably in the 70´s. PEACE.
  • So, now the numbers should be really hard to predict.
  • In order to only get random numbers between 0 and 100, we had to use the modulus 100 + 1 of the function rand()

So no more talking, the code can tell a thousend words

“Dead men tell no tales” ARGGGHH

Sorry Blackbeard, go back to the XVII century bro.

Have a look at my code:
#include <stdlib.h>
#include <time.h>
#include <iostream>

using namespace std;

int main ()
{
int x, y;
srand (time(0));

x = rand() % 100 + 1;

do {
cout << “Guess a

Continue reading “Random number generator”