Saturday, July 28, 2007

Do you want to be an Author of this Blog?

If you want to share your knowledge and ideas about programming just post a comment.

No money required. Spammers go to hell..

Setting the Number Digits after the Decimal Points in C++ using cout

Without a good book to grasp on how to control the way cout outputs numbers especially floating point numbers could be a misery at times. Here's a brief tips on how to do this:

Fig. 1:

#include // we include iostream since we'll be using cout, since it's definition
// is located in iostream.h file

int main(){
float num = 14.2345; //we directly assign a value to our float num for this example..

cout.setf(ios::showpoint); //this means even if there are no digits after the decimal
// point, just put zeroes

cout.precision(2); //set the number of digits after the decimal point.
cout << "The Number now is " << num << endl; //output the number now..

return 0;
}


That's all.. Try it.. By the way, be reminded that subsequent use of cout once you called
the two lines for setting the number of digits after the decimal points, will be affected, so that means if you have another variable shall we say, myNum and assign it a value of 3.141516
and then you do,

cout << myNum << endl;

it's output will be,

3.14

it will still have the adjustment from the previous settings. So if you don't want it to get affected by the settings for 2-digit decimal point, simply do this,

cout.unsetf(ios::showpoint); //this means reset the original setting of cout's way of outputting
//floating numbers..
cout.precision(0); //

cout << myNum << endl;

the output will be,

3.141516

and it's back to the original setting..

Compared to the printf edition, remember to set the number of digits after the decimal point we simply do something like this,

printf("%0.2f", num);

and everything is set up for us. So in C++ using cout, we'll need to have a bit more toggling before we can set the output for floating numbers according to our needs.

For now, don't mind of those cryptic syntax, the ios::showpoint thing, just stick to the meaning of their purpose first, then once you have a good grasp of the C++, then it's time to be more technical, alright? Trust me, it will save some neurons in your brain.

Take note, this info is for beginners only. So don't flame me you gurus out there.

Till the next tutorials..

Friday, July 27, 2007

Beginner Programmers' Mindsets

This post is for the beginners and wannabes who want to delve into the thrill of programming. If you seek answers to questions you have been asking yourselves, you may want to read on.

It's been a long time I happened to have met different persons who are digging into the intricacies of programming and at the early stage of their learning curve, they gave up because of the difficulties they had during their exploration of programming. They easily bewildered when a programming problem seemed impossible to be solved. Even to the easiest type of problem seems hell to them. Some simply sit around the corner, and forget the idea of pursuing. Some diverted to games, others just sticked to the conventional and use their PCs as mere personal stuffs like household appliances laying static in one corner of the house, just doing single purpose. Others would simply let knowledgeable friends do the job. They simply gave up. That's the greatest and seamless action they could give, Give Up!

Well, you better read on. Let me save the last drop of interest you might still have. First thing's first. You have to evaluate yourself. Yeah, that's the root of everything. Know your strength. This means, if you believe you're an in-born programmer, then go and pick a good book or search the internet for tutorials and read and read and express your knowledge through hands-on. But, if you just happened to be not having the breed, yet you felt there's a little spark of interest inside you, then go find someone who can be your mentor or adviser. You simply need a role-model, an inspiration in other words. Be careful however of your tendency to become dependent on him/her. Take note, first-hand information is still the best. That means, learning that comes from you, is better than the one which comes from others. Learning should start inside you. I have a friend, who's a B.S. Psychology, yet, because of his unwavering interest in programming, he's now a successful web developer and currently doing advanced programming chores. So nobody can tell me that it depends on intelligence, mind you, it's in the INTEREST!

Once you have the interest, I believe, no one can stop you from learning, especially programming. Others have a lot of reasons to cover up their tracks. One said, "I don't have any PC at home, how can I learn?" Well, I only had myself a programming book and a unit of my imagination before. There's also one hacker I remember, he was living somewhere in Alaska, no electricity, no telephones and of course no computers, yet, his eagerness to learn the mystery of computers cannot stop these factors. Now, he's one of the respected hackers of the U.S. That's all it takes. Believe me, the important thing is to boost your interest first and sharpen it until it's needle-like. Once it has reached its peak, then nothing is impossible.

Somebody asked me before, what do you get from programming your PC? Well, I told him, have you seen a bike daredevil? It's the same feeling, once this guy reached the other end out of danger, he felt satisfied, he felt something has replenished his deepest soul, he felt that he owns something which others cannot own. That's the feeling of having solved mind-boggling programs. Having skills in programming sets you apart from others. You can express your thoughts in a language that only fellow programmers can understand. Isn't that unique?

