Review del semestre

--Originally published at Solving problems with programming

A mi me gusto mucho la clase de programación, aprendí muchas cosas y aprendí a buscar soluciones a cualquier problema en internet. Está muy padre el el sistema en que no hay calificaciones para las tareas, esto hace que no hagas las tareas solo por entregarlas o tener la necesidad de copiar, esto te permite decidir hacer las tareas por aprender. También aprendí que si te esfuerzas por tu cuenta hasta conseguir la respuesta o esforzarte antes de que te den las respuesta, es mucho más fácil recordar las cosas y aprender. Estos son los aspectos principales que me gustaron de la clase, excelente semestre ?


Calculando e

--Originally published at Solving problems with programming

Para este WSQ lo que necesitamos es la libreria <iomanip> que lo que es es establecer cuantos decimales se van a establecer. Ademásde esto necesitamos la formula de e que es  una serie  e=e+1/n!. Con esta información podemos hacer nuestro código

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

float nfactorial(int n){
 float fact;
if(n==0){
return 1;
}
else {return n*nfactorial(n-1);}
}

float calculae(float decimales){
 float e=1;
 for(int i=1; i<= decimales; i++){
 e= e + 1/nfactorial(i);
 }
 cout<< fixed << setprecision(decimales);
 return e;
}
main(){
 int dec;
 cout<<"¿Con cuantos decimales deseas caclcular e?"<<endl;
 cin>>dec;
 cout<<"e= "<<calculae(dec);
}

Gobananas

--Originally published at Solving problems with programming

In this homework what we need to do is count how many times you find the banana in a txt file. It doesn’t matter if the word is in capital letters or lower case. What we need to do is change chang every line of the into lowe case. For that we use the function transfrom from the library algorithm. So then while the function getline gets a line, you will search for the string banana, in every line. If find() finds a banana you will look again adding 6 to the place were find is searching.

Here is the code.

#include <iostream>
#include <string>
#include <ctype.h>
#include <fstream>
#include <algorithm>

using namespace std;

int gobanana(string filename){
 string linea;
 int contador=0,a=0, i=0;
 ifstream file(filename.c_str());
 if(file.is_open()){
 while(getline(file,linea)){
 cout << "checking " << linea << endl;
 transform(linea.begin(), linea.end(), linea.begin(), ::tolower);
 cout << "lower case: " << linea << endl;
 a=0;
 a = linea.find("banana",a);
 while(a !=string::npos){
 cout << "found banana" << endl;
 contador++;
 a = linea.find("banana",a+6);
 }
 }
 }else{
 cout<<"No se pudo abrir el archivo"<<endl;
 }
 return contador;
}
main(){
 string programa;
 int bananas;
 cout<<"Escribe el nombre del programa"<<endl;
 cin>>programa;
 bananas=gobanana(programa);
 cout<<"El número de bananas es "<<bananas<<endl;
}

Babylonian method

--Originally published at Solving problems with programming

What you need to do in this homework is calculate the square root using the babylonian method. To understand what is hte babylonian method i saw this video. It is simple, you use a simple formula. First of all you need an integer aproximation of  the rootsquare with that you can do the babylonian mehtod x=aproximation z=number of the rootsquare you want to calculate rootsquare=(z/x+x)/2. With that formula you get the rootsquare with the babylonian mehtod. To have a more accurate result I use the result of the formula to repeat it as the aproximation again, and do it 30 times.

#include <iostream>
using namespace std;

double babylonian(int raiz, int numero){
 double resultado,aprox,root;
 root=raiz;
 aprox=numero;
 for (int i = 0; i < 30; i++) {
 resultado=(aprox+(root/aprox))/2;
 aprox=resultado;
 }
 return resultado;
}
main(){
 int squareroot,aproximacion;
 double resultado;
 cout<< "Escribe la raíz que deseas sacar"<<endl;
 cin>>squareroot;
 cout<<endl<<"Escribe la aproximacion entera más cercana de esa raíz"<<endl;
 cin>>aproximacion;
 resultado=babylonian(squareroot, aproximacion);
 cout<<"La raíz cuadrada es "<<resultado<<endl;
}


