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”

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”

Temperature

This program works by asking some temperature in Fahrenheit and then convert it to Celsius. This program was not so easy but not very hard, so here i have my program in Atom.

temperature

I have a very dumb problem because when I thought that it was done, I did it as the other two, I compile it and try to run it,  but when I tried that there was not a response with the temperature in Celsius, jaja I forgot to tell the program to give me the response. Then when I was getting crazy I re-checked the hole program and I find out that error.

And here is how the program works.

temperature1