Tuesday, January 29, 2008

Some project ideas in C++

Here is a collection of project ideas that can be easily implemented in c++ to try your hands at the game.. I will list them in order of difficulty (low to high)
  1. Basic operator overloading - try creating as many as possible combinations of function name and rest of the function signature - purely syntactical exercise.
  2. Create a function that can convert an "int" type into a "string" containing the Binary representation and vice-versa.
  3. Implement a rotated array solver - a rotated array means the elements of a "sorted" array are rotated and wrapped around from last position to 0th by rotating factor - 'r'
    1. So after rotation, the array's element Ai will be at ( ( i + r ) % arraylength ). Now suppose you are given such an array, which is already rotated by some unknown rotation factor - 'r', create a function to bring the array to original sorted form without using sorting directly. Keep in mind performance parameters in terms of speed and memory and elegance of code.

Labels:

Monday, August 28, 2006

A sneak peek on Data Structures

Data Stuctures...hmm when i started writing this post i thought why am i jumping to data structures so early? But then i thought since i was being a lazy bum for quite a while so why not Data structures; it's better than nothing! (and i can't not think of anything else at the moment)

So , Data structures are here... The first question that one might ask is; seemingly what are data structures?? But there is an obvious answer in the name of the topic itself!

Data- it means any set of raw values in any format which we are interested in so that we can collectively process it to yield useful information!

Structure- it means any way of organizing. Be it data, variables or any heirarchy! It can be anything. Did you know that orgzanizing residential space can also be considered a sturcture, of course, high rise buildings are called super-structures in general terms..
Ok, so we have data and structures; 1+1=2. Yes, it is as simple as that! Organizing data according to specific parameters to yield useful information is creating a data structure.

Types: Some simple types are- Que, Linked list, Stack, Circular que, Tree etc;

-> que is simple, its just like data standing in line, with the first data packet getting processed first.
-> Linked list; when one data packet links to other, it is called a linked list. Its like elephants moving in circus holding each others' tail. ;)
-> Stack, just like plates stacked on top of each other.
-> Circular cue; what starts also ends at the same point.
-> Tree; the first thing that comes to one's mind when they hear the word tree is 'branch'. So data branching out towards other data is called a tree structure.

Just keep these simple examples in mind and you will have a very good understanding of D.S when you read about it's implementation.

Sunday, July 16, 2006

Calling Polly - Polymorphism

'POLYMORPHISM' - The computer geek's buzz word.
one thing that drives the software industry is polymorphism! It is a very strong concept. To say, it is not as hard to master as it sounds, and at the same time it is not as easy to master as it seems from this statement. Boggled ?? Well dont be ! Lets make polymorphism simple.
POLYMORPHISM -> 1. FUNCTION OVERLOADING.
2. SCOPE RESOLUTION.
An easy way to understand things is to divide them into logical parts, ans as they say divide and rule....
1. Function overloading - this is what packs most of the punch.

Function overloading means using the same function name to call different functions according to need. It can be understood like this, your dining table functions as a table/surface on which food is served and eaten, while some times it also serves as table/surface on which the family board games are played. The world is full of such implementations of overloading. Your dad uses the computer to do his office work, you use it for playing games.
The point is that you use the same thing for different roles as per need.

int myfunc (int a, int b)
{
return (a+b);
}
int myfunc (char a, char b)
{
cout<}

here the functions have the same name but different parameter list and different body. With practice and further analysis you will understand it better.

2. Scope resolution - same variable name, but different variable (in distinct scope)

C++ provides a way to declare two variables having the same name but different their root scopes are disctinct, although they can overlap.

{ int a=1;
{
int a=2;
{
int a=3;
cout< }
}
}

Note: some programmers dont feel that scope res. should be called a part of polymorphism.

Saturday, May 13, 2006

One difference between C and C++

It is worth noting that one major difference between C and C++ is the way the code is documented, that is to say that the layout of various items like declaration and initialization as well as the position of the main function is fixed in C.

while C++ provides a more flexible approach in the same matter. while coding in c++ you can initialize anywhere in between the code, and declare and define any function keeping in mind just that it is not called before it's declaration. so that is an added benefit on coding in C++.
actually this can be thought of to be a direct consequence of the ability of a C++ code to perform late binding. SO you can call the free store of memory any time you want.

example:
In C the following code is invalid, but valid in C++:

void main( )
{
int a;
clrscr( );
int b;
}
here initialzing the interger 'b' after calling the function clrscr( ) would lead you to an error.
while at the same time, it's a perfect little code in case of C++.

that is why in case of C++ you can write the for loop like this:

for(int i=0; i<10;>

Keep this fact in mind to take your programming skills to a better level.

Friday, March 10, 2006

Knowing data abstraction

Data abstraction is an important part of OOP- Object Oriented Programming.
Herein a group of data holding members are thought of as Objects having their own behaviour and interaction protocols towards other objects and the global environment.
These objects exhibit their behaviour through functions which can access them.
So basically Abstraction is the process of hiding the information that is at work in the background. Example: A car is known as a system(object) comprising of four wheels(functions to interact with th road), a steering wheel (a function that interacts with you, the user) ... the gearstick(function that interacts with the user again), but you do not know the nitty-gritty of the complex gear system yet you can easily use it to your advantage. Now this is a working and moving example of abstraction....

Wednesday, November 02, 2005

BASICS TO KNOW

THE INCLUDE SECTION:

1> WHAT ARE LIBRARY FUNCTIONS?

They are the routinely used functions which are made use of in almost all the programs.
They are stored in the header files which are included in the standard package in the folder generally named as ' Include'. Thewy header files store the defination of these library functions so you can use them by simply including those header files in your program and then calling those functions with whatever arguments needed.

example : #include

This header file is better known as : Input-Output-Stream.header

So, as the name suggests, it includes the defination of the commonly used input and output functions.

Thus, this facility helps you to avoid writing the code for the same functions again and again.
And the good news is that you can even create your own header files which define commonly used functions, classes and variales used generously all over your proejct.

Thursday, September 22, 2005

STRUCTURE OF A GOOD 'C/C++' PROGRAM

There are certain basic aspects that should be kept in mind; such as the format of the code, indentation, comments, smart variables etc..

As for the basic layout, here is a tip:

1> Documentation:

This should contain the objective of the program, the creator's name and date if neccessary and other details about the program. It should be in the form of a multi-line comment.

Example: /* Program to add two numbers.
created on: 22 sept-2005, by:urjit */

2>Linking of files and Inclusion:


This section should be used to link to all header files, external files and the library functions. Macros could also be placed in this section.

Example: #include
#include
define PI 3.14

3>Main program:


This section should ideally contain the classes (in case of C++), global variables, filestreams, and the body of the main( ) function, containing all the sub-functions, local variables, objects etc..



4>Functions and extra comments:


This section should contain the extra information and notes about debugging if required (untill the code is perfected), other functions that are to be used in the main program (for defining the functions here, they have to be declared above their control entry; i.e., generally above main).



Example:
/*program to add two numbers. created by:xuxux created on: 30/6/05 */