QUIZ #11

Here 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.cpp

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.

In this case, the first code will read a series of numbers that were placed line by line and then add those numbers.

In order to do that, we will need to open the file, and create a vector to store all the values in each line.

Also, we will need to transform string characters (the numbers from the file) into integers and for that we will use the function (atoi). 

Here is an example of how the code should look:

<iostream>

<fstream>

<sstream>

<cstdlib>

<string>

<vector>

<cmath>

using namespace std;

 

int ReadNumbersFromFiles (string filename){

 

  int number_of_lines = 0;

  int sum = 0;

  int x;

  float deb;

  int mean;

  float sdev;

  float var;

  vector <int> numbers;

  ifstream myFile;

  myFile.open(filename.c_str());

  string line;

  int numLines = 0;

 

while ( getline(myFile, line) ){

  ++numLines;

  x = atoi(line.c_str());

  sum = sum + x;

  numbers.push_back(x);

}

      mean= sum/ numLines;

      for(int i = 1; i <= numLines; i++){

              deb = (x – mean)*(x – mean);

              sdev = sdev + deb;

          }

      var = sdev / (numLines – 1);

      deb= sqrt(var);

      cout << “Number of lines in text file: ” << numLines<<endl;

      myFile.close();

      cout<<“the sum of the numbers: “<< sum<<endl;

      cout<<“mean: “<<mean<<endl;

      cout<<“The standard deviation: “<<deb;

    }

 

int main(){

  string file;

//  cout<<“please enter the name of the file you wish to open: “<<endl;

//  cin>>file;

  ReadNumbersFromFiles(“numbers.txt”);

}

Bananas:

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:

<iostream>

<iostream>

<fstream>

<sstream>

<cstdlib>

<string>

 using namespace std;

 

 int bananas(string banana){

   string str2=”banana”;

   ifstream myFile;

   myFile.open(banana.c_str());

   string line;

   size_t found;

    while ( getline(myFile, line) ){

     found = line.find(str2);

        if (found!=string::npos)

            cout << “first ‘banana’ found at: ” <<found<<endl;

            found=line.find(“banana”,found+1);

          if (found!=string::npos)

            cout << “second ‘banana’ found at: ” << found <<endl;

 

            found=line.find(“banana”,found+2);

          if (found!=string::npos)

            cout << “third ‘banana’ found at: ” << found <<endl;

 

            found=line.find(“banana”,found+4);

          if (found!=string::npos)

            cout << “forth ‘banana’ found at: ” << found <<endl;

 

            found=line.find(“banana”,found+5);

          if (found!=string::npos)

            cout << “fifth ‘banana’ found at: ” << found <<endl;

    }

 

    myFile.close();

 

  return 0;

}

int main(){

  bananas(“bananas.txt”);

}

CC BY 4.0 QUIZ #11 by carlos green is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.