Multipart Data & Files

--Originally published at Loading…

Hi!! Is almost the ending of this semester and I promised not to leave everything to the last minute… but here I am… doing all in the last minute.

Today I’m going to explain how I did the #WSQ09. To make this program I asked for help to my friend Jenifer Romero, she explained me everything, but the problem was that we did it on her Mac, so… I had to make some arrangements in order for it to work in my Windows computer.

This is what we had to do:

So for this assignment I would like to see you create a function that receives as parameter the name of a file (this would be a string value like data.txt) and your function counts the number of lines and the number of characters in the file which it returns as a single value (but with two values). You will want to look at how to create/define and return a struct value from a function and how to open and read text files line by line

The first thing I did was to add the two necessary libraries <fstream> & <string>Then, for make the “it returns as a single value (but with two values)” part, I added a struct that I called datos, which stores two int, l (for lines) and c (for the characters). Next, in my function (func) which receives the string url (or ruta) and called the struct. After that I wrote ifstream archivo(ruta.c_str(), ios::in) it associates it with the file name and opens it. I established my variables (at the final I don’t use the char car, ‘cause Ken helped me to do the same with less), and wrote a while for do the things until the

Data01
Data02
Data03
Continue reading "Multipart Data & Files"

#WSQ12

--Originally published at OlafGC / Class #TC1017

