Saturday, October 23, 2010

Summing two Fractions entered by user

This is also a question fro beginners. You have to take input from user in fraction form like
3/4, 7/9 etc and then add them and display answer in the form 4/5.
Here is the code
Figure: Program that takes two Fractional inputs and sums them to display result
Similarly, you can try many simpler logics with mathematical formulas. Try to implement the formula for square of sum of two numbers.

A Program that converts gallons in to Cubic Feet

This is a starter question:
Write a C++ code that takes an input from user as gallons and converts them in to cubic feet to display how many cubic feet are present in entered number of gallons.
Figure: A C++ code and output for gallon-cubic feet conversion
The above program takes input from user and converts it to gallons.

Writing a Simple C++ Program


Printing a string to console…

Code


Figure: Printing a String on the Console's Output Window



Explanation

In the above example "# include " is the preprocessor directive which tells the compiler to add a file from library.

"iostream.h" is a header file which controls basic input/output commands.

"using" is a directive while namespace is the part of program in which names are recognized, in this case "std".

"void main()" is the main function which is always executed at the start.

"{}" are function body delimiters.


 

Wednesday, October 20, 2010

Data types and variable in C++


Now that you have had an introduction to the origin and history of C++, we'll start our introduction to C++ variables and data types supported by C++ language. Many IDEs are used for C++ language like TurboC, Developer C++, VSC++ etc. You will have to download one of these in order to start practicing C++.
C++ supports many data types including
Character data type = char, takes 1 byte
Boolean data type = bool, takes 1 byte
Short data type = short int or short, takes 2 bytes
Integer Data type = int, takes 4 bytes
Long Data type = long int or long, takes 4 bytes
Floating data type= float, takes 4 bytes
Double data type = double, takes 8 bytes
Long double data type = long double, takes 8 bytes
Declaration of Variables
Declaring a variable means that you are telling your compiler to reserve a place for your variable in memory, so whenever you declare a variable, you compiler reserves number of bytes required by the data type of that variable in memory.
For example
float myvariable;
is a statement that will allocate a memory of 4 bytes to myvariable.
C++ compiler works statement by statement and a statement in C++ always ends with a semi-colon. If you forget to put semi colon you will not be able to go past compiler and an error will be issued.
Definition of a variable
After the declaration you will need to define a value for the declared variable.
For example
int myvar = 3;
This statement also called the assignment statement, basically defines a value for the variable myvar.
Multiple Definitions and Declarations
Multiple variables can be defined and declared at the same time. For example
int a,b,c;
float y=1.9, t=0.9;
So this is just the beginning, there is a lot more to come. So stay tunedJ




 

Tuesday, October 19, 2010

A Little Bit about C++

            There are many of you who have heard about C++. Yet, you might not know what it’s about really. That is why we are here to give you a little bit of knowledge about what C++ is. For starters, C++ is a language. This language is statically typed. It’s free formed. It’s also in a multi-diagram format. Basically, it serves a general purpose as it’s used in programming. What it all boils down to is that it’s the official language of programming.
            This official language began in 1979 when the C with Classes was offered by Bjarne Stroustrup. He got this idea when working on this thesis paper for his Ph D. He looked at the different languages that were out there and found Simula to be too slow. He found BCPL to be too fast. What he frankly did was taken the Simula formula and adds a few things to it so that it was better and faster.
            In 1983, the name was changed. What started as C with Classes was then changed to what it is now known as. This is C++. Virtual functions were added at this time to make it even better than before. Then in 1989, a new release known as 2.0, was put out on the market. In 1991 they updated this yet again and it was put out on the market in 1991. Many advances have been made. They even created a manual. From there, they went all out. They created templates. They created exceptions and name spaces. They also created a Boolean type.
            As with anything in life, there are some good things about it and some bad things about it. What are a few of these things? Well, for those of you who think that C++ can’t be all that bad, the biggest downfall that people complain about is that it is hard to learn. Besides that, there are a few other downfalls as well.
            For instance, the templates that they create which templates are a good think; they can’t be linked or distributed to a library. Something else you will find is that C++ is not compatible with compilers either.
            The other thing is that some of the developments that they have created didn’t work for one simple reason. This reason was that it didn’t work with some programs. For instance the Visual C++ didn’t work with Windows 3..0
            Some will find that bigger companies favor C++. You might wonder why as Java was created. Well the reason for this is because of the fact that C++ is normally better when using robust applications. So, don’t be alarmed if the company sticks with C++ even though it has its downfalls.
            Templates are the biggest pro about this. The other pro is that since this language is becoming so well known, there are more resources you can use to learn this faster. This is something that you can find works in your favor.
            Some of you might be wondering what has been happening with C++ as of lately. There are some developments that you should know about in regards to C++ and its developments. While we know that when C++ first came out, it couldn’t be used with compilers, they have created the Intel C++ compiler 8.0.
            The other thing that is going on with C++ has to do with the new Android. Google has launched a kit. This means that third parties using the Android applications allow developers who create these applications the ability to use C++ and C. This expands it so much more. This puts them ahead of Java. There are some limitations to this. There are many resources that will allow you to read through this to learn how Iphone application creators can use this to work in their favor as they develop iPhone applications.
            This is what you really need to know about C++ it has come far and many more additions are being created. So, find those online resources and more that can help you learn about what is coming out in terms of advancements in C++. You will find that if you are familiar with C++ then there are many jobs that are looking for you due to the fact that learning this language isn’t the easiest task to do.
           