I need to improve this program and calculate the square root without the aproximation, but i don't know how, i will ask in the next class to my classmates

Problem with UBUNTU compiler

--Originally published at Solving problems with programming

I was doing the quiz of distance where you get 2 points, and with that you get the distance of the two points with pitagoras. What happened is that when I tryed to compile it, I couldn’t do it i don’t know why. I asked Ken about my problem and my program in his computer worked, so we got to the conclusion that my compiler was not working correctly, it wasn’t using the latest version of c++ compiler that works with structures. What he told me is specify the compiler to use the latest version.

g++ -std=c++11 filename.cpp

Using that command the problem was solved. I use ubuntu and the compiler for some reason has that problem if you have it also you can solve it like that.


WSQ9 Multipart Data

--Originally published at Solving problems with programming

In this homework what we do is read a .txt file and count how many lines does it has and the total sum of characters. We use structures to just return a structure with the number of lines and characters. A structure is a variable that has many variables inside. To solve this homewor I got help from the blog of Fabrizzio and a tutorial that explains how to read an write on other files from c++.

#include <iostream>
#include <string>
#include <fstream>  //Esta librería es la que permite que puedas leer otros archivos
using namespace std;

struct lector {      //estructura de struct y variables dentro de lector
 int contlineas;
 int caracteres;
 int acumulador;
};

lector lectordoc(string nombre){
 lector contador={0,0,0};
 string linea;
 ifstream file(nombre); //Esta función te permite abrir otro archivo ifstream, escribir es con ofstream. file(nombre) File es el nombre que le asignas al archivo abierto y nombre es la variable del nombre del archivo.
 if(file.is_open()){
 while(getline(file,linea)){
 contador.caracteres= linea.length();
 contador.acumulador= contador.acumulador + contador.caracteres;
 contador.contlineas ++;
 }
 return contador;
 }else{
 cout<<"No se pudo abrir el archivo intentalo de nuevo"<<endl;
 }
}
main(){
string archivo;
lector informacion;
cout<<"Escribe el nombre del archivo"<<endl;
cin>>archivo;
informacion=lectordoc(archivo);
cout<<"El archivo tiene "<<informacion.contlineas<<" lineas y "<<informacion.acumulador<< " caracteres"<<endl;
return 0;
}

 

With this homework we cover #Mastery09. Here is the complete code if you want to download.


Yo soy 196

--Originally published at Solving problems with programming

In this homework what we need to count in a range of numbers how many palindromes are, how many numbers aplying a certain formula convert into palindromes, and how many numbers are lycheral numbers. A palindrome is a number that reading it from left to right and right to left is the same number. A palindrome a nonpalindrome can be converted into one with an easy formula, flip the order of the the values and add it to the orginal value, the result will do a palindrome. Example: 100 + flip(001) = 101. The result of the example is a palindrome so the formula works. There are numbers that even with the formula don’t become palindromes. Those are called lychrel numbers. To identify a lucheral number we apply the formula 30 times, if we already applied the formula 30 times and the number is not a palindrome it is because is a lychrel number.

In this code we use an especial library called BigInteger.hh that it is an aditional library besides the ones that are alredy insalled in our computer. To add it you have to put all the files from the library in the location of you cpp file and use the command make to compile it. Also the including the library you have to use (” “), instead of (<>), you will see it in the code example code.

In this homework we cover #Mastery19 with the use of strings. Strings are also variables and we include the library <string> to use the lenght() function that works to count the number of characters of a string. In this program we count the number of characters to be able to flip a number and apply the formula of palindromes.

Another #Mastery15 that we cover with nested loops. In

Continue reading "Yo soy 196"

Quiz 09 Distance

--Originally published at Solving problems with programming

