Estimating e

--Originally published at my programming blog

So what we needed to do was a function that had just one parameter and to estimate the value of e.

I got help from this page and this video tutorial.

What I did was:

  1. I did first a float function where I calculated the factorial.
  2. Then In the second function my para meter was precision which is the number of decimal points I want in my result.
  3. Inside the function I only did a for loop where the condition was that while the int i was smaller or equal to precision this would happen : e will be equal to e plus 1/factorial of i.
  4. Then the function will cout (BUT IT WILL NOT PRINT ANYTHING) first it says fixed, which I used to start counting the precision form the decimal point, and the I used the function setprecision from the library “iomanip”. and at the end my function will return the value of e.
  5. Finally in my int main I just ask the user for the precision and then I call my functions with parameter that my user gave me.

Screen Shot 2017-05-02 at 12.16.57 AMScreen Shot 2017-05-02 at 12.17.07 AMScreen Shot 2017-05-02 at 12.16.39 AM

My code in Github: https://gist.github.com/mariasantoyodl/2cb288d17ecc951b8349e226d651a36a

 


SciLab

--Originally published at my programming blog

The last WSQ was to use SciLab for data analysis and to display data for example in charts.

This time I’m not going to explain each thing step by step because there is a pdf that explains everything I will do.

you can make a lot of things like: Obtain the sine, cosine, logarithm etc. of a number, also you can make functions which start with “function” and end with “endoffucntion” and it is very similar to c++.

So this is an example of what I learned with SchiLab of data analysis:

Screen Shot 2017-05-01 at 11.31.23 PM

Here I did an analysis if the vectors had equal values, I did a sequence from 20 to 10 where each time there is a -2 (is a loop) and I obtained the value of e/2.

Example of a graph:

Screen Shot 2017-05-01 at 11.39.11 PM

I graphed this curve and the linspace is where you put values where the value of x is the smallest, the biggest and the number of values calculated  between them.

 


Babylonian method

--Originally published at my programming blog

So for this WSQ I needed to make a function that asked the user for a number and then return a floating point in which it calculated the square root of the number the user entered, but the square root needs to be calculated with the Babylonian method.

First, here’s a picture of the Babylonia method (so  you can have an idea of what I did in my code):

Screen Shot 2017-04-05 at 12.45.02 PM

(Obtained from Wikipedia)

Also check this tutorial, it helped me a lot!

So this is what I did:

  1. I included the cmath library because I’m also going to print the square root of the number to check if the code is correct.
  2. I made a function (a float) called SquareRoot and in the parameters I included the double x (which will be the number the user will enter in the int main).
  3. Then I made a double called Error and I equaled it to 0.0001.
  4. Then for the Babylonian method you need to have a reference number, so I declared the double y which I assigned to the value of the double x.
  5. Then I did a while loop that while the double y minus x over y is bigger than the error I declared earlier this would repeat: I wrote the formula for the Babylonian method, that is that y will be equal to y plus x over y and that answer over 2. (This will be an intermediate result of the square root).
  6. Then once the condition of the while loop doesn’t happen the function will print “The final result is” and then return the value of y.
  7. Finally in the int main, I ask the user for a number, which is the declared double x and then call the function.
  8. After all this I print
    Screen Shot 2017-04-05 at 12.41.57 PM
    Screen Shot 2017-04-05 at 12.29.18 PM
    Continue reading "Babylonian method"

quizz8 #quizz8

--Originally published at ISRA'S BLOG :D

The fibonacci numbers work as the same activity as we did for the factorial numbers, but there are few differences in the program. First of all i recomend you to check the secuence of the fibonacci numbers, and after that a recomend you to use a function and recursion to make make your program smaller.

i implement a for command to print the fibonacci numbers until 10, so you can see how my program works:

IMPORTNAT: Using a function will help you to reduce the size of your program, and also it is less complicate:

I implement the two ways to obtein the secuence of the fiboncci, the first one with the number 1, when 0 and 1 are equal to 1, and the other way is when the number is 0 or 1 it prints the real number that we type.

fibonacci

fibonn

results:

fibo3

fibo4

 

 


Helllo world :D

--Originally published at ISRA'S BLOG :D

This is my first using c++.

Hello world is the first program that we did in the c++ language, when i saw it the first time, i did not understand the structure of the program, and then i started to worry about. But after that i said, to myself, dont worry you can do it, and then i started to anylised the different commands that the program had, and because i had remedial 1 last semester, i saw that they were the same as programming in C#. So finally i understand the semantic and sintaxys of thee program.

IMPORTANT: REMEMBER THE STEPS THAT OUR TEAHCER TOLD US

  • SAVE
  • COMPLAIN
  • RUN

DON’T HAVE THE SAME MISTAKE AS MINE, YOUR PROGRAM WOTN RUN IF YOU DON’T FOLLOW THE STEPS.

