How do airlines calculate center of gravity? I have read in several airlines websites that they optimize the center of gravity position, to improve aerodynamic performance and reduce fuel consumption. Do they use some kind of software to do this?
Most airlines of any size use software to calculate the c.g., either their own software or vendor software.
Passenger aircraft weight & balance is relatively trivial compared to cargo aircraft w&b, where such things as floor loading, lateral imbalance, and monocoque limits can come into play.
http://www.champ.aero/ is one provider of that service and software for cargo aircraft for those companies not wishing to program their own.
Fuel efficiency is one advantage of loading to given c.g. An oft-used method is to load to a specified zero fuel weight c.g. For example, on 747 aircraft through the -400 models, a zfw c.g. of 26.6% mac is a typical target.
If you want to see and play with a free, online, open-source w&b system for cargo aircraft, go to http://terryliittschwager.com/WB/index.php.
Airlines are allowed to calculate center of gravity by using legally defined assumptions for what the weight of each person will be. It might vary between countries, but normally there are numbers for men, women, children under 12, and infants up to 2 years old. These weights can be added to in the winter to account for extra clothing in some regions. All the information is sent into a computer thats then says if they are a go or not.
In theory there are much better was this could be done, for example adding a weighing system to each leg of the landing gear would allow precise control. In the end though things move around in flight and the baggage handlers aren't exactly known for their careful placement of luggage, best you will probably get is put more stuff that way.
In flight would seem a little trickier, however most aircraft record data such as angle of attack, flight speed, ambient pressure, and control surface position. Using a spreadsheet generated from models, an aircraft could easily determine its approximate cg by comparing how much lift it should be producing, where the center of pressure on the wing should be, and how much the horizontal stabilizer is pushing downwards.