C++ Programming

C++ Programming


File handling functions

Posted: 27 Apr 2013 09:32 AM PDT

fopen() FILE *fopen(const char *path, const char *mode); The fopen() function is used to open a file and associates an I/O stream with it. This function takes two arguments. The first argument is a pointer to a string containing name of the file to be opened while the second argument is the mode in which the file is to be opened. The mode can be : 'r'    :  Open text file for reading. The

File Input/output in C

Posted: 27 Apr 2013 09:20 AM PDT

Input and output to and from files is identical to that at the command line, except the fprintf and fscanf functions are used and they require another argument. This additional argument is called a file pointer. In order to write two floating point numbers to a file, you first need to declar the file pointer with the FILE type, and you need to open it, as in float x=1, y=2; FILE *file; file =

Defining your own types

Posted: 27 Apr 2013 09:17 AM PDT

C allows you to define your own types, and this can make your codes much more legible. For example, you can define a type called price that you can use to define variables that you use to represent the price of certain objects. To do so, you would use typedef float price; int main(void) { price x, y; } The typedef is used in the following way: typedef existing_type new_type; The price type

Basic structure of a C program

Posted: 27 Apr 2013 09:12 AM PDT

As we saw in the hello.c example in the last lecture, Every C program must contain a main function , since the main function is the first function called when you run your program at the command line. In its simplest form, a C program is given by int main(void) { printf(''Hello world!\n''); } The int stands for the "return type", which says that the main function returns an integer if you tell

Understanding Tree structures

Posted: 27 Apr 2013 07:57 AM PDT

Tree Structures Introduction In many situation, an efficient way to represent data structures is to use trees. A tree can be defined recursively as an object containing some data and references to a certain number of subtrees. This definition leads to a hierarchical structure, in which trees with no sub-trees are called leaves. The other ones are called internal nodes. More

Overloading the >> operator

Posted: 27 Apr 2013 07:44 AM PDT

The left operand of >> will always be an istream and the right operand will be whatever we want : #include <iostream> class Crazy { public: double a, b; }; istream & operator >> (istream &i, Crazy &c) { return i >> (c.a) >> (c.b); } int main(int argc, char **argv) { Crazy x; cin >> x; } The ostream can not be copied, and will always exist as a lvalue (by definition printing

Fusion sort

Posted: 27 Apr 2013 07:21 AM PDT

The usual dumb algorithms for sorting things require a number of operations proportional to the square of the number of elements to sort (O(n2)). In practice, the used algorithms require a number of operations proportional to n×log n. The first one is the fusion sort. The main point is that given two sorted list of numbers, generating the sorted merged list needs a number of operations

Big-O Notation

Posted: 27 Apr 2013 07:04 AM PDT

Big-O Notation Why ? How ? To estimate the efficiency of an algorithm, the programmer has to be able to estimate the number of operations if requires to be executed. Usually the number of operations is estimated as a function of a parameter (like the number of data to work on, or the expected precision of a computation, etc.) For example : for(i = 0; i < n; i++) { ... } has a cost

Anti-bug tools

Posted: 27 Apr 2013 06:47 AM PDT

GDB The most standard debugging tool on UNIX is the GNU Debugger gdb. Its main functionnality is to display the piece of code which procuced a crash. To do it, compile your code with the -g option, so that debugging information will be added to the executable. This information is mainly a correspondance between the machine langage instructions and locations in the source. Then, execute the


Post a Comment