Sunday, October 17, 2010

Beginner's Guide to Programming



Programming, also known as coding, is the basic tool for programmers to design, test, debug and write computer softwares and applications that you use almost every day. If you want to use software that keeps track of your business details, or you want to develop a website from where you can manage your clients and employees, then programming is the tool for this. Even the drivers that you use for accessing different hardware resources of your computer are available because of programming. All these applications and different driver tools are written using a language. You can develop a new code or enhance your existing code according to the requirements. To develop strong and error free applications whether desktop or online, you may need to have strong understanding of different algorithms and you must also be good at logic. So to become an expert, you always need to take a start. This writing is meant for beginners who want to learn different coding languages.
Due to constant development in hardware and increase in the public demands, programming languages have also experienced developments. Once applications were built in FORTRON or COBOL languages, but with the passage of time languages like C, C++, Java, C#, ASP, Python etc were developed to assist programmers so that they can use these powerful tools to develop fast, easy-to-use applications and software. We are going to consider basics of advance languages like PHP, Python and PERL.
PHP
PHP was developed in 1995 by Rasmus Lerdorf and after its creation it has seen many amendments and newer versions. It is still being enhanced as it is an open source language. PHP is basically a script language which is used at server’s side for dynamic web page creation. PHP code is derived from basic languages like Java and C. It has some functions and methods of its own too. PHP code is used in the HTML text to achieve the “Dynamic” functionality in web pages. When somebody visits your webpage written in PHP, the server then processes the code embedded, for example some calculations, converts the result in to HTML form and shows it to the visitor.  
Uses of PHP
PHP has many advantages over static web page creation languages like HTML.
         It’s easy to understand and implement. It is also available in many languages; you can find documentations and online help easily.
         PHP is a flexible language and can be used in any way you want. Its not mandatory to use OOP concepts or any other conventions.
         When you use PHP, much of your time is saved due to dynamic property it allows.
         Using PHP allows you to gather data about every visitor of your page. Based on that data you can create customized web pages for those visitors.
         PHP allows the programmer to use thousands of tools available online.
         If you are creating an e-shopping website, then PHP allows you to maintain online shopping carts for every user.
         PHP saves system memory too as it does not use all of the system resources for its execution.
         It is a strong language that provides different mechanisms to avoid malicious attacks.
         You can even write your own customized codes for your website and connect them with PHP’s code.

Drawbacks
One of the drawbacks is that PHP’s execution time is a bit slower than the compiled languages like C. The fact that PHP is freely typed can create unexpected problems due to errors in the syntax.

Basic Syntax for PHP

Syntax is basically some rules and conventions that are defined for every language. These rules are important for writing well structured and error-free codes.
In the case of PHP, syntax for coding is same as the coding languages like Java, C etc. But it should always be contained within the tags.
For example:
<?php  your code here?>
Note that you must save your PHP embedded file with the extension .php, otherwise your web page will not function properly.

