Wednesday March 10 , 2010
Font Size
   

ACMSolver :: Art of Programming & Tech Site

ACMSolver for Ubuntu

AddThis Social Bookmark Button

We are planning to start a separate page for ubuntu and relevant open source softwares.

Please send any suggestion : This e-mail address is being protected from spambots. You need JavaScript enabled to view it

 

From Baylor to Baylor

AddThis Social Bookmark Button

From Baylor to Baylor preserves the legacy of the ACM-ICPC World Finals. The book contains all the problems used during the 1991 to 2006 competitions, carefully typesetted and formatted to the highest standard. Also, almost 100 figures have been completely redrawn to improve their printed quality. Prefaced by William B. Poucher from Baylor University (Texas) and coordinated by Miguel A. Revilla from Universidad de Valladolid (Spain), this work is the definitive guide to 16 years of history of the International Collegiate Programming Contest, published thanks to the collaboration of the Competitive Learning Institute and the Competitive Infrastructure Initiative. This book is tribute to all the staff, contestants, judges and volunteers that made it possible.

Buy it at http://www.lulu.com/content/621089.

 

C++ I/O

AddThis Social Bookmark Button

The <iostream> library automatically defines a few standard objects:

  • cout, an object of the ostream class, which displays data to the standard output device.
  • cerr, another object of the ostream class that writes unbuffered output to the standard error device.
  • clog, like cerr, but uses buffered output.
  • cin, an object of the istream class that reads data from the standard input device.

The <fstream> library allows programmers to do file input and output with the ifstream and ofstream classes. C++ programmers can also do input and output from strings by using the stringstream class.

Some of the behavior of the C++ I/O streams (precision, justification, etc) may be modified by manipulating various I/O stream format flags.

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
char ch;
int i;
float f;
char str[80];

ofstream out("test.txt");
if(!out) {
cout << "Cannot open file.\n";
return 1;
}

out << 10 << " " << 123.23 << "\n";
out << "This is a text.";
out.close();

ifstream in("test.txt");
if(!in) {
cout << "Cannot open file.\n";
return 1;
}

in >> i;
in >> f;
in >> ch;
in >> str;

cout << i << " " << f << " " << ch << "\n";
cout << str;

in.close();
return 0;
}
I/O Constructors constructors
bad true if an error occurred
clear clear and set status flags
close close a stream
eof true if at the end-of-file
exceptions set the stream to throw exceptions on errors
fail true if an error occurred
fill manipulate the default fill character
flags access or manipulate io_stream_format_flags
flush empty the buffer
gcount number of characters read during last input
get read characters
getline read a line of characters
good true if no errors have occurred
ignore read and discard characters
open open a new stream
peek check the next input character
precision manipulate the precision of a stream
put write characters
putback return characters to a stream
rdstate returns the state flags of the stream
read read data into a buffer
seekg perform random access on an input stream
seekp perform random access on output streams
setf set format flags
sync_with_stdio synchronize with standard I/O
tellg read input stream pointers
tellp read output stream pointers
unsetf clear io_stream_format_flags
width access and manipulate the minimum field width
write write characters
 

ROOT system provides a set of OO frameworks

AddThis Social Bookmark Button

The ROOT system provides a set of OO frameworks with all the functionality needed to handle and analyze large amounts of data in a very efficient way. Having the data defined as a set of objects, specialized storage methods are used to get direct access to the separate attributes of the selected objects, without having to touch the bulk of the data. Included are histograming methods in an arbitrary number of dimensions, curve fitting, function evaluation, minimization, graphics and visualization classes to allow the easy setup of an analysis system that can query and process the data interactively or in batch mode, as well as a general parallel processing framework, PROOF, that can considerably speed up an analysis.

 

Thanks to the built-in CINT C++ interpreter the command language, the scripting, or macro, language and the programming language are all C++. The interpreter allows for fast prototyping of the macros since it removes the, time consuming, compile/link cycle. It also provides a good environment to learn C++. If more performance is needed the interactively developed macros can be compiled using a C++ compiler via a machine independent transparent compiler interface called ACliC.

