Quiz 6

--Originally published at My awesome Blog!

What do you think the code below does? answere in the comments

#include <iostream>
#include <stdio.h>
using std::cout;
using std::cin;
using std::endl;

int mcd(int a, int b)
{
int c, d, e;
e = a % b;
do
{
d = b;
b = e;
e = d % b;
} while(e != 0);
return (b);
}

int main(){
int a, b, c, d, e;
cout << “Algoritmo de Euclides ” << endl;
cout << “Introduce un numero ” << endl;
cin >> a;
cout << “Introduce otro numero ” << endl;
cin >> b;
cout << “El M.C.D de los numeros es ” << mcd(a, b) << endl;
return 0;
}


Quiz 5

--Originally published at My awesome Blog!

#include <iostream>
#include <math.h>
using std::cout;
using std::cin;
using std::endl;

int find_threes(int number[], int numNumbers){
int sum = 0;
for(int i = 0; i < numNumbers; i++){
if (number[i] % 3 == 0)
{
sum += number[i];
}
else
{

}
}
return sum;
}

int main(){
const int NUM_NUMBERS=8;
int x[NUM_NUMBERS];
for (int i = 0; i < NUM_NUMBERS; i++){
cout << “Dame un número “;
cin >> x[i];
}
int sum = find_threes(x,8);
cout << “La suma es ” << sum << endl;
}

Here is the code from my quiz 5. Type down the comments what do you think i do


How to Write coments

--Originally published at My awesome Blog!

In this code it is shown how to type coments in any code in c++

 

#include <iostream>

using namespace std;
// Add a Double “//” then just type. The program will ignore the text after the “//”
int main()
{
cout << “Hello world!” << endl;
// No matter where in the code it is

/* you can also use “/*” to start a coment but you need to close it just like a parenthesis with “* /” */

return 0;

}


Nested If

--Originally published at My awesome Blog!

En este programa se muestra como utilizar if anidados, y muestra una jerarquía para ver el orden en el cual se ejecutan, por ejemplo para sacar cual es el numero mayor de todos, en un listado de números dados por el usuario

#include <iostream>

using namespace std;

int a , b, c, d;

int main()
{
cout << “escribe un numero” << endl;
cin >> a;
cout << “escribe otro numero” << endl;
cin >> b;
cout << “escribe otro numero” << endl;
cin >> c;
cout << “escribe otro numero” << endl;
cin >> d;
if (a>b){

if (a>c)
{
if (a>d){

cout << “El primer numero es mas grande que todos ” << endl;
}
}
}
else {

cout << ” mejor me mato ” << endl;
}
return 0;
}


Nested For

--Originally published at My awesome Blog!

En este código se muestra brevemente como utilizar for anidado, es decir un for dentro de otro. se puede apreciar claramente como aplicar en el ejemplo de abajo, donde hay una jerarquía entre fors para ver cual se ejecuta primero.

 

#include <iostream>

using namespace std;
int a;

int main()
{
cout << “For example ” << endl;
for (int x= 0 ; x <= 10;x++ ) {
cout << “Este es el ciclo numero ” << x << endl;

}
cout << “——————————————————————————–” << endl;
cout << “Nested For example ” << endl;

cout << ” ” << endl;
for (int z = 0 ; z >= 0 ; z– ) {
for (int y = 10; y >= 0 ; y–){
cout << “El ciclo for para la variable Z comienza en ” << y << endl;

}
}
return 0;
}


Creating and calling Functions

--Originally published at My awesome Blog!

In this program it is show how to declare and call funtions, as shown below

#include <iostream>

using namespace std;
int suma,x , y,suma2;
int funcionuno(int x) {
y = x;
suma2 = y + 10;
return suma2;
}

int main()
{
cout << “Escribe el valor de x ” << endl;
cin >> x;

suma = funcionuno (x);

cout << “El valor de la Funcion es ” << suma << endl;
return 0;
}

 

Te declare a funtion you hace to declare what type of value it will be (Int, Float , Double atc)
then declare the name, and open “(putting here the value the function will recieve)”

To call a function you just have to type the name of the funtion and in the “()” you put the value to run the function

Int Funcion1 (x)


QUIZ 4

--Originally published at My awesome Blog!

Este código da el valor para la variable mas baja , y da el resultada para la suma de los cuadrados de las mismas variables utilizando funciones.
lo hice utilizando if y else comparando los números utilizados y luego utilizando la función pow () de la librería cmath para hacer las operaciones y luego todo se corre en el main.
#include <iostream>
#include <cmath>
using namespace std;
int x =2, y =3  , z=5 , minimo, sumsquare;
int valorminimo (){
if (x < y || x<z ){
        minimo=x;
}
else {
if (y <x || y<z ){
minimo=y;
}
else { minimo=z;}}
return minimo;
}
int squaresums() {
sumsquare = pow(x,2)+ pow(y,2) + pow(z,2);
return sumsquare;
}
int main()
{
valorminimo();
squaresums();
cout << “El valor minimo es ” << minimo << ” la suma de los cuadrados es ” << sumsquare << endl;
return 0;
}

Quiz 3

--Originally published at My awesome Blog!

Quiz 3 :
First i added the library “cmath” so i can use the functions for square and cubic root , then i declared some variables for to use as shown.

Inside the main function i ask the user for the value of x, then i send the value of x to the function Square_root , that returns the value for the result for the square root . when it finishes it goes back to the main and then sends the value of x to the function Cubic_root and returns a value for the result.

i have an if-else to decide if the x value is negative to decide if its a imaginary root.

Enjoy!
CODE HERE:
#include <iostream>
#include <cmath>
using namespace std;
double x1,x2,x, squareroot , cubicroot ;
double Square_root (double x) {
squareroot = sqrt( x );
return squareroot ;
}
double Cubic_root (double x) {
cubicroot = cbrt (x);
return cubicroot;
}
int main()
{
cout << “Escribe el valor de x” << endl;
cin >> x;
   x1 =  Square_root(x);
x2 = Cubic_root(x);
    if (x >0  ) {
    cout << “la raiz cuadrada de  ” << x << “es  ” << x1 << endl;
cout << “la raiz cubica de  ” << x << “es  ” << x2 << endl;
    }
else {
cout << “La raiz es imaginaria ” << endl;
}
return 0;
}