Sample code:
Following is a sample code in which PHP is embedded within HTML text.

<html>
<head>
<title>Starter’s PHP Page</title>
</head>
<body>
<?php
echo "This is my first PHP web page!";
?>
</body>
</html>

Remember that like C, a semicolon in PHP language shows the end of the PHP statement, so never forget a semicolon at the end of PHP statement.

Python

Python is a high-level language used for programming. Like PHP it is also used as a scripting language. Unlike PHP, it emphasizes on code conventions and proper syntax, so it is not loosely typed programming language. Python is used in variety of paradigms for programming. Along with its use as a scripting language, it is also used for creating non-script source codes. Python is also known as interpreted language which means that the Python code is converted into machine language only at runtime.

Uses and Drawbacks

Python language is not only easy to learn but it is also a very strong programming tool for developers. It can be used for many purposes due to its dynamic nature. It has a large library of tools which are freely available online. This language is very helpful when it comes to customizing your developments. It can be used for desktop as well as online applications. It can be used to develop games and network security measures. Python is preferred over the language PERL, is because of its usability. Code written in Python is not only easy to write but also easy to read and understand. It’s more effective to use OOP concepts in Python than in PERL.
There are some drawbacks too. One of the disadvantages of Python is that it is not good for application which need to process quickly. This is because it is an interpreted language.

Syntax and sample code:

The comment line in Python starts with “#”. Anything written after that hash sign will be considered as a comment by the interpreter. After invoking the interpreter, you will see the sign “>>>” at the command prompt which shows that interpreter is ready to take input.
You can do simple calculation on the prompt command like
>>> 2+2
Or you can assign values to variables and then perform calculation on those
>>> length = 10
>>> length * length * length
You can either print string as
>>> ‘This is a string’
>>> ” This is a string in double quotes.”
Or you can define a string variable as
>>> stringvar = “This is a string variable.”
>>> print stringvar
You can not only create lists, but also use loops for coding.

PERL

PERL is a very powerful high-level language. This language is used for dynamic programming. Since its birth in 1987, it has undergone many changes and enhancements. It has large collection of resources and libraries available free online. Codes developed by using PERL language are also called the Perl scripts. PERL language is perfect for short and straight forward codes.

Advantages and Disadvantages

PERL is an interpreted language. This property has rendered PERL with many advantages and disadvantages. These are listed below.
PERL, as already discussed, is an interpreted language, which means that it is compiled only at runtime. So, PERL written codes usually take more time than a simple C program to compile and execute.
PERL is not system specific and is supported by almost every operating system in use today. It also combines the best features of other languages. On the other hand, creating a binary file from a PERL file is extremely difficult. PERL allows dynamic memory allocation.
Syntax and Simple code

To create a PERL script, you will need a simple text editor application. There are also many IDEs available on net that are solely meant for PERL code. Remember that PERL code must be saved with the extension .pl, otherwise your code will not be functional. One more thing is that PERL does not allow spaces in the file names.
PERL is a case sensitive language like C. It will show an error if you will not take care of case sensitivity of your variable names.
Like Python, PERL comments start with a # sign.
For variables use the following convention to define them.
($) symbol (scalar)
(@) symbol (arrays)
(%) symbol (hashes)
Here is a simple example of a program that prints and assigns values to variables.
print "Content-type: text/html \n\n"; #HTTP HEADER
 
$number = 5;
$exponent = "2 ** 8";
$string = "Hello, Perl!";
$linebreak = "<br />"; #HTML LINEBREAK TAG
 
# PRINT THEM TO THE BROWSER
print $number;
print $linebreak;
print $exponent;
print $linebreak;
print $string.$linebreak;                                  

Resources

For PHP, you can refer to online tutorials like

For Python, you can refer to

For PERL, you can refer to online tutorials like

Conclusion
All the programming languages, be it C or Java, Python or PERL, have some advantages or disadvantages over each other. You can make use of any language and turn your knowledge into developing strong and secure applications. This was just the introduction to programming and few languages used for development purposes. From now on, we will focus solely on the language C++. Stay tuned for more...