Quiz 7

Last quiz everyone!!!!! jajajaja in this quiz I had to calculate the dot product of two vectors “seen in physics”, using arrays.

//CODE:

#include <iostream>
using namespace std;

double dot_product(double list1[], double list2[], int l1){
double dot=0;
for(int i=0; i<l1; i=i+1){
dot=dot+(list1[i]*list2[i]);
}
return dot;
}

int main(){
int l1;
double product;

cout<<“This program help to calculate the dot product of the lists of values given by the user.”<<endl;
cout<<“Please enter the values to the first list.”<<endl;
cout<<“How many caracters the lists have?”<<endl;
cin>>l1;
double list1[l1], list2[l1];

for(int i=0; i<l1; i=i+1){
cout<<“Type the ” <<i+1<<” “<<“value of the 1st list.”<<” “;
cin>>list1[i]; }
cout<<“Please enter the values to the second list.”<<endl;

for(int i=0; i<l1; i=i+1){
cout<<“Type the ” <<i+1<<” “<<“value of the 2nd list”<<” “;
cin>>list2[i]; }

product=dot_product(list1, list2, l1);
cout<<“The dot product of both lists is:”<<” “<<product<<endl;
return 0;
}

Captura de pantalla 2016-04-30 a las 21.57.50.png

Quiz 6

Write a function to calculate the greatest common denominator of two positive integers using Euclid’s algorithm.

//CODE:

#include <iostream>
using namespace std;

int gcd(int x, int y){
int r;
if (x==0){
return x;
}
while (y!=0){
r= y;
y= x%y;
x= r;
}
return r;
}

int main (){
int x,y;
cout<<“Please enter a number: ” <<endl;
cin>>x;
cout<<“Please enter a second number: ” <<endl;
cin>>y;
cout<<“The greatest common denominator between (” <<x <<” y ” <<y <<“) is = ” << gcd(x,y) <<endl;

return 0;
}

Captura de pantalla 2016-04-30 a las 21.54.25.png

Captura de pantalla 2016-04-30 a las 21.56.18

Quiz 5

Determine if the word given by the user is a palindrome or not, by making the word backwards and comparing it to the original word with a simple “if condition”.

//CODE:

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

string word, fin;

string is_palindrome(string word)

{
string backwards;
cout << “Type a word: n”;
cin >> word;

int large = word.length();
for (int x = large – 1; x >= 0; x–)
{
backwards += word[x];
}
if (backwards == word)
{
fin = “Your word IS a palindrome”;
}
else
fin = “Your word IS NOT a palindrome”;
return fin;
}

int main()
{
cout << is_palindrome(fin);
}

Captura de pantalla 2016-04-30 a las 21.53.33.png

Quiz 4

Create a function called euler_calc with a single parameter precision. The value of precision is used to determine when to stop calculating. Your calculation will stop when the two consecutive values estimating e differ by less than precision

 

//CODE

#include <iostream>
#include <iomanip>
using namespace std;

double factorial(int n){
double fact = 1;
for (int i = 1; i <= n; i++){
if (n==0){
return 1;
}
else {
fact = fact * i;
}
}
return fact;
}
float euler_calc (float ac){
float e = 1.0;
for (int i=1; i<999; i++){
e = e + 1/(factorial(i));
}
cout<< fixed <<setprecision(ac) << e;
return e;
}
int main(){
float ac;
cout<<“How accurate you want the result? “;
cin>>ac;
cout<<“Here is your number: “<<endl;
euler_calc(ac);
cout<<endl;
return 0;
}

Captura de pantalla 2016-04-30 a las 21.49.28.png

Euclids on the block! #Quiz6

Euclides es como mi dignidad, yo aseguro que existe pero nadie está de acuerdo con eso. Al igual que lo anterior dicho, la ente simplemente no puede ponerse de acuerdo sobre si Euclides existió o no, lo que no se puede negar ni de broma, es la utilidad de su algoritmo es la geometría.

Euclid_o_24537

Esta es la mejor imagen que pude encontrar.

El código resultó ser muy sencillo, creo que ha sido el más corto de todos pues la salida del programa sólo tenía que ser el MCD (máximo común divisor) y la función principal era portar el algoritmo de Euclides hacia el lenguaje de programación, el cual resultó en esto:

# --------------------- Este es el código correspondiente al Quiz 6 ----------------------------
def ade(a, b):
if a > b:
c = a - b
ade(b, c)
elif a < b:
c = b - a
ade(a, c)
else:
print(a)

ade(a=int(input("Vamos, introduce el primer número: ")), b=int(input("Ahora introduce el segundo número: ")))

Como siempre, mi código en Github está aquí.

 

Quiz 3-Finally

Hey world…

Just to tell you that I finally finished the entire work for this partial, I have got help from the professor Ken via Twitter, I asked for help and he did answered, also it took like a minute really!!!

Quiz three was divided in two programs or sections… the first one is related with the Cartesian Plane, and some points in it, the thing is that the program is able to calculate the distance between those two point, which are given by the user randomly, using the Pitagora’s theorem to solve it.