Beginners, who are just new to the scene and most probably those who are just taking B.S. in Computer Science or other related fields, listen and open up your ears. The earth is still young and you still have a lot of time to catch up with the ever-changing computer technology. My words for you, just learn all the fundamentals of programming as much as you can. Focus only on one language first, preferably C or C++ as these two are the most popular languages being used in any other languages, Java, PHP, C#, etc. which are commonly used in building the Internet infrastructure. Your Windows was written in C, C++ and I believe Assembly, so that would be enough background to know their capabilities. Next, buy a book, those Idiot's guides are good but they're quite expensive. If you can't afford, then go for the Internet. Use Google, and only Google! It's fast! For example, you can search for: C++ Tutorials then go and find the links that you think are easier to understand. If you have some friends who are good in programming, stick to them, they can save your butt most of the time. Don't forget to ask. It's not humiliation, it's curiosity. Mr. Einstein once said, "Don't loose the Holy Curiosity!".

Lastly, if you want shortcuts, questions about your programming problems, go to this blog spot, and I'll be open for questions.

That's all for now! Make this site alive!

-Author




Introduction from the Author

Welcome!

You have just come to the right place where all beginning programmers, wannabes, computer tech gurus, programmers, designers, teachers, hobbyists, and knowledgeable people meet finally on this portal. We all know that the Internet as of now has affected the era of informational books. If you want to know something, just type your thoughts on a search engine and it will give you vast amount of information, mostly updated. We, in the third-world country, are happy about this, since we cannot afford expensive books to support our learning process especially in the fields of computers. I myself, a programmer have bought several books before I finally considered myself professional, but those are all having the basics and fundamentals to guide me where should I go first. Usually, as you might agree with me, the ideas written inside those bulky stuffs are considered as for "textbook" only situations and commonly not for the real-world scenarios. What has saved me from the requirements of the fast-evolving computer era is the Internet itself. Currently, if you have a project, shall we say, a website which should be written in ASP.NET, and you are a PHP programmer, what would you do? No time to have a crash course right? The bosses and the managements don't like to wait for you. Time is gold as they say. So, what you should most probably do is search the Internet about ASP.NET and voila! You just have yourselves several keys unlocking all the secrets about the language and you simply translate your knowledge in PHP into the language of ASP.NET and that's how it works. This is the main purpose of the site, to contribute something to the tree of knowledge, the Internet, where information about computer languages, programming techniques, programming solutions will be shared freely to all aspiring to become better programmers.

Let me give you my background if you don't mind...

I, as the author of this site, have been doing a lot of programming tasks since I had my first 286 Mhz PC, running with 20MB Harddisk and 16MB RAM.. The first language I used to play with was QBasic, and then later, I had manage to have a copy of Pascal compiler and then I eventually converted all my programs from QBasic to Pascal. Pascal, is one of the easiest to learn and I'm a witness to that idea. This was also when I realized the difference between an interpreted and compiled programs (QBasic as an interpreted language and Pascal was implemented as compiled language). Compiled languages are faster compared to interpreted ones. Read it from textbooks. At that point of my life, I simply did a lot of programs. While reading my textbook and found a programming problem, I immediately compose my thoughts and express them to my computer. Once I solve the problem, I could feel the adrenaline rush and it makes my day. If I fail, I simply analyze the problem several times and try it again and again and again. That's my cycle. Then I read another book to learn new programming styles and concepts. Up to this days, I just can't stop doing this cycle, until my thirst is relieved, I won't stop. Of course I'm not saying that 100% of the problems I encountered, I succeeded. When I finally reach my limits, then it's the time I need to learn from other programmers who are better than me. Search the Internet and find some good tutorials, download the program's source codes, and then reverse-engineering them to understand what actually happens inside. No man is an island. It's true, you can't survive if you "cannot" learn from others. Don't humiliate yourselves, most programmers who consider themselves experts also came from these cycles. But take note, sometimes, you might not get what you want. You then need to listen to your instincts. Search some more, until you find it.

This is quite long already. Let me once again welcome you to another blog site of computer programming. Here you will find advices, solutions and ideas not only the common ones but complicated programming chores and problems. Join me in helping the computer enthusiasts unfold the mystery of computer programming.

-Author