Bismillah

Bismillah

Sunday, December 26, 2010

Common Programming Mistakes We Always Do

Hey Guys!!!!!!!!!
So, in this post, I will tell some common mistakes, which we always do.... Even I do sometimes now.... But I used to repeat same mistakes again and again when I was newbie in programming. SO, I just thought that it would be helpful for you, if I alert you about those mistakes. Let's start it from here using a list.... :P

1. Undeclared Variables
The most common programming mistake, we do, is just we use variables before declaring them. So compiler becomes angry on us and fights for its right i-e declaration of variables. And we just don't give it its rights, what we do is we throw our laptops on bed, shout on it and rush from our room, and finally while closing doors with full force, we just say 
 I HATE PROGRAMMING MAN I JUST HATE IT 
Here is an example of what I am saying.
int main()
{
  cin>>x;
  cout<<x;
}
 "Huh! Heyyyyyyyyyyyyy" Oh my God!!!!!!!!! Why Do I get an ERROR?............. :( "
Your dearest friend, whose name is Microsoft Visual Studio, does not know about x. So make him a favor on him. And declare variable x. He will not complain after that.
 int main()
{
  int x;
  cin>>x;
  cout<<x;
}
"Whoa!!! WhooooooooooooWHOOOOOO....... I made it.... I finally did it..... :) "

2. Uninitialized Variable
But some times, we declare our variables but we forget to initialize them. So compiler gets angry on us. In the reaction, we get angry on compiler. Thus, in the result, we just exit our compiler and start watching movies. (not we, I watch movie when i exit compiler... :) ). Here is an example...
int count;
while(count<100)
{
  cout<<count;
}

"Oh my God... Why doesn't my program enter the while loop? :( "
Now after declaring variable count, it has been initialized to a garbage value. So, compiler doesn't know about value. So, on what basis, he should enter in loop. So don't worry, just make count equal to zero or any other value. And you are done....
TIP: Always make your variable equal to zero at time of declaration.


3. Setting a variable to an initialized value
So when we forget initializing our variables, then a new problem occurs. That is we just enter formulas and  mathematical expressions on other variables. However, some variables have garbage value, then how can compiler evaluate those expressions. If YES, then on what basis? So, in the result, compiler gives an ERROR. Here is an example:
int a, b;
int sum=a+b;
cout<<"Enter two numbers to add: ";
cin>>b;
cout<<"The sum is: "<<sum;
When Run:
Enter two numbers to add: 1 3
The sum is: -1393

"Every thing is perfect, then why sum is a negative number?
I know compiler has problems. Let's try to reinstall it."
No there is no problem in compiler, but there is problem in your program. Often some beginning programmers believe that variables work like equations - if you assign a variable to equal the result of an operation on several other variables that whenever those variables change (a and b in this case), the value of an variable (sum in this case) will change automatically. No, in C, assignment operator ( = ) does not work this way: it's a one shot deal. Once you assign a value to a variable, it will be same until you reassign the values. In the example, because a and b are not initialized, sum will be any garbage number. Then it does not matter what user inputs in a and b. Here is solution of this problem:
int a, b;
int sum;
cout<<"Enter two numbers to add: ";
cin>>b;
cin>>a;
sum=a+b;
cout<<"The sum is: "<<sum;
"Yeah!!!!! hooooooo!!!! I made it... Its time to submit it.... :) "
 
4. Using a single equal sign to check equality
Here is a very common mistake we do. Even I sometimes do it mistakenly. Sometimes, we just compare two variables with single equal sign in any if condition or in any loop. Compiler will not give you error on this problem, but your program will not work as you want it to do. So please be careful. Sometimes, this problem becomes very dangerous for your program as well as your computer. Here I show you an example,
char x='Y';
while(x='Y')
{
  //...
  cout<<"Continue? (Y/N)";
  cin>>x;
}

"Why doesn't my loop ever end?"
When you use a single equal sign to check equality, then instead of checking equality, it performs assignment. It means it assigns right side value to left side (in this case, x will become 'Y' on every iteration). In the end, either your loop never ends or never start (in this case, x becomes 'Y' every time, that is true, so your loop will not exit. It keeps on running and running making your brain hot :) ). Always use double equal sign to check equality carefully. Further more, to avoid accidental assignment, put your variables on right side and values on left side. When you put single equal to check equality, then compiler will give you an error, that he cannot put values in constants. So, you will get it at compile time. Here I show you example of it:
char x='Y';
while('Y'==x)
{
  //...
  cout<<"Continue? (Y/N)";
  cin>>x;
}

"Yes! Yes Yes Yes!!!! I made it."
char x='Y';
while('Y'=x)               //Compiler ERROR
{
  //...
  cout<<"Continue? (Y/N)";
  cin>>x;
}
"Huh! Compiler Error.... Oh yeah! I forgot to put two equal signs. :) "

5. Undeclared Functions
Sometimes, like variables, we call functions before declaring them. So, always put prototypes of functions above main, and definitions below main. And call them in any order, you will not get any error. Here I show you an example,
int main()
{
  menu();
}
void menu()
{
  //...
}

