So in this fourteen week class I started with doing this WSQ11 I started reviewing in creating and calling functions in C++.#Mastery06, #Mastery07, #Mastery16 Use of recursion for repetitive algorithms, #Mastery17 When to use what type of repetition in a program, #Mastery18 Creation and use of Arrays/ Vectors in C++. Furthermore, in this stage I have all the topics of the course from 1 to 22 including #Mastery22 Matrixes and vectors because in this code I use a matrix with a dimension of 1.

What I did for this numeric program is solving the problem to the user by creating a program with writing 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.

Hence, the resources I need it to solve this program are here:

ken bauer

Video to convert higher to lower cases from the .txt file

The following photograph shows the solution to this problem:

wsq11v1

wsq11v2wsq11v3wsq11v4a1

a2

a3a4

So at first I wrote the same structure of the program just did the same as what i did in Hello World: Second Class, Second Blog (Blog of the second class 12/01/17) and Hello World.cpp,  #WSQ01 Post Fun with Numbers 16/01/17 and WSQ1.cpp#WSQ02 Post Temperature 23/01/17 and WSQ02.cpp#WSQ03 Post Pick a Number 23/01/17 and WSQ03.cpp#WSQ04 Post Sum of Numbers 23/01/17 and WSQ04.cpp#WSQ05 Six Tutorial On To Functions 12/02/17 and WSQ05.cpp#WSQ06 Factorial Calculator 12/02/17 and WSQ06.cpp#WSQ07 Lists 03/03/17 and WSQ07.cpp, #WSQ08 Yo soy 196 11/03/17 and WSQ08.cpp#WSQ09 Multipart Data and Files 10/03/17 and WSQ09.cpp and #WSQ10 Babylonian Method 21/03/17 and WSQ10.cpp where i explained the application of the #MasteryTopic01 that it is for comments that could be very useful when debugging and #MasteryTopic04 that is basic output for data. Also I did some other mastery topics.

What i first put in the code was the library to call all the fuctions of inputs and outputs of data in languague C++ #MasteryTopic01

#include <iostream> //Library to call all the
//fuctions of inputs and outputs of data in languague
//C++ #MasteryTopic01

Next, we use a Library to call all the fuctions of reading AND writing files in languague C++ #MasteryTopic21

#include <fstream> //Library to call all the
//fuctions of reading AND writing files in languague
//C++ #MasteryTopic21

Furthermore, in C++ we need a command that everytime goes with the instructions of input and outputs of data that is call std but with this command helps writing these std out of the main because the machine factorizes them.

using namespace std; //In C++ we need a command
// that everytime goes with the instructions of input and outputs
//of data that is call std but with this command helps writing
//these std out of the main because the machine factorizes them

In addition, we need to begin our program and declarate string varible filename and initialized it to “banana.txt” #Mastery19 and #Mastery20

