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

#WSQ13-Partial exam 2

Here is the partial exam answered of course jajajaja so the exam just had four questions and I will list them in order.

QUESTION 1:

Write a function called triangles which receives a single parameter (int) which represents the size of a triangle as explained below. The function should print a triangle using loops (for or while). The only characters printed here are ‘T’ and the new-line character. The first line is length one, the middle line is length size and the last line is length one. The example below is for size 6.

      T
      TT
      TTT
      TTTT
      TTTTT
      TTTTTT
      TTTTTT
      TTTTT
      TTTT
      TTT
      TT
      T

//CODE

// Ever Olivares
#include <stdio.h>
#include <iostream>
using namespace std;

int triangle (int x)
{
for (int i=1; i<=x ;i++)
{
for (int j=1; j<i+1; j++)
cout <<“T”; cout <<endl;
}
for (int i=x-1; i>=1; i–)
{
for (int j=1; j<i+1; j++)
cout <<“T”; cout <<endl;
}
return 0;
}
int main ()
{
int y;
std::cout<<“type the numbers of T you want” <<std::endl;
std::cin >> y;
triangle (y);
}

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

QUESTION 2:

Write a function called superpower that has two parameters of type long and returns a long which is first parameter raised to the power of the second, which is to say it returns ab So, superpower(3,4) would return 81.

//CODE:

// Ever Oliavres
#include <iostream>
#include <cmath>

void superpower(int num1, int num2){
int R;
R= pow(3,4);
std::cout << “The result is… “<<R << std::endl;
}

int main(int argc, char const *argv[]) {
int R;
superpower(3,4);

return 0;
}

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

QUESTION 3:

Write a function called fibonacci which receives a long “n” and returns a long which is the value of the nth number in the fibonacci series which is: 0,1,1,2,3,5,8,13,21,34,55,89…………So, fibonacci(0) would return 0. fibonacci(5) would return 5, fibonacci(8) would return

Captura de pantalla 2016-04-30 a las 21.42.16.png
Captura de pantalla 2016-04-30 a las 21.43.21.png

Continue reading “#WSQ13-Partial exam 2”

#WSQ12-Word count

This one was cool but I do not know if it really work because it compiles and runs but do not count any words :(… anyway here it is.

//CODE

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int find_word(string word, string tipo){
string line;
int cont=0;
ifstream myfile(tipo);
if (myfile.is_open()){

while ( getline (myfile,line) ){
if(‘line’ == ‘word’){
cont=cont+1;
}
// cout << line << endl;
}
myfile.close();
}
else {
cout<<“CANNOT OPEN THE FILE”<<endl;
}
cout<<“The word…”<<word<<“… is used “<<cont<<” times.”<<endl;
return cont;
}
int main(int argc, char const *argv[]) {
string word, tipo;
int cont;
std::cout << “Please type the word you want to search.” << std::endl;
std::cin >> word;
std::cout << “Please enter the name of the file to search the word” << std::endl;
std::cin >> tipo;
find_word(word, tipo);
return 0;
}

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

#WSQ11-Yo soy 192

This one in particular was a challenge… but with help everything is possible. Also for this WSQ I had to “add” a new library so I could work with bigger numbers it is called “BigIntegerLibrary.hh”.

//CODE

#include <iostream>
#include <string>
#include “BigIntegerLibrary.hh”
using namespace std;

BigInteger lychrel (BigInteger x){

string y = bigIntegerToString(x);
lychrel(y.begin(), y.end());
BigInteger lychrel = stringToBigInteger(y);
return lychrel;
}

int main() {
int low,up,number,j,k,p=0;
int l=0, n=0;
BigInteger z,count=0, sum=0;

cout << “Give me the lower bound”;
cin>>low;

cout<< “Give me the upper bound”;
cin>>up;

for(count=low;count<=up;count++){
for(BigInteger i=count; i<=count;i++){

z= lychrel(i); sum=y+count;}

if(count==z) {
n=n+1;
} else {
int times= 0;

BigInteger zeit = count;
z=lychrel(zeit);

while(zeit != z && times < 30){

zeit = zeit + z;
z = lychrel(zeit);
times=times+1;
}
if(times < 30){
p = p + 1;
} else {
cout << “Lychrel found ” << count << endl;
l = l +1;
}
}
}

cout <<“The palindrom is: ” << n <<endl;
cout<<“Lychrel doesn’t become palindrome in: “<< p <<endl;
cout<<“The lychrel is: “<< l <<endl;

return 0;
}

Captura de pantalla 2016-04-30 a las 21.04.04Captura de pantalla 2016-04-30 a las 21.04.09

#WSQ10-Lists

In this WSQ I used arrays of numbers to calculate via functions some mathematical operations given, for example to calculate the total, average and standard deviation of those numbers.

 

//CODE

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

float total(float numbers[], int howMany){
float t = 0;
for (int num=0; num<howMany; num++){
t = t + numbers[num];
}
return t;
}

float average(float numbers[],int howMany){
float sum=0.0,prom=0.0;
for (int i=0; i<howMany;i++){
sum+=number[i];
}
prom=sum/howMany;
return prom;
}

float standarddeviation(float numbers[], int howMany){
float variation= 0.0, sum=0.0,prom=0.0,deviation=0.0;
for (int i=0;i<howMany;i++){
sum+=number[i];
}
prom=sum/total_num;
for(int i=0;i<howMany;i++){
variation=variation+pow((number[i]-prom),2);
}
deviation=sqrt(variation/howMany);
return deviation;
}

int main(int argc, char const *argv[]) {
float n, t, a, sd;

float num[10];
std::cout << “Enter 10 numbers Please.” << std::endl;
for (int i=0; i<10; i++){
std::cin >> n;
num[i]=n;
}
std::cout << ” Total,average,stdev of your numbers are..”;
t=total(num,10);
a=average(num,10);
sd=standarddeviation(num,10);
cout << t << std::endl;
cout << a << std::endl;
cout << sd << std::endl;
return 0;
}

Captura de pantalla 2016-04-30 a las 20.58.02Captura de pantalla 2016-04-30 a las 20.58.15

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”