"Why do I get an error about menu being unknown?"
The compile, off course,  don't know about menu. Because you are calling menu() before declaration or definition. So, just put a prototype before main() and you are done.
void menu();
int main()
{
  menu();
}
void menu()
{
  ...
}

"That's what I wanted to do for last 4 hours. :p"


6. Overstepping Array Boundaries
Sometimes, we put indexes of array, which goes beyond array boundaries. Like if array is of 10 elements, we use number 10 in an index. Although we are allowed only from 0 to 9 not 0 to 10. And remember, in this case compiler will not give you error. It just will run your program and do what program says. Here is an example:
int array[10];
//...
for(int x=1; x<=10; x++)
  cout<<array[x];
"Huh!!!! Why doesn't my program showing me first number?"So you are going from 1 to 10. Although array only exists from 0 to 9. So be careful when using arrays. So, always start from 0 and end at < arraySize. That is tip, which can be applied on loops.
int array[10];
//...
for(int x=0; x<10; x++)         //Start from 0. End when less than Array Size
  cout<<array[x];

"Finally, I got it........... :)"

So dear guys, I make sure your program does not contain these mistakes. And be careful at the time of programming. I will keep on posting other basic and common mistakes. Please keep on reading it....

Regards,
Wajahat Karim
Author, Dreamers Are Developers

Saturday, December 25, 2010

Tips on Improving your Programming Skills

The question that novice programmers often ask is, How to improve programming skills? As a programmer I used different ways to improve my programming skills, like reading/writing code, working with experienced programmers and reading blogs and websites. I have listed few of the points here which in my opinion will be quite useful for novice programmers.
Practice, Practice, Practice – The obvious way to practice is to write programs. Try to make as many programs as you can. To learn, to effectively practice, you need to work on things that aren’t easy for you. Always create a schedule or structure that you follow. After all practice makes a man perfect.
The most important thing is to NEVER STOP LEARNING. I think one should always try to make a conscious effort to improve.
I believe on “learn one programming language every year or semester” system as each programming language changes the way we think about programming.
Looking back at the code you have written in the past and realizing just how bad it was. As Churchill says, “Those that fail to learn from history are doomed to repeat it.
Pair programming with other programmers might increase the quality of code, broadened your horizons, and help you work in as a team member.
Reading and figuring out code written by different people makes you differently.
Write documentation for code written by other people. This helps in understanding the way other people think and program.
Explaining something really helps you find the gaps in your own knowledge.
Get involved with open source projects. There are many open source projects that have experienced software developers working on them.

Courtesy: http://www.mycplus.com/featured-articles/ten-tips-on-improving-your-programming-skills/

Regards,
Wajahat Karim
Author, Dreamers Are Developers

Tuesday, December 21, 2010

File Handling in C

Assalam-o-Allaikum

Today, I got a request for How to do File Handling in C? So I have given a link from there you can read about File Handling in C. Along with it, I have also uploaded a C file, in which both reading and writing to files have been done. Download it and do experiments on it.

Note: I have just did reading and writing on Characters (char) and Strings (char [ ] ). So do experiments on int, double, float yourself. In case of any problem, just post a comment on this blog. I will give you answers.

Reading Article on File Handling in C
http://www.mycplus.com/tutorials/c-programming-tutorials/file-handling/

 Download File Handling in C
http://wajahatmemon.uuuq.com/Dreamers_Are_Developers_Blog/C_File_Handling.c



Regards,
Wajahat Karim
Author, Dreamers Are Developers

Tuesday, December 14, 2010

Lecture on Console Graphics for BE(SE)-1A

Hey guys!!!!!
It was a very great to see you in classroom. I and my project mate (Sheeraz Ahmed) really enjoyed it by presenting on Console Graphics. So far, if you have any type of question, even not from Console Graphics, you can post a comment on this..... We will give you the answers....

Regards,
Wajahat Karim
Sheeraz Ahmed
Authors, Dreamers Are Developers

Monday, December 13, 2010

Adding Animations in C/C++ : Console Graphics Series

Dear members, here I am back again with Console Graphics. Following link downloads a file, in which you will find a code on adding animations in your console screen text... Use your arrow keys to control your character. Download it and enjoy.... In case of any problem, leave any comment......

http://wajahatmemon.uuuq.com/Dreamers_Are_Developers_Blog/Simple%20Animation.zip

Regards,
Wajahat Karim,
Author, Dreamers Are Developers

Adding Colors in C/C++ : Console Graphics Series

Dear members, here I have uploaded some data about Console Graphics. Following link downloads a file, in which you will find a code on adding colors in your console screen text... Download it and enjoy.... In case of any problem, leave any comment......

http://wajahatmemon.uuuq.com/Dreamers_Are_Developers_Blog/Color_Project.zip

Regards,
Wajahat Karim,
Author, Dreamers Are Developers

Welcome to Dreamers Are Developers

Hi friends!!!!
How are you doing? I, Wajahat Karim, appreciatively  welcome you to my very first blog "Dreamers Are Developers". This blog will contain all information about Programming in different languages including C/C++, VB.Net, C#.Net, Web Development, OpenGL, Qt Toolkit, Android Development and more....

Please keep visiting and posting on this blog......

Thanks for visiting this blog....

Regards,
Wajahat Karim
Author, Dreamers Are Developers