In this quiz what we did is ask for the user fo 2 points for each it’s x and y coordinates. With the coordinates of the two points we can easily calculate the distance between them. To solve the problem we use strucutres that are variables that can be asigned to them multiple variables, it is like an array.

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

 struct smurf{
 double x;
 double y;
 };

 double distance(smurf p1,smurf p2){
 double dx, dy;
 dx=p2.x - p1.x;
 dy=p2.y - p1.y;
 return sqrt(pow(dx,2)+pow(dy,2));
 return 0.0;
 }

 int main() {
 double resultado;
 smurf punto1,punto2;
 cout<<"Escribe la cordenada x del punto 1 "<<endl;
 cin >>punto1.x;
 cout<<"Escribe la coordenada y del punto 1"<<endl;
 cin>>punto1.y;
 cout<<"Escribe la coordenada x del punto 2"<<endl;
 cin>>punto2.x;
 cout<<"Escribe la coordenada y del punto 2"<<endl;
 cin>> punto2.y;
 resultado=distance(punto1,punto2);
 cout<<"La distancia entre los dos puntos es "<<resultado<<endl;
 return 0;
 }


if you don’t understand structures here is anyway the solution without structures.

#include <iostream>
#include <cmath>
using namespace std;
double distance (double x1,double y1,double x2,double y2){
 double dx, dy;
 dx=x2-x1;
 dy=y2-y1;
 return sqrt(pow(dx,2)+pow(dy,2));
}
int main() {
 double resultado,px1,px2,py1,py2;
 cout<<"Escribe la cordenada x del punto 1 "<<endl;
 cin >>px1;
 cout<<"Escribe la coordenada y del punto 1"<<endl;
 cin>>py2;
 cout<<"Escribe la coordenada x del punto 2"<<endl;
 cin>>px2;
 cout<<"Escribe la coordenada y del punto 2"<<endl;
 cin>> py2;
 resultado=distance(px1,py1,px2,py2);
 cout<<"La distancia entre los dos puntos es "<<resultado<<endl;
 return 0;
}

 


Mastery Topics

--Originally published at Solving problems with programming

For purposes of the class, we need to prove our learning adquired during the course of specific topics. Down here i’ll list all the mastery topics and the link of the post that treats them.

  1. Use of comments
  2. C++ Good Style coding conventions
  3. Basic types and their use
  4. Basic output (print) X
  5. Basic user input (text based) X
  6. Calling functions
  7. Creating functions
  8. Importing and using libraries
  9. Creating and using your own libraries (program with multiple files)
  10. Use of the conditional “if”
  11. Use of “else” with a conditional if
  12. Nesting of conditional statements (ifs inside ifs)
  13. Use of loops with “while” and “do while”
  14. Use of loops with “for”
  15. Nested loops
  16. Use of recursion for repetitive algorithms
  17. When to use what type of repetition in a program
  18. Creation and use of Arrays/Vectors in C++
  19. Creation and use of strings
  20. Validated user input (ensure correct/expected data entry)
  21. Reading and writing of text files
  22. Matrixes and Vectors
  23. Data analysis with tools (to be determined which tool, most likely SciLab)
  24. Visualization of data with tools

I will mark with an x all the topics that have been covered.


WSQ07 Lists

--Originally published at Solving problems with programming

In this homework what we had to do is ask for the user for 10 numbers, save them, display them and with all of them calculate the avegrage and the standart deviation. To start with we need to use an array. An array is a variable that can save multiple variables. How they work is like the other variables , you need to specify that is a variable.

float array[10];

This means that the variable in the array are going to be floating point and the [10], means that the array is going to consist of 10 variables, you can save 10 numbers in that array.

To fill data in the array, you do it like with any other variable.

 cin&gt;&gt;array[0];

But in the arrays you need to spicify wich variable of the 10 spaces you created, you are going to save. In This code i’m saying that i’m gong to enter variable of the first spot. In arrays you start counting the spots from 0. Being that have 10 spots y can enter variables from the spot 0 to the spot 9.

So at the beggining of the program i wil ask for 10 variables. I’m going to use a for loop to enter all these variables in my array. The key here is to change the spot of what i’m entering to my array, every repetition i’m going to enter the next spot available in my array.

 
for(int i=0;i&lt;10;i++){
 cout&lt;&lt;"Escribe tu valor "&lt;<i+1<<endl;
 cin&gt;&gt;array[i];

The other important information we need for this program, is to calculate the sanadart deviation. I didn’ what it was but in this page I understood it excellent. To get the standart deviation you also need arrays, but I’ll let you think how to do it. Anyway if there is any problem I let

Continue reading "WSQ07 Lists"