Tag Archives: quiz11

Quiz11

#Quiz11

Finally i did it the quiz!! this is my code for question 1 https://github.com/Xochitl96/WSQ/blob/master/Quiz11p1.cpp

and this code for question 2 https://github.com/Xochitl96/WSQ/blob/master/Quiz11p2.cpp

Quiz 11

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”);

}

quiZ 11

#Quiz11

Aquí puedes encontrar mi código:

https://github.com/taniaprogram/SolvingProblemsWithProgramming/blob/master/quiz11.cpp

Fue necesario aprender a usar la librería fstream para poder abrir, leer y utilizar los datos contenidos en un archivo de texto.

Encontré información sobre dicha librería en los siguientes links:

http://c.conclase.net/curso/?cap=039

http://www.nebrija.es/~abustind/Informatica/MetodologiaI/Archivos.pdf

http://www.programacionenc.net/index.php?option=com_content&view=article&id=69:manejo-de-archivos-en-c&catid=37:programacion-cc&Itemid=55

Quiz #11

Me, after the C++ final exam 

Link to code Q2

Link to code Q1

Quiz 11. Apr 27th.

Heres the code for of

Question 1: https://github.com/IsmaLga/Quiz11/blob/master/Q1.py

Question 2: https://github.com/IsmaLga/Quiz11/blob/master/Q2.py

 

I really recomend understanding of how to work with files in Python. This helped a lot in the process of writing the code for these two masteries.

Quiz 11 – Gone Bananas for Files

Quiz 11

 

That question 2 made me crazy with that thing of “string.find” but here it is…

 

Question 1:

https://github.com/Gonzalomata22/Quizzes/blob/master/Quiz11/Quiz11.cpp

Question 2:

https://github.com/Gonzalomata22/Quizzes/blob/master/Quiz11/question2.cpp