The system has been designed in such a way that it can query its databases in parallel on clusters of workstations or many-core machines. ROOT is an open system that can be dynamically extended by linking external libraries. This makes ROOT a premier platform on which to build data acquisition, simulation and data analysis systems.

Download : http://root.cern.ch/drupal/

 

C++ Coding Conventions

AddThis Social Bookmark Button

Naming conventions

For naming conventions we follow the Taligent rules. They have written a very large body of C++ and their rules seem well thought out. No need to invent something new. The only addition/change we made is to append an _t to typedefs and simple structs, e.g.:

typedef int Int_t;
struct Simple_t { ..... };

Addherence to the rules is mandatory. Any deviation will cause confusion and chaos. After a while one really gets used to the fact that all class fields start with an f followed by a capitalized word, fEnergy, or that TCollection is a class. If the convention is sporadically violated debugging becomes a nightmare. The usage of a standard begin letter or token for the different types also makes it easy to parse and search the code using simple tools.

Class definition conventions

Also here the Taligent guide is quite reasonable. Of course, no class data member should ever be public (OO remember). Make the data fields always private. Or protected, if you want to grant an inherited class direct access.

Inline

Add trivial get or setters directly in the class definition. This improves reading time since one does not have to look for it somewhere else. Add more complex inlines (longer than one line) at the bottom of the .h file. Creating separate /.icc/ files increases the build time, the complexity of the /Makefile/ and, more importantly, increases the number of files one possibly has to scan to find a piece of code (remember code is write once, read often).

Declaration Order

In the class definition we first declare all private data members, followed by the private static members, the private methods and the private static methods. Then the protected members and methods and finally the public methods (remember no public data members). We put private members first since that is the language default and it gives the developer a quick view on what data members are used in a class.

Avoid raw C types

Avoid the use of raw C types like int, long, float, double when using data that might be written to disk. For example, the sizes of int and long are machine dependent. On 32 bit machines ints and longs are 32 bits, but on 64 bit processors an int can be either 32 or 64 bits and a long 64 bits, depending on the processor. For portability reasons and consistent numerical results use the typedefs provided by ROOT's /Rtypes.h/ for the basic raw C types. E.g.:

#ifdef B64
typedef long Long_t; //large 64 bit integer
typedef unsigned long ULong_t; //large 64 bit integer
#else
typedef long long Long_t; //large 64 bit integer
typedef unsigned long long ULong_t; //large 64 bit integer
#endif

Click here to view all ROOT defined types.

Templates

Template support is well evolved for most compilers. However, proper support for the latest template features, like member templates, default template arguments, etc. is still not universally supported. Compilers can take long instantiating templates when linking the program. A few template classes are no problem, but a large number can cause severe degradation in compile and link performance and bloat your binaries due to excessive code replication. This is especially painful in the frequent edit-compile-link cycles during code development.

Exception handling

Exception handling is reasonably well supported by most compilers. Here, however, no link time penalty but a run time penalty is incured (the run time needs to keep track of the resources to be freed when an exception is thrown). Don't let every method throw an exception when a simple error return code is often enough.

RTTI

RTTI, run-time type identification, is available for all classes having at least one virtual method. However in the ROOT environment there is hardly any need for the compiler provided RTTI since the ROOT meta-classes, TClass, et al., provide much more complete type information.

Namespaces

In ROOT 5 all classes are in the ROOT namespace. Some packages will be in a sub-namespace, e.g. ROOT::Reflex. For backward compatibility with the previous versions of ROOT, where all classes were in the global namespace, we have by default using namespace ROOT; in all headers. However, this can be turned off by defining the USE_ROOT_NAMESPACE macro.

Using comments to document the code

Using the information stored in the dictionary and the source files ROOT can automatically generate a hyperized version of the header and source files. By placing comments in a few strategic places a complete reference manual, including graphics, can be generated using the THtml class.

There are three different kinds of comments used by THtml.

Data member description comments