int main ()
{

string filename = "banana.txt";
//Declarate string varible filename and initialized it to "banana.txt" #Mastery19 and #Mastery20

Next we need to Declarate int variable cfinal

int cfinal; //Declarate int variable cfinal

Then, we need to use THIS ASSIGNATION HELPS TO CALL THE FUNCTION OF “find_bananas” TO MAKE THE TASK OF COUNT THE NUMBER OF TIMES THAT THE WORD banana is found WITH THE HELP OF THE VARIABLE int ‘cfinal’ with the parameter filename.c_str() IN ORDER to convert string to char as a parameter #Mastery06 and #Mastery07

cfinal=find_bananas(filename.c_str()); //THIS ASSIGNATION HELPS TO CALL THE FUNCTION OF "find_bananas" TO MAKE THE TASK OF
//COUNT THE NUMBER OF TIMES THAT THE WORD banana is found WITH THE HELP OF THE VARIABLE int 'cfinal' with the parameter filename.c_str()
//IN ORDER to convert string to char as a parameter #Mastery06 and #Mastery07

Moreover, This is the structure of a function where you have subrutines in order to make different tasks, in this case we have an int function with the name “find_bananas” and with const char parameter filename that has the value of “banana.txt” in order to have int results inside this function.

int find_bananas (const char *filename) //This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have an int function with the name "find_bananas" and with const char parameter filename
// that has the value of "banana.txt" in order to
//have int results inside this function.

Furthermore, we have a text name of the file .txt “banana.txt”

cout<<filename<<endl; //text name of the file .txt "banana.txt"

After that, we need to declarate a ifstream variable called “file” in order to write or read #Mastery21 Reading and writing of text files. Therefore, this is a variable called file where you also make the function of giving the value of filename to file that in this case is banana.txt

ifstream file(filename); ///Declarate a ifstream variable called "file" in order to write or read #Mastery21 Reading and writing of text files
//Therefore, this is a variable called file where you also make the function of giving the value of filename to file that in this case
//is banana.txt

Next, we need a command of data in form of text space

cout<<endl; //command of data in form of text space

Moreover, we need to declarate int variable counteralllines initialized in 0 in order to count all the words of banana in banana.txt

int counteralllines=0; //Declarate int variable counteralllines initialized in 0 in order to count all the words of banana in banana.txt

Next, Declarate string variable lines in order to save all the characters of the banana.txt file

string lines; //Declarate string variable lines in order to save all the characters of the banana.txt file

Then we use a #Mastery10 use of the if statement. This command allows the condition if the file is open and when this condition will be true, the tasks inside of the statement will execute. In this case if the value file is open this will occur:

if(file.is_open()) //#Mastery10 use of the if statement //This command allows the condition if the file is open and when this
//condition will be true, the tasks inside of the statement will execute. In this case if the value file is open
//this will occur:

After that we need a loop. With this command while is utilized as a loop WHILE execute all the instruccions that has this loop until that does not complete that we get all the characters of lines in the file

while(getline(file,lines)) //With this command while is utilized as a loop WHILE execute all the
//instruccions that has this loop until that does not complete that we get all the characters of lines in the file
{

counteralllines = counteralllines + linebanana(lines); //This is a math operation between 'counteralllines' and
// the value of the int function linebanana with the string parameter 'lines' and stands for
//do the sum between those values and saved it in the int variable 'counteralllines'

cout<<"COUNTER THIS LINE= "<<counteralllines<<endl; //In
//this text we show the authentic output of the value entered of counteralllines in the terminal interface #Mastery20 Validated user
//input (ensure correct / expected data entry) I used it to tell me all the words banana appeared

}

Next, This is a math operation between ‘counteralllines’ and the value of the int function linebanana with the string parameter ‘lines’ and stands for do the sum between those values and saved it in the int variable ‘counteralllines’

counteralllines = counteralllines + linebanana(lines); //This is a math operation between 'counteralllines' and
// the value of the int function linebanana with the string parameter 'lines' and stands for
//do the sum between those values and saved it in the int variable 'counteralllines'

Next, this function escapes to another function and this is the structure of a function where you have subrutines in order to make different tasks, in this case we have an int function with the name “linebanana” and with string parameter lines that has just the value of all the words bananas in string of the file in order to have int results inside this function.

int linebanana (string lines) //This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have an int function with the name "linebanana" and with string parameter lines
// that has just the value of all the words bananas in string of the file in order to have int results inside this function.

Declarate int variable i initialized in 0.

int i=0; //Declarate int variable i initialized in 0.

int counterbananaeachline=0; //Declarate int variable counterbananaeachline initialized in 0.

int counterbananaeachline=0; //Declarate int variable counterbananaeachline initialized in 0.

With this command for is utilized as a loop FOR execute all the instruccions that has this loop until that does not complete that the variable j will be less than the numerical value of the lines variable while it is increasing one in one in this variable i inicialized from the value 0 #Mastery22 Matrixes and vectors because in this code I use a matrix with a dimension of 1.

for(int j=0; j		<lines.length(); j++) //With this command for is utilized as a loop FOR execute all the
//instruccions that has this loop until that does not complete that the variable j
// will be less than the numerical value of the lines variable while it is increasing one in one in this variable i
//inicialized from the value 0 #Mastery22 Matrixes and vectors because in this code I use a matrix with a dimension of 1.

Next, This is a string operation between ‘lines’ and ‘lines[j where this j is the space of each string value per each line bananas from line 1 to the last line, I am using the #Mastery18 Creation and use of Arrays/Vectors in C++]’ and the similar functions like vectors in#Mastery22 Matrixes and vectors, stands for transform all the character of the banana.txt file to lower case between those values and saved it in string variable ‘lines’. Hence, by doing this all the characters from the file banana.txt will be in lower cases in order to count next just the condition of “banana” all in lower cases.

lines[j]=tolower(lines[j]); //This is a string operation between 'lines' and
//'lines[j where this j is the space of each string value per each line bananas from line 1 to the last line, I am
//using the #Mastery18 Creation and use of Arrays/Vectors in C++]' and stands for transform all the character of the banana.txt file
//to lower case between those values and saved it in string variable 'lines'. Hence, by doing this all the characters from the file
//banana.txt will be in lower cases in order to count next just the condition of "banana" all in lower cases.

After that, we need this command while is utilized as a loop WHILE to execute all the instruccions that has this loop until that INSIDE of this loop has a break statement to break this infinite loop.

while (1) //With this command while is utilized as a loop WHILE execute all the
//instruccions that has this loop until that INSIDE of this loop has a break statement to break this infinite loop

Next, we need to Declarate int variable foundtop. Then we have an assignation operation between ‘foundtop’ and ‘the string value lines’ and stands for find the numeric position everytime when the word “banana” appears inside the variable lines and save this new value of the int position to int variable ‘foundtop’ (position 0 is the begining of the file .txt)

int foundtop = lines.find("banana", i); //Declarate int variable foundtop. Then we have an assignation operation between
//'foundtop' and 'the string value lines' and stands for find the numeric position everytime when the word "banana" appears
//inside the variable lines and save this new value of the int position to int variable 'foundtop' (position 0 is the begining
// of the file .txt)

Next, we need a math operation between ‘i’ and ‘foundtop’ and stands for adding those values and saved it in int variable ‘i’

i=foundtop+1; //This is a math operation between 'i' and 'foundtop' and stands for adding
//those values and saved it in int variable 'i'

Then, we need a #Mastery10 use of the if statement //This command allows the condition if the value of foundtop NOOT BE equal to -1 and when this condition will be true, the tasks inside of the statement will execute. In this case the if executes because the variable foundtop is not finishing in counting all the characters of each line and has the value different of -1. In this case if the value is not equal than -1 this will occur:

if(foundtop!=-1) //#Mastery10 use of the if statement //This command allows the condition if the value of foundtop
//NOOT BE equal to -1 and when this condition will be true, the tasks inside of the statement will execute.
//In this case the if executes because the variable foundtop is not finishing in counting all the characters of each line
//and has the value different of -1
// In this case if the value is not equal than -1 this will occur:

Then, This is a math operation between ‘counterbananaeachline’ and ‘1’ and stands for adding those values and saved it in int variable ‘counterbananaeachline’

counterbananaeachline++; //This is a math operation between 'counterbananaeachline' and '1' and stands for adding
//those values and saved it in int variable 'counterbananaeachline'

After that, we need a #Mastery11 Use of else with a conditional if: // if the condition does not achieve on the other hand you will do this:

else //#Mastery11 Use of else with a conditional if: // if the condition does not achieve on the other hand you will do this:
{

cout<<"COUNTER= "<<counterbananaeachline<<endl; //In
//this text we show the authentic output of the value entered of counteralllines in the terminal interface #Mastery20 Validated user
//input (ensure correct / expected data entry) I used it to tell me all the words banana appeared in each line

break; //This command helps to end the infinite while (1) and allow the program to go back to the next function

}

Next, in this text we show the authentic output of the value entered of counteralllines in the terminal interface #Mastery20 Validated user input (ensure correct / expected data entry) I used it to tell me all the words banana appeared in each line

cout<<"COUNTER= "<<counterbananaeachline<<endl; //In
//this text we show the authentic output of the value entered of counteralllines in the terminal interface #Mastery20 Validated user
//input (ensure correct / expected data entry) I used it to tell me all the words banana appeared in each line

After that, this command helps to end the infinite while (1) and allow the program to go back to the next function.

break; //This command helps to end the infinite while (1) and allow the program to go back to the next function

Next, With this command you return the value of the previus operations of “linebanana” function inside of the variable int counterbananaeachline in order to call it in THE FIND_BANANA FUNCTION

return counterbananaeachline; //With this command you return the value of the previus operations of "linebanana" function inside of the
//variable int counterbananaeachline in order to call it in THE MAIN FUNCTION

Then, the program goes to the main function and in this text we show the authentic output of the value entered of counteralllines in the terminal interface #Mastery20 Validated user input (ensure correct / expected data entry) I used it to tell me all the words banana appeared

cout<<"COUNTER THIS LINE= "<<counteralllines<<endl; //In
//this text we show the authentic output of the value entered of counteralllines in the terminal interface #Mastery20 Validated user
//input (ensure correct / expected data entry) I used it to tell me all the words banana appeared

After that, we need this command you return the value of the previus operations of “find_bananas” function inside of the variable int counteralllines in order to call it in THE MAIN FUNCTION

return counteralllines; //With this command you return the value of the previus operations of "find_bananas" function inside of the
//variable int counteralllines in order to call it in THE MAIN FUNCTION

Finally, the program goes back to the main function and in this text we show the authentic output of the value entered of cfinal in the terminal interface #Mastery20 Validated user input (ensure correct / expected data entry)

cout<<"TIMES THE WORD banana FOUND="<<cfinal<<endl; //In
//this text we show the authentic output of the value entered of cfinal in the terminal interface #Mastery20 Validated user
//input (ensure correct / expected data entry)

Finally we have a return statement. This command allows label the final of the function main ()

return 0; // This command allows label the final of the function main ()

And i also resolved it by doing this video tutorial that i made reafirming these parts at this link :#WSQ11 Tutorial 12 Go Bananas 15/04/17 and WSQ11.cpp

Finally with the command ./a.out and WSQ11.cpp  and tells the terminal to run the C++ file:

vvvv

This code for solution will be here and in Github:

Link in Github: WSQ11.cpp

If you do not want the WSQ11.cpp there is it in here with comments. The comments are very useful to detect errors and create new things #Mastery01 #Masterytopic01 #MasteryTopic01 #MASTERY01 #MASTERYTOPIC01 :

Also in the last class I learned how to present my C++ in a better ‘readable’ way after doing it #Mastery02 #Masterytopic02 #MasteryTopic02 #MASTERY02 #MASTERYTOPIC02:

#include <iostream> //Library to call all the
//fuctions of inputs and outputs of data in languague
//C++ #MasteryTopic01

#include <fstream> //Library to call all the
//fuctions of reading AND writing files in languague
//C++ #MasteryTopic21

using namespace std; //In C++ we need a command
// that everytime goes with the instructions of input and outputs
//of data that is call std but with this command helps writing
//these std out of the main because the machine factorizes them

int linebanana (string lines) //This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have an int function with the name "linebanana" and with string parameter lines
// that has just the value of all the words bananas in string of the file in order to have int results inside this function.
{

int i=0; //Declarate int variable i initialized in 0.
int counterbananaeachline=0; //Declarate int variable counterbananaeachline initialized in 0.

for(int j=0; j<lines.length(); j++) //With this command for is utilized as a loop FOR execute all the
//instruccions that has this loop until that does not complete that the variable j
// will be less than the numerical value of the lines variable while it is increasing one in one in this variable i
//inicialized from the value 0 #Mastery22 Matrixes and vectors because in this code I use a matrix with a dimension of 1.
{

lines[j]=tolower(lines[j]); //This is a string operation between 'lines' and
//'lines[j where this j is the space of each string value per each line bananas from line 1 to the last line, I am
//using the #Mastery18 Creation and use of Arrays/Vectors in C++]' and stands for transform all the character of the banana.txt file
//to lower case between those values and saved it in string variable 'lines'. Hence, by doing this all the characters from the file
//banana.txt will be in lower cases in order to count next just the condition of "banana" all in lower cases.

}
cout<<lines<<endl; //In
//this text we show the authentic output of the value entered of counteralllines in the terminal interface #Mastery20 Validated user
//input (ensure correct / expected data entry) I used it to tell me all the words banana appeared

while (1) //With this command while is utilized as a loop WHILE execute all the
//instruccions that has this loop until that INSIDE of this loop has a break statement to break this infinite loop
{
int foundtop = lines.find("banana", i); //Declarate int variable foundtop. Then we have an assignation operation between
//'foundtop' and 'the string value lines' and stands for find the numeric position everytime when the word "banana" appears
//inside the variable lines and save this new value of the int position to int variable 'foundtop' (position 0 is the begining
// of the file .txt)

i=foundtop+1; //This is a math operation between 'i' and 'foundtop' and stands for adding
//those values and saved it in int variable 'i'

if(foundtop!=-1) //#Mastery10 use of the if statement //This command allows the condition if the value of foundtop
//NOOT BE equal to -1 and when this condition will be true, the tasks inside of the statement will execute.
//In this case the if executes because the variable foundtop is not finishing in counting all the characters of each line
//and has the value different of -1
// In this case if the value is not equal than -1 this will occur:
{

counterbananaeachline++; //This is a math operation between 'counterbananaeachline' and '1' and stands for adding
//those values and saved it in int variable 'counterbananaeachline'

}

else //#Mastery11 Use of else with a conditional if: // if the condition does not achieve on the other hand you will do this:
{

cout<<"COUNTER= "<<counterbananaeachline<<endl; //In
//this text we show the authentic output of the value entered of counteralllines in the terminal interface #Mastery20 Validated user
//input (ensure correct / expected data entry) I used it to tell me all the words banana appeared in each line

break; //This command helps to end the infinite while (1) and allow the program to go back to the next function

}

} //END OF WHILE

return counterbananaeachline; //With this command you return the value of the previus operations of "linebanana" function inside of the
//variable int counterbananaeachline in order to call it in THE MAIN FUNCTION

}

int find_bananas (const char *filename) //This is the structure of a function where you have subrutines in order to make
//different tasks, in this case we have an int function with the name "find_bananas" and with const char parameter filename
// that has the value of "banana.txt" in order to
//have int results inside this function.
{
cout<<filename<<endl; //text name of the file .txt "banana.txt"

ifstream file(filename); ///Declarate a ifstream variable called "file" in order to write or read #Mastery21 Reading and writing of text files
//Therefore, this is a variable called file where you also make the function of giving the value of filename to file that in this case
//is banana.txt

cout<<endl; //command of data in form of text space

int counteralllines=0; //Declarate int variable counteralllines initialized in 0 in order to count all the words of banana in banana.txt

string lines; //Declarate string variable lines in order to save all the characters of the banana.txt file

if(file.is_open()) //#Mastery10 use of the if statement //This command allows the condition if the file is open and when this
//condition will be true, the tasks inside of the statement will execute. In this case if the value file is open
//this will occur:
{
while(getline(file,lines)) //With this command while is utilized as a loop WHILE execute all the
//instruccions that has this loop until that does not complete that we get all the characters of lines in the file
{

counteralllines = counteralllines + linebanana(lines); //This is a math operation between 'counteralllines' and
// the value of the int function linebanana with the string parameter 'lines' and stands for
//do the sum between those values and saved it in the int variable 'counteralllines'

cout<<"COUNTER THIS LINE= "<<counteralllines<<endl; //In
//this text we show the authentic output of the value entered of counteralllines in the terminal interface #Mastery20 Validated user
//input (ensure correct / expected data entry) I used it to tell me all the words banana appeared

}

} //END OF IF

return counteralllines; //With this command you return the value of the previus operations of "find_bananas" function inside of the
//variable int counteralllines in order to call it in THE MAIN FUNCTION

}

int main ()
{

string filename = "banana.txt"; //Declarate string varible filename and initialized it to "banana.txt" #Mastery19 and #Mastery20

int cfinal; //Declarate int variable cfinal

cfinal=find_bananas(filename.c_str()); //THIS ASSIGNATION HELPS TO CALL THE FUNCTION OF "find_bananas" TO MAKE THE TASK OF
//COUNT THE NUMBER OF TIMES THAT THE WORD banana is found WITH THE HELP OF THE VARIABLE int 'cfinal' with the parameter filename.c_str()
//IN ORDER to convert string to char as a parameter #Mastery06 and #Mastery07

cout<<"TIMES THE WORD banana FOUND="<<cfinal<<endl; //In
//this text we show the authentic output of the value entered of cfinal in the terminal interface #Mastery20 Validated user
//input (ensure correct / expected data entry)

return 0; // This command allows label the final of the function main ()

}
Anuncios

3 comentarios en “#WSQ11 Go Bananas 15/04/17 and WSQ11.cpp

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s