Also I had difficulties with the function formula (synthesis), but was no problem i manage to solve the problem with a different algorithm. As you can see part of the algorithm is as a “comment”, just to ask in class what is wrong with it and how I should have done it.

#SolvingProblemsAsEngineer #IMD

//CODE:

# include <iostream>
# include <cmath>
using namespace std;

/*int x1, x2, y, y2;

void Distance(int x1, int x2, int y, int y2) {
int dx, dy, dist, C;
dx=(x2-x1);
dy=(y2-y);
C=((dx*dx)+(dy*dy));
dist=sqrt(C);
std::cout << “dx–“<<dx << std::endl;
std::cout << “dy–“<<dy << std::endl;
std::cout << “The distance between the points is… “<<dist << std::endl;
}*/

int main(int argc, char const *argv[]) {
int dx, dy, x1, x2, y, y2, dist, C;

std::cout << “This program calculates the distance between two points in the cartesian plane.” << std::endl;
std::cout << “Next enter the coordenates of the first point” << std::endl;
std::cout << “Please enter x1)” << std::endl;
std::cin >> x1;
std::cout << “Pease enter y1” << std::endl;
std::cin >> y;
std::cout << “Please enter x2” << std::endl;
std::cin >> x2;
std::cout << “Please enter y2” << std::endl;
std::cin >> y2;

dx=(x2-x1);
dy=(y2-y);
C=(pow(dx,2)+pow(dy,2));
dist=sqrt(C);
std::cout << “The distance between the points

Captura de pantalla 2016-02-17 a las 19.56.15

Continue reading “Quiz 3-Finally”

QUIZ 2

This quiz was also separated into two parts… the first one is able to ask for two different numbers to the user and calculate with a power function, the first number to the power of the second number.

//CODE 1:

# include <iostream>
#include <cmath>
using namespace std;

int main(int argc, char const *argv[]) {
int num1, num2, R;
std::cout << “This program makes the power function between number 1 and number 2.” << std::endl;
std::cout << “Please enter number 1” << std::endl;
std::cin >> num1;
std::cout << “Please enter number 2” << std::endl;
std::cin >> num2;

R=pow (num1, num2);

std::cout << “The final result is… ” <<R << std::endl;
return 0;
}

Captura de pantalla 2016-02-17 a las 19.14.53

This code is able to print a certain number of stars with a value given by the user. Using a function called “stars” and a loop.

//CODE 2:

#include <iostream>

using namespace std;

void stars(int s) {
int count=0;
do {
std::cout << “*” << std::endl;
count++;
} while(count!=s);
}

int main(int argc, char const *argv[]) {
int s, count;

std::cout << “This program prints the stars you ask for.” << std::endl;
std::cout << “Please enter the number of stars you want to print in the console.” << std::endl;
std::cin >> s;
stars(s);

return 0;
}

Captura de pantalla 2016-02-17 a las 19.16.49

QUIZ 1

Quiz 1 was divided in three programs… the first one is able to determine the volume of a cylinder… the second one is able to calculate some basic operations (without using functions)… and the third one is basically similar to the second one but with more basic operations.

//CODE 1:

#include <iostream>
using namespace std;

int main(int argc, char const *argv[]) {
float radius, height, volume;

std::cout << “This program can determine the volume of a cylinder.” << std::endl;
std::cout << “Please enter the value of the radius of the cylinder.” << std::endl;
std::cin >> radius;
std::cout << “Please enter the height of the cylinder.” << std::endl;
std::cin >> height;
volume=(3.1415*(radius*radius)*height);
std::cout << “The volume of the cylinder is “<<volume << std::endl;

return 0;
}

Captura de pantalla 2016-02-17 a las 19.09.49

//CODE 2:

#include <iostream>
using namespace std;

int main(int argc, char const *argv[]) {
int n1, n2, r1, r2, r3;
std::cout << “This program can determine the product, integer division and the remainder of two random numbers.” << std::endl;
std::cout << “Please enter the first number.” << std::endl;
std::cin >> n1;
std::cout << “Please enter the second number.” << std::endl;
std::cin >> n2;
r1=n1*n2;
r2=n1/n2;
r3=n1%n2;
std::cout << “The product of the two numbers is… “<<r1 << std::endl;
std::cout << “The integer division of the two numbers is… “<<r2 << std::endl;
std::cout << “The remainder of the two numbers is… “<<r3 << std::endl;
return 0;
}

Captura de pantalla 2016-02-17 a las 19.10.49

//CODE 3:

#include <iostream>
using namespace std;

int main(int argc, char const *argv[]) {
float n1, n2, r1, r2, r3, r4;
std::cout << “This program can determine the product, integer division and the remainder of two random numbers.” << std::endl;
std::cout << “Please enter the first number.” << std::endl;
std::cin >> n1;
std::cout << “Please enter the second number.”

Captura de pantalla 2016-02-17 a las 19.11.32

Continue reading “QUIZ 1”