Wednesday, May 21, 2008

C/C++ If statement

Lately, I was been reading C/C++ Coding Styles. It talks about parenthesis-ing, spacing, alignment, etc...

I also recommends some common software practices, such as putting the numerical constant first in the comparison statements like if, while, for and ?:

for example:


if (1000 < stack_size) {
...
}

while (10 != num) {
...
}


for (int i=0; 20 == value; ++i) {
...
}


Why does it emphasize this rule ? It will help you to avoid ambiguous assignments in the conditional statements.

A common mistake in for loop
=====================


for(int i=0;i < X; i++)

whenever we write "for" loop we are tend to use the post-fix operator ++ instead of pre-fix ++. First one need to understand what is the difference between i++ and ++i, and when to use what.

i++ is more complex and takes more clock cycles than ++i. So please make it a habit to use ++i instead of i++, this may make your code a little bit faster.

Monday, May 19, 2008

My Birthday and Pat & Stan

I have done my birthday shopping last weekend. Tomorrow (21-may) is my 30th birthday. hmm...

I feel good,

Miles to go before I sleep,
Miles to go before I sleep ....


My kid Rohan and myself love watching The Owl and Pat&Stan from JetX Channel. The imaginative work involved in these cartoons are very cool. I have a Pat&Stan singing "In the jungle" in my blog.

I'm making steady progress in my goals and objectives in my life. It is very slow, but very steady.

1. I'm doing meditation on the weekends
2. Solved 1 ACM (2062) last week
3. Reading The C++ Programming Language

Wednesday, May 14, 2008

Make "make" work for you

Make - tool to control the compilation and creation of executables. I was hacking into this tool for quite sometime. I feel "make" is good for all the linux/unix c/c++ programmers.

Here is a simple Tutorial of Make:

source file (helloworld.c)
========
#include ...
int main() {
using namespace std;

cout<<"hello world\n"; return 0;
}
simple makefile(gnumakefile/makefile)
===========
helloworld: helloworld.c
TAB g++ -o helloworld helloworld.c

TAB - is important, otherwise gnumake will prompt for error.

command
=======
make TARGET

make helloworld

Example 2
========

/* sum.c */

int sum(int a, int b) {
return a+b;
}

/* diff.c */

int diff(int a, int b) {
return a-b;
}

/* main.c */

int main() {
...
c=sum(20,30);
...
d=diff(30,23);
...
}

/* Make file */

CC = g++ /* which compiler*/
CFLAGS = -Wall -g -ansi /* compiler flags */

sum.o: sum.c
$(CC) $(CFLAGS) -c -o $@ $<
diff.o: diff.c
$(CC) $(CFLAGS) -c -o $@ $<
main: sum.o diff.o main.c
$(CC) $(CFLAGS) -o $@ $^
clean:
-rm core *.o main
Special Tags
=========
$@ -- current target name
$LT -- First name of the prerequisite
$^ -- List of all prerequisite
$* -- List of all prerequisite (without prefix)

So, dudes, start using make. It is cool, it makes life a lot easier for c/c++ programmers.

Monday, May 12, 2008

My Study on Virtue

We use some words without knowing what is the exact meaning of it. One such word is Virtue, What does it mean ?

Virtue - it stands for Moral Excellence of a person.

The four cardinal (hinge) virtues are
1. Justice
2. Courage
3. Wisdom
4. Moderation

The four classic Western cardinal virtues are:
1. Temperance - Self discipline
2. Prudence - Wisdom
3. Fortitude - Courage
4. Justice

Buddhist practice as outlined in the Noble Eightfold Path can be regarded as a progressive list of virtues.

1. Right Viewpoint - Realizing the Four Noble Truths
2. Right Values - Commitment to mental and ethical growth in moderation
3. Right Speech - One speaks in a non hurtful, not exaggerated, truthful way
4. Right Actions - Wholesome action, avoiding action that would do harm
5. Right Livelihood - One's job does not harm in any way oneself or others; directly or indirectly
6. Right Effort - One makes an effort to improve
7. Right Mindfulness - Mental ability to see things for what they are with clear consciousness
8. Right Meditation - State where one reaches enlightenment and the ego has disappeared

The Four Noble Truth:

1. The Nature of Suffering
2. Suffering's Origin
3. Suffering's Cessation
4. The Way Leading to the Cessation of Suffering - (It is the Noble Eightfold Path)

Capital Vices

1. Pride or Vanity — an excessive love of self
2. Avarice (covetousness, Greed) — a desire to possess more than one has need
3. Lust — excessive sexual desire. "lust detracts from true love".
4. Wrath or Anger — feelings of hatred, revenge or even denial, as well as punitive desires outside of justice
5. Gluttony — overindulgence in food, drink or intoxicants, or misplaced desire of food as a pleasure for its sensuality
6. Envy or jealousy - resentment of others for their possessions "Love of one's own good perverted to a desire to deprive other men of theirs")
7. Sloth or Laziness - idleness and wastefulness of time allotted. Laziness is condemned because others have to work harder and useful work can not get done.

----- Collected from Wiki ----