This is a video that will help you calculate euler’s number, which is an irrational number… I’m having a little trouble with the function because it’s totally ignoring the precision (which is the number that the user gives, but it does calculate euler’s number correctly.

Here is the code:

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

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

float calculate_e(float precision)
{
float e=1;
float previous;
int i=1;

do
{
previous=e;
e=e+1/(factorial(i));
cout<<e<<endl;
i++;
} while(abs (e-previous)>precision);
return e;
}

int main()
{
float precision;
cout<<“How many decimals of accuracy do you want? “;
cin>>precision;
cout<<“The estimation of e with “<<precision<<” decimal points is: “<<calculate_e(precision)<<endl;
return 0;
}


Dovahkiin, Dovahkiin, naal ok zin los vahriin.. (#WSQ09)

--Originally published at Programming Path

The assignment of #WSQ09 was the next one:

So for this assignment I would like to see you create a function that receives as parameter the name of a file (this would be a string value like data.txt) and your function counts the number of lines and the number of characters in the file which it returns as a single value (but with two values). You will want to look at how to create/define and return a struct value from a function and how to open and read text files line by line

This for sure was a new topic. It was thanks to Ken’s Disciple 01‘s blog that I could understand this task.

First I needed to make a text file (.txt) so I wrote a Skyrim quote. It is from an ancient dragon and I got attached to him because it was the only friendly dragon, but anyway here is the text:

skyrim

Then we had to make the program open the file and count how many characters and lines does it has. Then print it out.

WSQ09

Thanks for reading.

 


Survey! (Quiz week 14)

--Originally published at Programming Path

For this quiz we had to do an evaluation of our teacher Ken. I really want to thank him for the new experience he gave us of his way of teaching. I actually like that method because it helps me with my will force. Not only that, it helps me realize how responsible I am.

Thanks Ken Bauer.

It’s still not over, but I will give my best.

I want to share the song I am listening right now. It is The Bay from Metronomy.


Lots of Problems (Quiz week 9)

--Originally published at Programming Path

This quiz was veeeery long. There were 10 problems, but we only had to do 8 problems.

1. Escribe el función distancia cual recibe 4 números (x1, y1, x2, y2) cuales representan dos puntos en espacio (x1,y1) y (x2,y2). El método debe regresar la distancia entre los dos puntos. Recuerda que el valor cuadrada del hipotenusa del triangulo es igual que la suma de las cuadradas de los otro dos lados del triangulo (the hypotenuse squared is equal to the sum of the squares of the other two sides).

Quiz9

2. (5 puntos) Escribe un función que se llama triangulo cual recibe un parámetro size y imprime un triangulo derecho como el siguiente. El renglón mas grande debe llevar size numero de “T”. SOLO imprime los “T”s y los endlines. Nota que no hay characteres (espacios) a la derecha de los T’s. Debe usar un ciclo “for” para controlar el repetición.

PQ2

 

3. Escribe la función factorial cual recibe un entero “x” y regresa el valor de x! Recuerda que 0! = 1, 1! = 1, 2! = 2, 3!= 6, 4! = 24, etc. Para los de Python: NO PUEDES usar el factorial como parte del module “math”

PQ3

4. Escribe una función que se llama promedio_lista que recibe un parámetro (una lista (Python) o arreglo/Vector de C++) de valores float y regresa como float el promedio de los números en la lista.

PQ4

5. Escribe una función que se llama smallest_of_four cual recibe cuatro valores float como parametros y regresa el minimo (más pequeño) de los valores. Ojo: puede recibir unos valores iguales.

Click to view slideshow.

6. Escribe una función que se llama fibonacci cual recibe un número n (puedes dar por cuenta que valor mayor o igual que cero) y regresa y valor correspondiente del serie de fibonacci,

PQ6
PQ7.png
PQ10
?
Continue reading "Lots of Problems (Quiz week 9)"

Distance (Quiz week 9)

--Originally published at Programming Path

The assignment of the quiz was to ask for two coordinates and calculate the distance between them. It needed to have floats and a function as well.

This problem was very easy, because in school they made me calculate the distance a lot of times. I only add the math library (<cmath>) because I elevate to the power and resolve a square root.

Here is the code:

Quiz9

Thanks for reading.


#WSQ10

--Originally published at OlafGC / Class #TC1017

Hi guys! It’s me again with a video of how to get the square root of a number using the old Babylonian method. The code and the link to the video are bellow:

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

float babylonian_square(float x)
{
float ans, a=0;
ans=x/2;

do {
cout<<ans<<endl;
a=ans;
ans=(ans+x/ans)/2;
} while(abs (a-ans)>0.0001);
}

int main()
{
cout<<babylonian_square(2);
}


Quiz

--Originally published at Loading…

Hi! I know it pass a long time without posting anything, sorry I’m in that point of the semester trying to save it.

And I know maybe this post would be more useful the last week, before the partial, but… I hope it could help you.

1. Distance again.

Actually I’ve already explain this code, and I do the same in this quiz, so I’m not going to explain this one, but here is my code:

Quiz11.1

2. T’s pyramid

This one was kind of difficult because one of my functions was wrong, but technically  it’s veryyyy easy.

I only use the library <iostream>, but I used two functions. The first one was for print the T in each line, for this I used an if and a for, which simply is adding one T in each line of the pyramid. The second function is for print how many lines must have the pyramid, so I use two for, one to increase f (fila) while it’s smaller than n(the number that the user enter). And the other one to decrease f, when it is the same as n and until is 1.

Quiz11.2

3. n!

This one I’ve already explained too, so like the first one, I’m not going to explain it. The only difference is that I made this code more simply like the one that is in the link.

Quiz11.3

4. Average list

This one was kind of easy. First I established the libraries <iostream> and <cmathand as always put the using namespace std;. Then, I created a float function named promedio_lista that receives the sum and returns the prom. Next, in my int main I established two kind of variables, in the float I put the array cal[7] (this ‘save’

Quiz11.4
Quiz11.5.1
Quiz11.5.2
Quiz11.6
Quiz11.7
Quiz11.10
Quiz11.p1
Quiz11.p2
Quiz11.p3
Continue reading "Quiz"

#WSQ09

--Originally published at OlafGC / Class #TC1017

Hi guys! I just finished the video for WSQ09, the link and the code are bellow. A brief explanation: this WSQ is about Multipart data and Files. This program opens a text file and tells you the number of characters and files in it. Enjoy and thanks in advance for watching.

And the code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct charlines
{
int chars, lines;
};

charlines countCharLines(charlines & num)
{
string line;
num.lines=0;
num.chars=0;
ifstream file(“data.txt”);
if(file.is_open())
{
while(getline(file, line))
{
num.lines ++;
num.chars += line.size();
}
file.close();
}
return num;
}
int main()
{
charlines number;
countCharLines(number);
cout<<“File chars: “<<number.chars<<endl;
cout<<“File lines: “<<number.lines<<endl;
return 0;
}