Thursday, April 22, 2010

Data Structures and Algorithms - Episode 1: Sorting Algorithms (Bubble Sort)

When starting to learn to program, whatever the language may be, the first and foremost concern should be thorough understanding of the concept of an 'Algorithm'. We know that computer is a machine which operates on a sequence of instructions fed to it by the human in order to function and/or solve a particular problem. This sequence of instructions formally is known as an algorithm. An algorithm in plain English say is understandable to a human while when written in a programming language it is understandable by the computer. Sorting is one of the basic problems when we are dealing with a sequence of items (say numbers). In this article, the known Sorting methods would be discussed and demonstrated starting with "Bubble Sort".

Win32 - Episode 1: Basic Application Structure

In this tutorial/article you will learn how to go about developing a basic user interface in C++ using win32 API. The first thing while developing a Graphical User Interface (GUI) based application is to understand the architecture of a WIN32 application together with the interface elements and the program logic behind. Considering this tutorial suitable for a newbie to win32 I would try to explain every terminology that we might encounter while developing our application. Here's the list of topics we are going to discuss in this episode.

Windows Programming
Threads
Callback Functions
Messages and Commands
Message Loop
Example Application

(This post is in it's writing phase, please, check back later to find the full text)

Wednesday, April 21, 2010

C vs. C++

Because of both it's low-level and high-level features, C++ is still one the most popular and widely-used language today. The high-level features were the result of enhancements to the earlier C thus retaining the low-level features. So if C++ is just an enhanced version of C, where do the differences lie? To answer this question, C++ was enhanced to exclude some of the C principles and to include some the features unknown to a C compiler.

Syntax Change

The first and very basic difference between the two languages is the slight change in syntax of some of the libraries.

Library & Linking

C++ has a larger library than C. For example, to compile a math-extensive program in gcc you have to separately link the math library for basic math functions in C. On the other hand you don't have to do explicit linking in C++.

Memory Allocation/DeAllocation

In C memory allocation for both arrays and elements is done through malloc. Also, inorder to deallocate the memory, it has to be done using free. In C++ memory allocation is done using new operator. Similarly, delete is used to free the memory.

Function Declaration

You need not explicitly declare a function in C. On the contrary, a C++ function need to be declared before use.

Structs

In C 'struct' keyword has to be explicitly included when declaring an instance. This has not to be done in C++.

Boolean Type

There is no primitive boolean data type in C. On the other hand, C++ has a primitive "bool" data type.

Library

The first and very basic difference between the two languages is the slight change in syntax of some of the libraries.

Summary

C++ could be seen as a better and more capable C in view of it's capability of having low-level and high-level features. 


Simple Calculator in C++ Tutorial

Writing a Basic calculator in C++ or any language for that matter puts into practice the very basics of a programming language. I wrote this program the first time when I was in grade 9. As a novice programmer, to tackle each coding problem, it's wise to first lay out the steps that are required to solve the problem (algorithm) till you are confident and comfortable enough with the environment and the language.

Gather Requirements

A simple calculator provides the following basic functionality:
  1. Addition of two numbers, 
  2. Subtraction of two numbers, 
  3. Multiplication of two numbers and 
  4. Division of two numbers.
In addition to the basic function, it also provides a way to:
  1. Reset
  2. Exit 
  3. Check for Division By Zero
Implementation Decisions 
  1. Switch statement for Choice between the Functions
  2. Basic I/O (Input stream for user input, Output stream for result to display)
  3. Expressions (Arithmetic Operators, Assignment Operator )
  4. Choice of Correct Data Types
Program Snippet

#include
#include

using namespace std;

double Add(double n1, double n2){
     
      double r = n1 + n2;
      return r;
}

double Sub(double n1, double n2){
      double r = n1 - n2;
      return r;
}

double Mul(double n1, double n2){
      double r = n1 * n2;
      return r;
}

double Div(double n1, double n2){
      double r;

      if(n2==0)
            r = 0;     
      else
            r = n1/n2;
     
      return r;
}

int main(){
     
      double n1, n2;
      char choice;
      bool flag = false;
      double r;

      do{

      cout<<"\n\n\t\tBASIC CALCULATOR\n";

      cout<<"\t\t----------------\n";

      cout<<"\t\t(+)Add\n";
      cout<<"\t\t(-)Subtract\n";
      cout<<"\t\t(*)Multiply\n";
      cout<<"\t\t(/)Divide\n";
      cout<<"\t\t(#)Reset\n";
      cout<<"\t\t(^)Exit\n";

      cout<<"\t\t-----------------\n";

      cout<<"\n\nEnter First Number: ";
      cin>>n1;

      cout<<"\n\nEnter Second Number: ";
      cin>>n2;

      cout<<"\n\nOperation?";
      cin>>choice;

      switch(choice){
           
            case '+':
                  r = Add(n1,n2);
                  cout<<"\n\nReuslt of Addition:"<
            break;
           

            case '-':
                  r = Sub(n1,n2);
                  cout<<"\n\nReuslt of Subtraction:"<
            break;

            case '*':
                  r = Mul(n1,n2);
                  cout<<"\n\nReuslt of Multiplication:"<
            break;

            case '/':
           
                  r = Div(n1,n2);
                  if(r==0)
                        cout<<"\n\nDivision by Zero not Possible\n";
                  else
                        cout<<"\n\nReuslt of Division:"<
            break;

            case '#':
                  flag = false;
            break;

            case '^':
                  flag=true;
            break;

            default:
                  cout<<"\n\nFunction Does not Exist\n";
            break;
      }
      }
      while(flag!=true);

return 0;
system("pause");

}

Program Explanation

The program starts at the "main()" method. First, the input numbers to the calculator programs have been declared. Since, the user might enter a number of any possible range, a double datatype would be preferable over a float. For a detailed review of data types and choice of one data type over another, please Click Here! 

For better organization and future modifications to the functionality of the calculator, separate methods have been created for all operations. The expressions in each method consist of arithmetic ( +, - , *, / ) and assignment (=) operator. 

The main program functionality sits inside a do-while loop which exists upon selection of the "Exit" menu from the Calculator menu. A flag parameter checks for when to exit and till when to stay in the loop.

Summary

A Simple/Basic calculator implementation involves methods for the basic arithmetic operations and means to choose the operation to be applied on the two numbers input by the user. A do-while loop keeps going back to the calculator menu until "Exit". A switch statement serves to implement the operation selection.







 

How to C++

Today there's 1000 many resources on C++ on the internet. 'How to C++' being one of them, provides you with easy to understand articles, tutorials and links on C++ fundamentals, STL, Win32 and MFC. Also find useful readings and tutorials on Data Structures, Algorithms and OpenGL in C++.

About Me

I am a Research Assistant at Concordia University, Montreal, Canada. I Teach C++ and OpenGL. Also I work as a freelance UI Researcher/Designer.
- Noorulain Khurshid

Followers

How to C++ Copyright © 2009 Blogger Template Designed by Bie Blogger Template
EasyHits4U.com - Your Free Traffic Exchange - 1:1 Exchange Ratio, 5-Tier Referral Program. FREE Advertising!