# 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.     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.

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.   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: 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: 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): (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    # 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      Continue reading "Multipart Data and Files"

# 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     # This is getting hard….

--Originally published at my programming blog

Today I had to do the quiz #8 which was to create a function where I needed to calculate the nth number in the Fibonacci sequence. I had a lot of trouble with this quiz!!!!! I didn’t go to class, so I couldn’t ask to my classmates or to Ken, so I searched for a lot of examples and tutorials of different codes for this program and with what I understood I created my own program.

What I did was:

1. In my function I only declared one value x, which will be the number the user will enter.
2. Then I made an if that if x was equal to something less than 2, then the fibonacci number would be x.
3. Then I declared 3 values, n1, n2 and n, where n1 is 0 (the first fibonacci number) and n2 is 1 (the second fibonacci number) and n the total value of the fibonacci number we will calculate.
4. Then I did a for loop and as you can see on the code I equal n to n1 + n2, n1 equals n2, and n2 equals to n (This are the operations to obtain the fibonacci number).
5. And the return value was n.
6. The in my int main I asked the user for the value of a number in the fibonacci sequence which is the integer value.
7. Then to print the number I called the function but I changed the inside of the parenthesis to the int value so that the fibonacci number can be calculated based on the value the user entered.
8. And that’s it.

I hope you understand it! If not here are some examples of different codes and a tutorial that can give you an idea to create your own code. Also check the work of our classmates in    Continue reading "This is getting hard…."

# Quiz #5

--Originally published at my programming blog

The quiz of this week was very tricky, I needed to do the first 5 exercises in this page, at first I was a little bit confused with the first two exercises, but once the teacher explained me it was that hard.

1. For the first exercise I tested the code and tried to correct it based on the errors the terminal gave me, then thanks to Ken and the page he gave us, I was able to correct the program completely. 2. For the second exercise I asked the user for an integer, a character and a float, and I made an integer called x that would be equal to the integer/character/float converted. So what I did was to equal x to “(type) a” where a= to the value that the user enters (the int, float or char). I did this before each message that the program prints, changing the value of x each time. I learned this method thanks to this page. 3. In the third exercise what I needed to do was to make a program that could print this: What I did was to do it from scratch, counting spaces etc. It was a bad idea!! Because I wasted a lot of time and when I finished  Ken told the class that by copy pasting it it would be very easy, which made me mad because I didn’t think about that. 4. For the fourth exercise needed to ask the user for three numbers and then print those numbers but in ascending order. What I did was well first to ask the user for the three numbers, then I did nested ifs, for example: My first if’s condition was if a>b and inside that if I entered another if with the condition if b>c and then      # On to functions!

--Originally published at my programming blog

What we needed to do in this assignment was to use the first program we did and make functions ( like the addition, difference, division, etc.).

This assignment was easy because I only needed to do simple functions.

1. So the first thing I did was to make my functions, I named them Sum, Difference, Product, Division and Remainder.
2. Then in the parameters I included the integers a and b.
3. And in the return value I put (a+b), (a-b), (a*b), (a/b) and (a%b).
4. Then I made the int main
5. Then I asked the user for two integers and then I printed the results of each function.

Here is my code:

#include <iostream>
using namespace std;

int Sum(int a, int b){return (a+b);}
int Difference(int a, int b){return (a-b);}
int Product(int a, int b){ return (a*b);}
int Division(int a, int b){return (a/b);}
int Remainder(int a, int b){return (a%b);}

int main()
{
int a, b;
cin>>a>>b;
cout<<“The sum of these integers is: “<<Sum(a,b)<<endl;
cout<<“The difference between the integers is: “<<Difference(a,b)<<endl;
cout<<“The product of the integers is: “<<Product(a,b)<<endl;
cout<<“The division of the integers is: “<<Division(a,b)<<endl;
cout<<“The remainder of the division of the integers is: “<<Remainder(a,b)<<endl;
return 0;
} When I run it, it gives something like this: Thanks for reading! I hope it helps  # Sum of numbers

--Originally published at my programming blog

This week the activity I needed to do was create a program that would ask the user for two numbers, the lower bound and the upper bound, and the program would give the user the sum of all the numbers from the lower bound to the upper bound.

I had some trouble with this because I was a little confused because every program from my classmates or on the internet were different so when Ken explained it in class I understood.

So what I did was:

1. Named 3 integers, x,y and sum.
2. I equaled sum to 0.
3. Then I asked the user for the lower and upper bound.
4. I used a while loop so if the user gave me a higher number in the lower bound the program would ask the user to enter the numbers in the right order.
5. After that I used a condition where if the integers where in the right order there would be a for loop.
6. In the for loop is like instructions, it is stated that while the int i that is equaled to x is less or equal to y the second number , i will increase.
7. And then I equaled sum to i.
8. Finally I printed the sum of the range.

The code:

#include <iostream>
using namespace std;
int main ()
{

int x, y, i, sum=0;
cout<< “Let’s to the sum of all the numbers in a range.”<<endl;
cout<< “Please give me the lower bound: “<< endl;
cin>> x;
cout<< “Please give me the upper bound: “<<endl;
cin>> y;

while (x>y){
cout<<“Please enter the numbers in the right order (from small to big).”<<endl;
cout<< “Please give me the lower bound: “<< endl;
cin>> x;
cout<< “Please give me the upper bound: “<<endl;
cin>> y;
}

if(x<=y){
for(int i=x; i<=y;    