Bananas Bananas Bananas

--Originally published at my programming blog

What I needed to do:

Write a function called find_bananas which receives a single parameter called filename (a string) and returns a positive integer which is the number of times the word (string) “banana”  (or “BANANA” ) is found in the file. The banana can be any case (‘BaNana’ or ‘BANANA’ or ‘banana’, etc) and they can be “stuck together” like “banAnaBANANA” (that counts as two). Create your own test file (plain text) to check your work.

Thanks to this tutorial and this post.

So what I did was:

  1. I imported the library of fstream
  2. In the first function I did a for loop where if i is smaller than the length then with the function to lower the program will change all the letters to lower case.
  3. I also did an infinite loop (That’s why it has a 1 as a condition) this loop will only stop if you break it and in this I find the number of bananas of each line, and the program saves that info.
  4. Then at the end there is a break to end the infinite loop
  5. This function will return the number of bananas in each line
  6. Then in the next function I open the text file and read it and I made a counter that counts the accumulating bananas in each line of the text
  7. Finally in the int main I have a final counter and this will use the last functions and the return values will be returned.

Screen Shot 2017-05-02 at 1.34.20 AMScreen Shot 2017-05-02 at 1.34.28 AMScreen Shot 2017-05-02 at 1.34.37 AMScreen Shot 2017-05-02 at 1.34.46 AM

Screen Shot 2017-05-02 at 1.34.01 AM.png

My code in Github: https://gist.github.com/mariasantoyodl/31c998d729a8e608a0abfe31e59cf8e5


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.

 


My final project

--Originally published at my programming blog

So at the end we didn’t do a program that could catch the movement of a hand (my last post about my final project), because we didn’t found the sensors, so we decided to do a little robot, we obtained the instruction to make it here. (in this link you can find the instructions, materials and the code).

First we ordered the circuit online and we found a code and we understood and changed some things.

Then once we have the circuit and the micro servo motors, we attached everything together and we connected the robot to the computer and we run the program, the final result was that robot danced.

18176144_10212929911225526_27241085_o

The robot dancing:

 


YO SOY 196

--Originally published at my programming blog

For this assignment I needed to do program that asked the user for a range of numbers and it would print the number of lychrel, non lychrel and palindrome numbers.

Everyone was afraid of this WSQ, myself included, so I asked for help to Fabrizzio and Ken, and I saw different codes so that I could have an idea.

So this are the tutorials and codes I saw : tutorial, tutorial2, code1, code2

Steps:

  1. First include the BigInteger library.
  2. If you use the template Ken gave us, you just need to fill the functions and do the int main.
  3. In the bool function is_palindrome I have the parameter the big integer “n”, inside that functions I use the function of the big integer library where I turn the big integer n into a string called “a”.
  4. Then in the string library I use a function to change the order  of the spring backwards and then I convert it again to a big integer. I made an if so if the string backwards is the same as the original one, then the function would return true, else it would return false.
  5. In the BigInteger function called apply196 I have as a parameter the Big Integer n, then I do the same that I did in the bool function, but now at the end I sum the big integer backwards and the original and return the result.
  6. Then in my int main I declared my variables, the counters for each type of number, the upper and lower bound, and the Big Integer Final.
  7. So once I’ve asked the user for the upper and lower bound, I did a for loop where while the lower bound was equal to i and i was smaller or equal to the upper bound
    Screen Shot 2017-04-17 at 12.29.36 PM
    Screen Shot 2017-04-17 at 12.29.59 PM
    Screen Shot 2017-04-17 at 12.30.09 PM
    Screen Shot 2017-04-17 at 12.32.33 PM
    Continue reading "YO SOY 196"

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"

My final project

--Originally published at my programming blog

This blog post is about my final project, my team is made of two other students, and we decided we are going to make a glove with flex sensors that when connected to an Arduino and the computer will show the movements that are being made with the glove with the program that we will do.

We also have another idea, but right now I’m going to talk about this one.

We found this idea in a page of projects made by Arduino, there were a lot of ideas and we chose this idea. The idea was to make a robotic hand, but a teacher recommended us that first we should only do the glove with the flex sensors and with an Arduino and the computer, see if the program registers the movements of the glove.

Right now we have some resistors and the Arduino, and first to test it we did a program were a led would turn on. And we found a store where we can find all the materials, like the flex sensors, Xbee module, etc.

This is the original project and the the student that made it:

739333_20141207_gabriele-santin

(Obtained from Gabry295) https://www.facebook.com/Gabry295

 


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"

Miracle!!

--Originally published at my programming blog

Miracle-Ahead.png

( Photo credit to: Natalie Nichols)

Today I finished the quiz in the class!! Normally I finish the quiz during the day because I need to search for tutorials on how to do a code related to the quiz, or because I am completely lost! But this time with Ken’s explanation that the coordinates made a triangle I understood perfectly, also thanks to my project team that explained me the idea to make the code.

So the quiz was to ask the user for two coordinates (x1, y1, x2, y2) and then make a function that returns the distance between those two coordinates.

So first:

(I included the library cmath, to use the sqrt and the power function)

  1. The coordinates make a triangle! the only thing you need to do is calculate the hypotenuse of the triangle with the Pythagorean theory.So remember the the formula is c^2= a^2 + b^2
  2.   So I made my function, I called it Distance (float) and my parameters were the floats x1, y1, x2, y2.
  3. Inside my function I declared two integers distancex and distancey, distancex was equal to   the difference between x2 and x1 (The value of “a” in the Pythagorean formula) and that I raised it to the power of 2, I did the same but now with the values of y (which will be equal to the value of “b” in the formula).
  4. Then I declared a float named total_distance which was equal to the square root of distancex + distancey.
  5. And my function returned the value of total_distance.
  6. In my int main I declared the variables floats (x1, y1, x2, y2) and asked the user for the coordinates and then I printed the answer by calling the function Distance.

Here’s my code:

#include <iostream>
#include <cmath>
using

Screen Shot 2017-03-09 at 10.27.17 AM
Screen Shot 2017-03-09 at 10.18.50 AM
Continue reading "Miracle!!"

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!"