TIP: DONT GET FRUSTARED IF YOUR PROGRAM DOEN`T RUN, RELAX AND TRY AGAIN, ALSO READ THE BOOK, IT IS VERY USUFULL :))))))))))))))))))))

  • captura3captura

The list #WSQ07

--Originally published at ISRA'S BLOG :D

 

First of all i recomend you to read the chapter 10  that its about vectors and arrays, if you follow the lecture and do the excersise you will understand better what you need to do for this program.

Don’t copy the program of the book, try to do it by yourself, it is very easy to use.

Well, lets begin.

For my code I implement some many useful tools like functions, for commands and vectors. Vectors were very important because they help me to reduce some actions of my code, the also helpme to add new modification to my program.

Once the program its over i decided to implement the operations to calculate the standard deviation and the avarege, the avarage was inside the for command because this will change it when the user type the list that he wants, so i don’t need to asing a new value for this operation.

An important thing about vectors, is that if you work with them you don’t need to modify all your program if the vector change, this is very important beceuse they are very easy to use and more efective than implement a function that ask for ten different values. Also that was the purpose of the activity, creat a list by using vectors and see the different functions that they have in the program.

VECTORS CAN BE USED FOR THE CREATION OF LISTS.

REMEMBER: BE CAREFUL WITH THE FOR COMMANDS THAT YOU USE, ALSO CONSIDER THE SIZE OF THE VENCOTR, FINALLY DON’T FORGET TO ADD THE LIBRARIES OF MATHEMATICAL OPERATION <CMATH>.

THIS VIDEO COULD HELP YOU TO UNDERSTAND BETTER THE VECTORS:https://www.youtube.com/watch?v=SGyutdso6_c

THIS IS MY PROGRAM:

LIST

 

 

LIST2

RESULT:

LISTT


Multipart Data and Files

--Originally published at my programming blog

The WSQ of last week was to make a program that could read a file and return how many lines and how many characters does it have.

This links were very useful for the use of structs, how to count the lines in a file and to count how many characters .

So what I did was:

  1. I included the fstream library ,so that I can read the file, and the string library so that I can use the getline function.
  2. Then I created my struct, I named it Results and I declared my two integers that my function will return at the end, which are sum (the number of characters) and number_lines (number of lines).
  3. Then I created my function and I wrote Results (to make reference to my struct) and then I named it printResults and in the parameters I wrote Results(the struct)”&” w, which means that any variable that has “w.” will be directed to the struct.
  4. I declared my variables that the function will return (sum and number_lines).
  5. Then I declared the ifstream “file” and the name of my file in parenthesis which is “TextFile.txt”.
  6. Then I did an if loop and the condition was that if the file was open then i would do the following:
  7. A while loop and in the condition I included a getline (function that counts the number of lines) and inside the getline the parameter are the ifstream file and the string line.
  8. Then inside that loop I did number_lines++, which means that it will give me how many lines by repeating the loop while there are more lines.
  9. Then I declared an integer called NumOfChars and I assigned it to the length of the string line (line.length()) (length is another function of the  string library).
  10. Then I
    Screen Shot 2017-03-16 at 10.38.40 AM
    Screen Shot 2017-03-16 at 10.03.01 AM
    Screen Shot 2017-03-16 at 10.33.55 AM
    Screen Shot 2017-03-16 at 2.08.26 PM
    Continue reading "Multipart Data and Files"

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"

Foctorial numbers

--Originally published at Solving problems with programming

This homework was a little bit more dificult for me at the biggining, i didn’t understand what was a recursion and how did they work. I wrote it just because a web page said how, but I didn’t understand how it worked. After analizing it finally i understood .

A recursion is the call of the same function that is being defined at that moment. You can define a function and at the same time use it inside that function. It is confusing to understand and to explain also hahaha, but in the code and by doing the exercise you could understarnd it.

int factorial(int n){
int resultado;
if(n>0)
resultado=n*factorial(n-1);
else if(n==0)
resultado=1;
return resultado;

I will simulate an exucution of this function. If n=2, n is bigger than 0 so, we will multiply n by the factorial of n-1 that equals 1. So the factorial of 1 is  1 multiply by the factorial of 1-1 that equals 0. So the factorial of 0 is 1, because that is said on the function, if(n==0) resultado=1. So 2*1*1 gives 2.

Here is all the code

#include <iostream>
using namespace std;
int factorial(int n){
int resultado;
if(n>0)
resultado=n*factorial(n-1);
else if(n==0)
resultado=1;
return resultado;
}
main(){
int num, res;
char op;
do{
cout<<“Escribe el número que desea convertir en factorial”<<endl;
cin>>num;
res = factorial(num);
if(res==0)
cout<<“No se puede sacar el factorial a numeros negativos “<<endl;
else{
cout<< “El número factorial es “<<res<<endl;
}
cout<<“¿Quieres calcular otro número factorial?(S/N)”;
cin>>op;
if(op==’N’||op==’n’)
cout<<“Ten un lindo día”<<endl;
}while(op==’S’||op==’s’);
return 0;
}

I also solve the factorial number with loops do while and for, that are easier to make. The code is linked if you like to take a look.


Lists!

--Originally published at my programming blog

The assignment of last week was to ask the user for 10 values and then calculate the standard deviation with those values, then you needed to change your program so that the user could tell you how many values he wanted to enter.

It was easy to calculate the standard deviation when you ask the user for 10 values, but when I wanted the user to tell me the size of the array I had a lot of problems.

This is what I did:

  1. First I did my function and in the parameters I included the array and the integer size (which is the size the user will enter).
  2. Then I declared my values which were floats, sum=0, average, sum2=0 and SD
  3. Then I did a for loop where while i was less than the value of size there will be a sum of values.
  4. Then that sum that was calculated I divided it by the integer size to calculate the average.
  5. Then I did another for loop where when i was less than the size, then there will be a sum2 that will be equal to the sum of the  first value entered minus the average and that to the power of 2, (so you need to include the cmath library).
  6. Then I equaled SD to the sum2 divided bye the size of the array, and my function will return SD.
  7. In my int main I declared the integer size and I asked the user for the value of size.
  8. Then I declared my array with the value inside the brackets “size”. So that the program knows that the size of my array is equal to the size entered by the user.
  9. Then I asked the user for the values and I did a for loop where while i
    screen-shot-2017-03-06-at-10-39-48-am
    screen-shot-2017-03-06-at-10-40-01-am
    screen-shot-2017-03-06-at-10-40-58-am
    Continue reading "Lists!"