Euler number

--Originally published at TC1017 Programing Curse

In this assignment i had to estimate the mathematical constant e. I create a function called calculuate_e which receives one parameter called precision that should specify the number of decimal points of accuracy.

There is the code

#include <iostream>
#include <math.h>
using namespace std;

int getFactorial(int num){
if (num == 1 || num == 0)
{
return 1;
}
else
{
return num*getFactorial(num-1);
}
}
float euler_calc(float par){
float num = 0.0, result=0.0;
do{
result += 1.0/(getFactorial(num));
num = num + 1;
} while(num <= par);
return result;
}

int main(){
float par, result, num=0.0;
cout << “Give me the parameter ” << endl;
cin >> par;
cout << “The answer is ” << euler_calc(par) << endl;

return 0;
}

Resultado de imagen para euler number funny

image taken from http://digg.com/video/eulers-constant-explained


Quiz 8 with Recursion (MT 16)

--Originally published at TC1017 Programing Curse

Is fibonacci with the recursion method, i  had some help from the bookrecursion.PNG

#include <iostream>
using namespace std;

int fibonacci (int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return fibonacci (n-1) + fibonacci (n-2);
}
}

int main ()
{
int i, n;
cout << “give me a number ” << endl;
cin >> n;
for (i=0; i<n; i++){
cout << fibonacci (i) << endl;
}
return 0;
}

Resultado de imagen para fibonacci

image taken from: http://www.zurditorium.com/la-sucesion-de-fibonacci-y-los-numeros-primos


WSQ#9, MT 19,21

--Originally published at TC1017 Programing Curse

This homework ask me to create a program that prints something that is in a .txt, it was a little difficult to understand how to do it but at the end after investigate it was actually easy

and also here we can see Mastery Topic 19, and 21

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string game, name;
cout<<“Tell me which game do you want to know its history “<<endl;
cout<< “(FallOut, Borderlands, Doom)”<<endl;
cin>>name;
if (name==”FallOut”){
ifstream file(“FallOut.txt”);
if(file.is_open())
{
while(getline(file,game))
cout<<game<<endl;
}
file.close();
}
else
{
cout<<“Try Later “<<endl;
}
}
else{
if(name==”Borderlands”){
ifstream file(“Borderlands.txt”);
if(file.is_open())
{
while(getline(file,game))
{
cout<<game<<endl;
}
file.close();
}
else
{
cout<<“Try Later”<<endl;
}
}
else{
if(name==”Doom”){
ifstream file(“Doom.txt”);
if(file.is_open())
{
while(getline(file,game))
{
cout<<game<<endl;
}
file.close();
}
else
{
cout<<“Try Later”<<endl;
}
}
}
}
return 0;
}

i just have to convert this into a fucntion.

WSQ9.PNGWSQ9.1.PNG

Resultado de imagen para fall out game

image taken from: http://kotaku.com/lets-rank-the-fallout-games-best-to-worst-611408965


Mastery Topic 15

--Originally published at TC1017 Programing Curse

This program do a piramid with T, ask the user the range and print the piramid, this program use nested fors.

#include <iostream>
using namespace std;

void triangles(int a)
{
for(int i = 1; i <= a; i++)
{
cout << endl;
for(int k = i; k > 0; k–)
{
cout << “T”;
}
}
cout << endl;
for(int i = a-1; i > 0; i–)
{
for(int k = 0; k < i; k++)
{
cout << “T”;
}
cout << endl;
}}

int main()
{
int triangle;
cout << “Dame la cantidad de triángulos que quieres que aparezcan ” << endl;
cin >> triangle;
triangles(triangle);
return 0;
}

Nested.PNG

Resultado de imagen para Piramide

image taken from: http://respuestas.tips/cuales-son-los-tipos-de-piramides-geometricas/


WSQ #7

--Originally published at TC1017 Programing Curse

This time i have to create a program that asks the user for 10 numbers, and store those numbers in a list. Show to the user the total, average and standard deviation of those numbers.

Also we can see mastery topic 18 here, the use of list, http://www.cplusplus.com/doc/tutorial/arrays/ this page helped me.

Resultado de imagen para listas c++

image taken from http://blog.martincruz.me/2012/11/unir-dos-listas-simples-circulares-en-c.html

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

float getTotal(float numbers[], int numNumbers){
float answer=0.0;
for(int i = 0; i < numNumbers; i++){
answer = answer + numbers[i];
}
return answer;
}

float getAverage(float numbers[], int numNumbers){
float answer=0.0;
answer = getTotal(numbers,numNumbers);
answer = answer / numNumbers;
return answer;
}
float getStdDev(float numbers[], int numNumbers){
float varianza=0.0, answer=0.0, prom=0.0, dev=0.0;
prom = getAverage(numbers,numNumbers);
for(int i = 0; i < numNumbers; i++){
varianza = varianza + pow((numbers[i]-prom),2);
}
dev = sqrt(varianza/numNumbers);
return dev;
}
int main(){
const int NUM_NUMBERS=10;
float x[NUM_NUMBERS];
for (int i = 0; i < NUM_NUMBERS; i++){
cout << “Dame un número “;
cin >> x[i];
}
float total = getTotal(x,10);
float average = getAverage(x,10);
float stdev = getStdDev(x,10);
cout << “El total es: ” << total << endl;
cout << “El promedio es: ” << average << endl;
cout << “La desviación estándar es: ” << stdev << endl;
}

Lista1.PNGlista2.PNG