#TC1017<\/a> #QUIZ11<\/a><\/p>\n Here is the gitbub link to both exercises: https:\/ When using external files, you can open them with a program and read them line by line in order to receive information relevant for your program.<\/p>\n In this case, the first code will read a series of numbers that were placed line by line and then add those numbers.<\/p>\n In order to do that, we will need to open the file, and create a vector to store all the values in each line.<\/p>\n Also, we will need to transform string characters (the numbers from the file) into integers and for that we will use the function (atoi). <\/p>\n Here is an example of how the code should look:<\/p>\n #include<\/a> <iostream><\/p>\n #include<\/a> <fstream><\/p>\n #include<\/a> <sstream><\/p>\n #include<\/a> <cstdlib><\/p>\n #include<\/a> <string><\/p>\n #include<\/a> <vector><\/p>\n #include<\/a> <cmath><\/p>\n using namespace std;<\/p>\n <\/p>\n int ReadNumbersFromFiles (string filename){<\/p>\n <\/p>\n int number_of_lines = 0;<\/p>\n int sum = 0;<\/p>\n int x;<\/p>\n float deb;<\/p>\n int mean;<\/p>\n float sdev;<\/p>\n float var;<\/p>\n vector <int> numbers;<\/p>\n ifstream myFile;<\/p>\n myFile.open(filename.c_str());<\/p>\n string line;<\/p>\n int numLines = 0;<\/p>\n <\/p>\n while ( getline(myFile, line) ){<\/p>\n ++numLines;<\/p>\n x = atoi(line.c_str());<\/p>\n sum = sum + x;<\/p>\n numbers.push_back(x);<\/p>\n }<\/p>\n mean= sum\/ numLines;<\/p>\n for(int i = 1; i <= numLines; i++){<\/p>\n deb = (x – mean)*(x – mean);<\/p>\n sdev = sdev + deb;<\/p>\n }<\/p>\n var = sdev \/ (numLines – 1);<\/p>\n deb= sqrt(var);<\/p>\n cout << “Number of lines in text file: ” << numLines<<endl;<\/p>\n myFile.close();<\/p>\n cout<<“the sum of the numbers: “<< sum<<endl;<\/p>\n cout<<“mean: “<<mean<<endl;<\/p>\n cout<<“The standard deviation: “<<deb;<\/p>\n }<\/p>\n <\/p>\n int main(){<\/p>\n string file;<\/p>\n \/\/ cout<<“please enter the name of the file you wish to open: “<<endl;<\/p>\n \/\/ cin>>file;<\/p>\n ReadNumbersFromFiles(“numbers.txt”);<\/p>\n }<\/p>\n <\/p>\n Bananas:<\/p>\n The program needs to find a word from a text file and then print out where it find it no matter the way the word (STRING) is arranged:<\/p>\n #include<\/a> <iostream><\/p>\n #include<\/a> <iostream><\/p>\n #include<\/a> <fstream><\/p>\n #include<\/a> <sstream><\/p>\n #include<\/a> <cstdlib><\/p>\n #include<\/a> <string><\/p>\n using namespace std;<\/p>\n <\/p>\n int bananas(string banana){<\/p>\n string str2=”banana”;<\/p>\n ifstream myFile;<\/p>\n myFile.open(banana.c_str());<\/p>\n string line;<\/p>\n size_t found;<\/p>\n while ( getline(myFile, line) ){<\/p>\n found = line.find(str2);<\/p>\n if (found!=string::npos)<\/p>\n cout << “first ‘banana’ found at: ” <<found<<endl;<\/p>\n found=line.find(“banana”,found+1);<\/p>\n if (found!=string::npos)<\/p>\n cout << “second ‘banana’ found at: ” << found <<endl;<\/p>\n <\/p>\n found=line.find(“banana”,found+2);<\/p>\n if (found!=string::npos)<\/p>\n cout << “third ‘banana’ found at: ” << found <<endl;<\/p>\n <\/p>\n found=line.find(“banana”,found+4);<\/p>\n if (found!=string::npos)<\/p>\n cout << “forth ‘banana’ found at: ” << found <<endl;<\/p>\n <\/p>\n found=line.find(“banana”,found+5);<\/p>\n if (found!=string::npos)<\/p>\n cout << “fifth ‘banana’ found at: ” << found <<endl;<\/p>\n }<\/p>\n <\/p>\n myFile.close();<\/p>\n <\/p>\n return 0;<\/p>\n }<\/p>\n int main(){<\/p>\n bananas(“bananas.txt”);<\/p>\n }<\/p>\n <\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":" #TC1017 #QUIZ11Here is the gitbub link to both exercises: https:\/\/github.com\/carlosgreen\/TC1017\/blob\/master\/bananas.cpp and https:\/\/github.com\/carlosgreen\/TC1017\/blob\/master\/readnumbers.cppWhen using external files, you can open them… Continue reading