Comments behind a data member definition in the header file, e.g.:

Float_t     fEnergy;     // the particle energy

The comment "the particle energy" will be put in the dictionary and used whenever fEnergy needs to be described (e.g. on-line help). Have a look at a raw header file and compare it to the hyperized version.

Class description comments

A comment block at the top of the source file describing the class. This block has to be preceeded by a line containing a C++ comment token followed a delimiter string. All comment lines after this starting line are part of the class description, e.g.:

//______________________________________________________________________________
//
// TPrimitive
//
// This class is the abstract base class of geometric primitives.
// Use this class by inheriting from it and overriding the
// members, Draw() and List().
//

For an example of a class description comment block see this source file and compare it to the hyperized version.

Read more: C++ Coding Conventions

 

STL queue and priority queue examples

AddThis Social Bookmark Button

/* STL queue and priority queue examples */

#include <iostream>
#include <queue>

using namespace std;

/* simple queue example */
void functionA()
{
queue <int> q; //q is a queue of integers

q.push(2); //put 2, 5, 3, 2 into the queue
q.push(5);
q.push(3);
q.push(1);
cout<<"q contains " << q.size() << " elements.\n";

while (!q.empty()) {
cout << q.front() << endl; //print out the first element in the queue
q.pop(); //remove the first element of the queue
}
}

/* simple priority queue example */
void functionB()
{
priority_queue <int> pq; //pq is a priority queue of integers

pq.push(2); //put 2, 5, 3, 1 into the priority queue
pq.push(5);
pq.push(3);
pq.push(1);
cout<<"pq contains " << pq.size() << " elements.\n";

while (!pq.empty()) {
cout << pq.top() << endl; //print out the highest priority element
pq.pop(); //remove the highest priority element
}
}

/* example of priority queue where lower integers have higher priority */
void functionC()
{
priority_queue <int, vector<int>, greater<int> > pq; //pq is a priority queue of integers that uses
//a vector of integers for storage and uses >
//to determine priority. This means that if a > b,
//a has *lower* priority than b.

pq.push(2); //put 2, 5, 3, 2 into the queue
pq.push(5);
pq.push(3);
pq.push(1);
cout<<"pq contains " << pq.size() << " elements.\n";

while (!pq.empty()) {
cout << pq.top() << endl; //print out the highest priority element in the queue
pq.pop(); //remove the highest priority element
}
}

/* define a Height class */
class Height
{
public:
Height() {}; //default constructor
Height(int x, int y) { feet = x; inches = y; } //constructor
bool operator<(const Height&) const; //overloaded < operator

int get_feet() const { return feet; } //accessor methods
int get_inches() const { return inches; }

private:
int feet, inches; //data fields
};

Read more: STL queue and priority queue examples

 

Page 1 of 10

Projects

AddThis Social Bookmark Button
image Art of Programming Contest (Special edition for UVa Online Judge users)
This book is compiled by Ahmed Shamsul Arefin (and Steven Halim) to help new contestants and programmers. This is Must Read book and suggested by many university contest pages around the world. The book covers several important topics related to the development of programming skills such as, fundamental concepts of contest, game plan for a contest, essential data structures for contest, input/output techniques, brute force method, mathematics, sorting, searching, greedy algorithms, dynamic programming, graphs, computational geometry, Valladolid Online Judge problem category. Download - Mirror
AddThis Social Bookmark Button
imageProgramming in C
Another C book for new programmers (co-authored by Ahmed Shamsul Arefin), aims also the ACM Contestants. This book is locally published in Bangladesh and no Online Edition is available.
AddThis Social Bookmark Button
image Fundamentals of Computer
Another initiative by Ahmed Shamsul Arefin (co authored with Dr. Md. Haider Ali) to popularize computing . This book is locally published in Bangladesh. Demo

Contest Campaign

Links

ACMSolver Weblinks
Websites usually link back to us
User Articles

Articles Created by Users

Sponsors

Member Login

Who's Online

We have 85 guests online

JoomlaWatch Stats 1.2.9 by Matej Koval