WSQ10 – Babylonian Method

--Originally published at BRENDA

This assignment required me to create a function that calculated the squared root of a number using the Babylonian Method.

I created a function that received as a parameter the number, and an inital guess the user made.

My function was like this:

float squaredroot (float number, float guess) {
float babylonian=0;
do {
babylonian = guess;
guess = ((0.5)*(guess+(number/guess)));
} while (abs(babylonian-guess)>0.0001);
return babylonian;
}

Here is my code:

https://github.com/brendaruizt/TC1017/blob/master/babylonian.cpp

babylonian.PNG


WSQ09 – Data and Files

--Originally published at BRENDA

For this task, I covered the topics of strings. I also decided to learn how to do a structure, which is a way to create a function that returns two values instead of just one.

I had to include this two libraries:

#include <fstream>
#include <string>

The way I wrote my code so that it could read a file is as following:

struct structure counttext (string name) {
fstream file;
file.open(name.c_str());

I used (name.c_str()) to be able to read the string name which is in fact an array of characters.

A structure is done this way:

  1. Define the name of the structure and the values it will return:
    struct structure {
    int chars, lines;
    };
  2. Create a function with the structure:
    struct structure counttext (string name) {
    }
  3. Define the two values it will return with the function created:
    struct structure answer;
    answer.chars = countchars;
    answer.lines= countlines;
  4. Use the function combined with the structure inside main this way:
    struct structure answer = counttext(file);
  5. Return the value to the user:
    cout << answer.lines << answer.chars;

I also used the function getline(file, line) & line.length() to read the lines in the file and to calculate the length of the line in characters, respectively.

Here is my full code:

https://github.com/brendaruizt/TC1017/blob/master/multipart.cpp

multipart

 


WSQ07 – Lists

--Originally published at BRENDA

Here is my full code for this assignment:

https://github.com/brendaruizt/TC1017/blob/master/lists.cpp

For this task, I had to investigate and learn about arrays. This is where I obtained information:  Arrays. Also, from the book I have already mentioned in previous posts.

Basically an array is a list where information is stored. A size of the array must be defined. This is how I did the array:

int i;
float numbers [10];
float sum = 0.00;
for(i = 0; i < 10 ; i++) {
cout << “Please enter 10 numbers (float): “;
cin >> numbers [i]; }

Here I define the size of the array as 10 because of the excercise. In this example, the user is the one that defines the numbers that are going to be stored in the array.

I did a function that calculated the average and another that calculated the standard deviation.

lists


How I started in C++

--Originally published at Future Queen of C++

The first thing I did was google a bit about C++. From WSQ 00 I knew that I needed a bash/terminal and a program to edit. In Mac, you already have one, but in Windows you have to download one. So I went to Finder and in search I typed Terminal.

Then I went to Google and looked for Atom and just click on Download!

Screen Shot 2017-09-15 at 7.54.13 PM

After that you are ready to program ?


First Partial

--Originally published at BRENDA

So this is the end of my first partial at Tec. I found it to be not as hard as I had thought it would be. But still it was challenging.

My programming class was like a break from my other classes since I had to teach myself a whole new language. I like the way Ken teaches because you have to be responsable and dedicate time to the course because you want to, not because you have to. I learnt a lot this partial of programming, I mean, I didn’t know anything just about a month ago, so it’s a start.


Changing from integer to float

--Originally published at BRENDA

At first I didn’t even understand what the difference between these two terms was, but after reading about it I got it. An integer is a simple number without a decimal point. In C++ they are always rounded. For example if an answer is 9.79, it would appear as an integer just as 9 even if it should be rounded to 10. This is because in programming numbers round down. On the other hand, floating point numbers are the ones that have decimals.

To convert from an integer to a floating point number this is what you need to do:

int x = 3

float y = float (x);


WSQ06 – Factorial Calculator

--Originally published at BRENDA

I completed the task of making a program that calculates the factorial of a number.

I learnt the Mastery Topic #13 & #16, do while loops and recursions.

I created a function called factorial that used a recursion to repeat the same operation several times. This is my function:

int factorial (int n){
int answer;
answer=1;
while (n>1){
answer = answer * n;
n=n-1;
}
return answer;
}

I did a do-while loop to keep asking the user if they would like the program to calculate a factorial of a number again.

For this assignment I learnt what an unsigned integer is, as well as a char

Captura3

 


WSQ05 – On To Functions

--Originally published at BRENDA

This was a simpler task than the ones before because it was based on the program I had already done of Fun with Numbers, but instead of simply doing the operations inside main (), I created a function for each operation: sum, substraction, multiplication, division and residue.  For this I learnt about the Mastery Topic #6 & #7, functions. I learnt how to do it by reading the book Ken provided (http://www.greenteapress.com/thinkcpp/thinkCScpp.pdf) and by the explanation Ken gave in class.

This is an example of how I created and named a function:

int mysum (int x,int y) {
return x+y;
}

And then I just put: cout<< mysum (a,b); inside main ()  to do the operation.

Here is the code:

#include <iostream>
using namespace std;
int mysum (int x,int y) {
return x+y;
}
int mysubstraction (int x,int y){
return x-y;
}
int times (int x,int y){
return x*y;
}
int divide (int x,int y){
return x/y;
}
int left (int x,int y){
return x%y;
}
int main ()
{
int a;
cout <<“Please enter A:”;
cin>>a;
int b;
cout <<“Please enter B:”;
cin>>b;
cout << ” A + B = “;
cout<< mysum (a,b);
cout << “\r\n A – B = “;
cout << mysubstraction (a,b);
cout <<“\r\n A * B = “;
cout << times (a,b);
cout << “\r\n A / B = “;
cout << divide (a,b);
cout <<“\r\nThe remainder of A/B is “;
cout << left (a,b) <<endl;
return 0;
}Captura2


WSQ 04 – Sum of Numbers

--Originally published at BRENDA

This week I completed the Sum of Numebrs Assignment.

For this task I used the Mastery Topic #13, loops with while and #8, importing and using libraries. I had some trouble at first because I did not know how to make the program do what I wanted, but after consulting with Ken I managed to do it.

This is the code I used:

#include <iostream>
using namespace std;
int main ()
{
cout << “I will calculate the sum of integers in the range you provide ” <<endl;
int low;
int high;
cout << “Please introduce the range “<<endl;
cout << “From: “; cin >> low;
cout << “To: “; cin >> high;
int x=low;
int total=0;
while ( x <= high ) {
total = total + x ;
x++;
}
cout << “The sum from “; cout << low; cout << ” to “; cout << high; cout<< ” is “; cout << total <<endl;
return 0;
}

The picture shows how the program works.

Captura1