Tag Archives: #eeeeee

Mastery14

Creating and using a Python module

A module is a file containing Python definitions and statements. The file name is the module name with the suffix “.py”

Example:

  • Create “hello.py” then write the following function:
def helloworld():
   print "hello"

import hello
hello.helloworld()
>>>'hello'

AWESOME PAGE

14 1014

#Mastery28 #Tc1017

Reading and writing of files in C++

Se requiere de la libreria llamada fstream.

Hay maneras de elegir el tipo de variable. dependiendo los propositos

Data Type

Description

ofstream

This data type represents the output file stream and is used to create files and to write information to files.

ifstream

This data type represents the input file stream and is used to read information from files.

fstream

This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files.

Usamos esta para tomar los valores en el sudoku por ejemplo.

Se llama el archivo a la hora de correr el programa.

 

Creation and use of vectors in C++ #TC1017 #Mastery23

Creation and use of vectors in C++ 1017 23

Vector is a template class that is a perfect replacement for the good old C-style arrays. It allows the same natural syntax that is used with plain arrays but offers a series of services that free the C++ programmer from taking care of the allocated memory and help operating consistently on the contained objects.

The first step using vector is to include the appropriate header:

 

  1. using namespace std;
  2. //…
  3. vector v;

Here is my link

https://www.dropbox.com/s/m0popqb0mg8qbxo/Mastery23.mov?dl=0

Is a short video explaining a really easy vector 🙂 

Here is other link about vectors :D:

http://www.cplusplus.com/reference/vector/vector/vector/

http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm

#mastery28 #TC1017

 

28 1017

Reading and writing of files in C++

So far, we have been using the iostream standard library, which provides cin and coutmethods for reading from standard input and writing to standard output respectively.

This tutorial will teach you how to read and write from a file. This requires another standard C++ library called fstream, which defines three new data types:

Data Type

Description

ofstream

This data type represents the output file stream and is used to create files and to write information to files.

ifstream

This data type represents the input file stream and is used to read information from files.

fstream

This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files.

To perform file processing in C++, header files and must be included in your C++ source file.

Opening a File:

A file must be opened before you can read from it or write to it. Either the ofstream orfstream object may be used to open a file for writing and ifstream object is used to open a file for reading purpose only.

Following is the standard syntax for open() function, which is a member of fstream, ifstream, and ofstream objects.

Here, the first argument specifies the name and location of the file to be opened and the second argument of the open() member function defines the mode in which the file should be opened.

Mode Flag

Description

ios::app

Append mode. All output to that file to be appended to the end.

ios::ate

Open a file for output and move the read/write control to the end of the file.

ios::in

Open a file for reading.

ios::out

Open a file for writing.

ios::trunc

If the file already exists, its contents will be truncated before opening the file.

You can combine two or more of these values by ORing them together. For example if you want to open a file in write mode and want to truncate it in case it already exists, following will be the syntax:

Similar way, you can open a file for reading and writing purpose as follows:

Closing a File

When a C++ program terminates it automatically closes flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination.

Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects.

Writing to a File:

While doing C++ programming, you write information to a file from your program using the stream insertion operator (

Reading from a File:

You read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard. The only difference is that you use an ifstream or fstream object instead of the cin object.

 

 

#mastery24 #TC1017

24 1017

Creation and use of vectors in C++

C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

Declaring Arrays:

To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows:

This is called a single-dimension array. The arraySize must be an integer constant greater than zero and type can be any valid C++ data type. For example, to declare a 10-element array called balance of type double, use this statement:

 

#mastery23 #TC1017

23 1017

Creation and use of vectors in C++

Vector is a template class that is a perfect replacement for the good old C-style arrays. It allows the same natural syntax that is used with plain arrays but offers a series of services that free the C++ programmer from taking care of the allocated memory and help operating consistently on the contained objects.The first step using vector is to include the appropriate header:

Note that the header file name does not have any extension; this is true for all of the Standard Library header files. The second thing to know is that all of the Standard Library lives in the namespace std. This means that you have to resolve the names by prepending std:: to them:

  1. std::vector v; // declares a vector of integers

For small projects, you can bring the entire namespace std into scope by inserting a using directive on top of your cpp file:

  1. using namespace std;
  2. //…
  3. vector v; // no need to prepend std:: any more

This is okay for small projects, as long as you write the using directive in your cpp file. Never write a using directive into a header file! This would bloat the entire namespace std into each and every cpp file that includes that header. For larger projects, it is better to explicitly qualify every name accordingly. I am not a fan of such shortcuts. In this article, I will qualify each name accordingly. I will introduce some typedefs in the examples where appropriate—for better readability.

#Mastery25 – Creation and use of strings in C++

Creación y uso de strings en C++

Una de las utiliidades de C++ es la existencia de “strings” (cadenas de texto) como parte del lenguaje.

A continuación veremos su manejo primero con un ejemplo sencillo:

 

El manejo básico de las cadenas no presenta alguna dificultad:

  • Se declaran como cualquier otra variable( string cadena )
  • Se les asigna valor con el signo = , como se hace con los números enteros.
  • Su valor se muestra en pantalla con “cout”, igual que para las demás variables.

Así que veamos otro ejemplo un poco más complejo: 

La explicación para el código seria la siguiente:

  • Se puede crear una cadena sin valor inicial haciendo string texto1;
  • Se le puede dar una valor inicial a la vez que se declara, haciendo string texto2 = “Hola “; o bien string texto3(“Que tal”);
  • Se puede crear una cadena formada por varias, concateándolas (sumándolas), usando el signo +, así: texto1 = texto2 + texto3 + ” estas? “;
  • Se puede crear una subcadena a partir de un trozo de otra, la vez que se declara, así: string subcadena (texto1, 2, 6);
  • O bien se puede extraer un fragmento posteriormente: texto1.substr(0, 5);
  • Se puede insertar texto en el interior de una cadena: texto1.insert(5, “Juan “);
  • O reemplazar ciertas letras por otras: texto2.replace(1, 2, “ad”);
  • Se puede saber el tamaño (cantidad de letras) de la cadena: texto1.size()
  • Se puede acceder a una posición siguiendo el estándar de C: texto1[2]
  • O bien usando la función “at”: texto1.at(2)
  • Se puede comprobar el valor de una cadena (el texto almacenado) con ==, así: if (texto2 == “Hada “) …

 

Referencias: http://www.aprendeaprogramar.com/mod/resource/view.php?id=172

25 1017

Mastery 28

Reading and writing of files in C++

Opening a File:

A file must be opened before you can read from it or write to it. Either the ofstream or fstreamobject may be used to open a file for writing and ifstream object is used to open a file for reading purpose only.

Following is the standard syntax for open() function, which is a member of fstream, ifstream, and ofstream objects.

Here, the first argument specifies the name and location of the file to be opened and the second argument of the open() member function defines the mode in which the file should be opened.

Mode Flag Description
ios::app Append mode. All output to that file to be appended to the end.
ios::ate Open a file for output and move the read/write control to the end of the file.
ios::in Open a file for reading.
ios::out Open a file for writing.
ios::trunc If the file already exists, its contents will be truncated before opening the file.

You can combine two or more of these values by ORing them together. For example if you want to open a file in write mode and want to truncate it in case it already exists, following will be the syntax:

Similar way, you can open a file for reading and writing purpose as follows:

Closing a File

When a C++ program terminates it automatically closes flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination.

Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects.

Writing to a File:

While doing C++ programming, you write information to a file from your program using the stream insertion operator (ofstream or fstream object instead of the cout object.

Reading from a File:

You read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard. The only difference is that you use an ifstream or fstream object instead of the cin object.

Read & Write Example:

Following is the C++ program which opens a file in reading and writing mode. After writing information inputted by the user to a file named afile.dat, the program reads information from the file and outputs it onto the screen:

When the above code is compiled and executed, it produces the following sample input and output:

Above examples make use of additional functions from cin object, like getline() function to read the line from outside and ignore() function to ignore the extra characters left by previous read statement.

Credits:

http://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm

1017 28

Mastery 26

Creation and use of matrixes in C++ (multi – dimensional arrays)

Two-Dimensional Arrays:

The simplest form of the multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size x,y, you would write something as follows:

Where type can be any valid C++ data type and arrayName will be a valid C++ identifier.

A two-dimensional array can be think as a table, which will have x number of rows and y number of columns. A 2-dimensional array a, which contains three rows and four columns can be shown as below:

Mastery 26

Thus, every element in array a is identified by an element name of the form a[ i ][ j ], where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a.

Initializing Two-Dimensional Arrays:

Multidimensioned arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row have 4 columns.

The nested braces, which indicate the intended row, are optional. The following initialization is equivalent to previous example:

Accessing Two-Dimensional Array Elements:

An element in 2-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example:

The above statement will take 4th element from the 3rd row of the array. You can verify it in the above digram.

When the above code is compiled and executed, it produces the following result:

As explained above, you can have arrays with any number of dimensions, although it is likely that most of the arrays you create will be of one or two dimensions.

Credits:

http://www.tutorialspoint.com/cplusplus/cpp_multi_dimensional_arrays.htm

1017 26

Mastery 25

Creation and use of strings in C++

C++ provides following two types of string representations:

  • The C-style character string.

  • The string class type introduced with Standard C++.

The String Class in C++:

The standard C++ library provides a string class type that supports all the operations mentioned above, additionally much more functionality. We will study this class in C++ Standard Library but for now let us check following example:

At this point, you may not understand this example because so far we have not discussed Classes and Objects. So can have a look and proceed until you have understanding on Object Oriented Concepts.

When the above code is compiled and executed, it produces result something as follows:

Credits:

http://www.tutorialspoint.com/cplusplus/cpp_strings.htm

1017 25