Project

Hi!

I have partnered up with Oscar to do this semester’s project. We are going to do a game that guesses the character of Game of Thrones you are thinking of by asking the user a set of questions. You can check out Oscar’s post por a further description of the project and the schedule we are going to be working on.

 

WSQ11 – Bananas!

Ind this assignment I learnt how to find a specific string in a file, in this case the word banana. I used the function line.find(«banana»).

First I made the function work by searching only for the word banana, with lowercase. I made a loop inside of a loop so that if the a word banana was found in a line it wouldn’t just jump right to the next line, instead it would keep looking in the same line until all the bananas were found.

When the code worked I added a new function called transform to convert all the strings in the file to lowecases. This way, it wouldn’t matter if the word was written as Banana or banAna, because it would be converted to just «banana».

This is the function transform:

transform(line.begin(),line.end(),line.begin(), (int (*)(int))tolower)

Here is my code:

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

banana.PNG

WSQ10 – Babylonian Method

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

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

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