Skip to main content

Mastery14

1 min read

Diego Plascencia Sanabria A01229988

For creating and using your own lybraries just follow the next steps:

-First, create the files on "mylibrary.cpp" and "mylibrary.h"

-Next, compile the library without the function main(), this will generate a file called "myfile.o"

-Next, create another file called "main.cpp" (it can be another name, but this one is easier), in this file you need to "mylibrary.h"

-Finally, Compile main.cpp and add the library "myfile.o" 

Hope it help you.

STRING

1 min read

Diego Plascencia Sanabria A01229988

Strings are used very often in programming, strings are used to return non numerical values like words or phrases or even letters. Here is an example of a program that use strings:

<iostream>

using namespace std;

string my_string1 = "a string";
string my_string2 = " is this";
string my_string3 = my_string1 + my_string2;

// Will ouput "a string is this"
cout<<my_string3<<endl;

As you can see, it is very easy, hope it helps you.

VECTORS

1 min read

Diego Plascencia Sanabria A01229988

Vectors are sequence containers that can change in size. Its function is to store different values under a single name.

When using vectors in our programs we must include in the top of our program <vector>

The values can be in any data type (int, double, string...).

Vectors are declared with the following syntax:

           vector<type> variable_name (number_of_elements);

or

          vector<type> variable_name;  (the number of elements is optional).

Here are some example of vectors:

    vector<int> values (5);      // Declares a vector of 5 integers
    vector<double> grades (20);  // Declares a vector of 20 doubles
    vector<string> names;        // Declares a vector of strings,  

 

Use of recursion for repetitive algorithms

1 min read

In simple words, recursion is when a function calls itself, creating a loop. Recursion could be used to complete the fibbonacci serie, here is an example of a program with recursion:

 <iostream>

using namespace std;

int fib(int x) {
    if (x == 1) {
        return 1;
    } else {
        return fib(x-1)+fib(x-2);
    }
}

int main() {
    cout << fib(5) << endl;
}

As you can see in the example above, the function fib is used inside the same funtion
)it is calling itself).

Thank you for reading my explanation, I hope it have helped you. For a more complete
explanation take a look at this link:

http://computacion.cs.cinvestav.mx/~acaceres/courses/estDatosCPP/node37.html

FUNCTIONS

2 min read

   

DIEGO PLASCENCIA A01229988

PROGRAMA CON FUNCIONES DE SUMA, RESTA, MULTIPLICACION, DIVISION Y % CON VARIABLES DEFINIDAS.

<iostream>

using namespace std;

int fsuma(int num1, int num2){   

int resultadosuma;

resultadosuma= num1+num2;

return resultadosuma;}

 

int fresta(int num1, int num2){

int resultadoresta;

resultadoresta= num1-num2;

return resultadoresta;}

 

int fproducto(int num1, int num2){

int resultadoproducto;

resultadoproducto= num1*num2;

return resultadoproducto;}

 

int fdivision(int num1, int num2){

int resultadodivision;

resultadodivision= num1/num2;

return resultadodivision;}

 

int fresiduo(int num1, int num2){

int resultadoresiduo;

resultadoresiduo= num1%num2;

return resultadoresiduo;}

 

int main (){

int suma,resta,producto,division,residuo;

int a=7;

int b=3;

 

suma=fsuma(a,b);

cout<< "EL RESULTADO DE LA SUMA ES: "<<suma<< endl;

 

resta=fresta(a,b);

cout<< "EL RESULTADO DE LA RESTA ES: "<<resta<< endl;

 

producto=fproducto(a,b);

cout<< "EL RESULTADO DEL PRODUCTO ES: "<<producto<< endl;

 

division=fdivision(a,b);

cout<< "EL RESULTADO DE LA DIVISION ES: "<<division<< endl;

 

residuo=fresiduo(a,b);

cout<< "EL RESULTADO DEL RESIDUO ES: "<<residuo<< endl;}

TEMPERATURE

2 min read

DIEGO PLASCENCIA A01229988

THIS PROGRAM IS USED TO TRANSFORM A TEMPERATURE GIVEN BY THE USER IN FAHRENHEIT TO CELCIUS AND KNOWING IF THE WATER WILL BOIL AT THAT TEMPERATURE.

<iostream>
using namespace std;
int main (void){
    double t; // variable for temperature.
    double r; // variable for the result.
    cout<< "What is the temperature in Fahrenheit? "<< endl;
    cin>> t;//user introduce the value in Fahrenheit.
    r= (5*(t-32))/9; //formula
    cout<< "A temperature of "<< t << "degrees Fahrenheit is" << r <<"in Celsius"<<endl;
    if (r>=100){ //if r (temperature in celcius) is higher than 100, it will display that water will boil.
        cout<< "Water boils at this temperature"<<endl;}
    else{ //if r (temperature in celcius) is lower than 100, it will display that water will not boil.
        cout<< "Water don't boil at this temperature"<<endl;   
        }
   
}

FUN WITH NUMBERS

1 min read

  
I made this just to have fun with numbers, + , - , * , / , %
Diego Plascencia Sanabria.

<iostream>

using namespace std;

int x=5;

int y=3;

int s, r, p,d,otro;

int main (void){

 

s= x+y;

cout<<"la suma de los valores es =  "<< s << endl;

r= x-y;

cout<<"la resta de los valores es =  "<< r << endl;

p= x*y;

cout<<"el producto de los valores es =  "<< p << endl;

d= x/y;

cout<<"la division de los valores es =  "<< d << endl;

otro= x%y;

cout<<"el % de los valores es =  "<< otro << endl;

 

}