Category Archives: Basic concepts

Homework 10: Babylonian Method

This function will calculate a square root through the Babylonian Method.

First of all, I would like to explain how the method works. You have to establish a first number that is “aproximate” to the real squareroot, doesn’t matter if that number is equal to one, because we are going to make some mathematical operations. Shown below the steps.

  1. Divide the number, which we want to calculate its square root (N), between the before established number (y).
  2. The result of the past step will be added to the first established number (y).
  3. We repeat this steps until we obtain the aproximation as exactly as we want it.

Having said that, we only have to create a function that execute this algorithm, and ask the user for the number that he/she wants to calculate its square root.

This is my code.

#include <iostream>
using namespace std;

long double square_root(long double N)
{
  long double y=1, c;
  for(int i=0; i<1000; i++)
  {
    c=N/y;
    y=(y+c)/2;
  }
  return y;
}

int main()
{
  int N;
  cout << "Square Root... \n\nNumber: ";
  cin >> N;
  cout << "The square root of "<< N << " is equal to "<<square_root(N) << endl << endl;
  return 0;
}

 

Homework 9: Multipart data and files

In this asingment we are going to read a text file and count the number of both lines and characters present on the document.

We need to use the libraries #include and #include to be able to use the functions that open and read text documents.

This is my code.

#include 
#include 
using namespace std;

int main()
{
  fstream text1;
  int lines=0, chars=0; // we initialize two variables for lines and characters, to count them
  string line; //we declare and string variable to read a line
  char letter; // this is gonna let us read each character

  text1.open("Archivo.txt", ios::in); // the file is open
  while(! text1.eof()) // reads each line and counts it
  {
    getline(text1,line);
    lines++;
  }
  text1.close();
  fstream text2;
  text2.open("Archivo.txt", ios::in); // the file is open
  while (! text2.eof() ) { // reads each character and counts it. 
    text2 >> letter;
    chars++;
  }
  text2.close(); // close the file
  lines-=1;
  chars-=1;

  cout << "\nThe file contains ... \n" << endl;
  cout <<"Amount of lines: " << lines << endl;
  cout <<"Amount of characters: "<< chars << endl << endl;



  return 0;
}

Hello World!!!

This is the first program I make with C++. It is very simple, and it’s like the “Ritual of Iniciation” in the programming world. You only have to understand simple concepts to make it happen.

For example, to print or read informatio during the execution of the program, you need to write the line #include <iostream> and to avoid write std::comand; (the part of std::) in each line and comand you want to execute, you just have to write using namespace std; .

It’s as simple as that. Now, to write anything you can use the comand cout << //what you want to print into ” “;

This is my Hello World! code:

#include <iostream>
using namespace std;

int main()
{
  cout << "Hello World!";
  std::cout << "/* message */" << '\n';
  return 0;
}

 

What is the yours?