Monday, July 26, 2010

Extracting Digits from Float numbers

A friend just came up with an unusual request for me, to "intelligently" extract each digit from a float number.
He wanted each digit from the number sent by the Micro controller to be extracted separately to display on his Display Unit... While type-casting will lead to total loss of precision, and mathematical operations are useless if you think of any.

So I decided to convert this number entirely to a string, complete with the "dot" decimal and then refer to them as an array of characters. Please note that there is a loss involved due to size of the array but that is minimal.
The limit of numbers that will be displayed is 6, excluding the decimal number.

I, luckily found a library function to do the task....its called sprintf and declared inside stdio.h. Code looks like this.

/***************************************************************/
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#define nmbr 48.88         //Float number to be processed
void main()
{
clrscr();
char num[6];               //Recipient array
sprintf(num,"%f",nmbr);    /*Function requires Recipient        
                                   array, Datatype float, and our  
                                   float number 48.8*/
cout<<"\nDECIMAL PART:\n";

for(int i=0;i<6;i++)       //Simple array traversing
{
if(num[i]=='.')
{
cout<<"\nFRACTIONAL PART:\n";
continue;
}
cout<< num[i];                  /* Instead of displaying, the    
                                  character can be processed to   
                                  obtain any desired result*/
}
getch();
}
/***************************************************************/
OUTPUT

Monday, July 19, 2010

Flood-fill Algorithm - Working Demo

This is a working code to demonstrate the Flood-fill or Brushfire or Wavefront Algorithm in C++. The main crux of this code however, is the Flood method.

Flood-Fill algorithm calculates relative cost of moving to a Block of an array and fills all consecutive blocks with numbers corresponding to cost. The Source is tagged as 0 as there is no additional cost to move there.
Here is the code:


#include <iostream.h>
#include <conio.h>


#define GridMaxX 5
#define GridMaxY 5


int Grid[GridMaxX][GridMaxY],r,c;
// r and c are coordinates of filling source.


void DisplayGrid()//Display the grid
{
for(int i=0;i<=GridMaxX-1;i++)
{
for(int j=0;j<=GridMaxY-1;j++)
cout<<"\t";
cout<<"\n\n";
}
}


void Flood()//Flood the grid
{
for(int i=0;i<=GridMaxX-1;i++)
for(int j=0;j<=GridMaxY-1;j++)
Grid[i][j] = (((i-r)>=0)?(i-r):(r-i))+(((j-c)>=0)?(j-c):(c-j));

//x-distance(positive) added to y-distance(positive)
DisplayGrid();
}


void main()
{
clrscr();

//Initialize Grid with junk values of 99.
for(int i=0;i<=GridMaxX-1;i++)
for(int j=0;j<=GridMaxY-1;j++)
Grid[i][j]=99;
DisplayGrid();

//Input the source of Flooding
cout<<"ENTER SOURCE COORDINATES:\nx = ";
cin>>r;
cout<<"\ny = ";
cin>>c;
Flood();
getch();
}

Saturday, July 17, 2010

Microsoft Programming Languages


When programming was still new to me, I had no knowledge of any sort of languages by Microsoft, but as time progressed and .NET came into the picture, it became evident that you simply cant do without Microsoft's collection.
All programmers will have to one day work directly with the industry and requirements at that scale require .NET support, which means you need Microsoft products if you need quality...which is obviously non negotiable.

In case you are a programmer and want to code an application of any type then Microsoft has got just the right stuff for you. This article covers those commonly used languages that I have come across personally..

1) Languages you may already know or may have heard of are:

- Visual Basic - Event Driven Language for Rapid Application Developement(RAD) for GUI based needs.
- Visual C# - Microsoft's implementation of the C# specification. It has features very similar to Java like Garbage Collection, etc.
- Visual C++ - Microsoft's IDE(Integrated Developement Environment) for C\C++ code


2) .NET Platform programming languages

- Visual Basic .NET
- Visual C# .NET
- Visual C++ .NET

The above languages are evolved versions of Visual Basic, Visual C#, Visual C++ respectively. These languages have been developed to build .NET Applications and hence include much more features, libraries.
- Managed Extensions for C++ - Extensions for C++ that allow programmers use their existing applications in C++ with the .NET framework.


3) Transact-SQL - It works upon Microsoft's Relational Database Systems.


4) Scripting Languages

- Windows Script Host - Scripting environment that works with all scripting languages.
- VBScript
- JScript
- JScript .NET


5) Extensible Markup Language (XML) - Most useful when it comes to marking meta-data. XML and HTML cannot replace each other, in fact they compliment each other. XML is used to move metadata between systems.


6) Visual J++ - Allows migration of Java programmers to .NET, you can do so by either extending your Java code to .NET or you can use automated tools to convert to C# code.


7) X++ - OOP's based language similar to C# and has applications in building accounting and business management software. It hence used SQL along with the familiar "C++ style" of programming.


8) Alternative Languages
Many comapnies have collaborated with Microsoft to harness the growing power and influence of the .NET Framework. Besides these we also have alternate languages like:

- Standard ML
- SmallTalk
- Salford FTN95 (Fortran)
- Python
- Perl
- Pascal
- Oberon
- Mondrian
- Mercury
- Eiffel
- Dyalog APL
- COBOL

Tuesday, July 13, 2010

Garbage Collection


Garbage Collection is the concept of collecting useless "dereferenced" memory, and freeing it for re-use by the system. De-referenced resources are those objects that are no longer in use by the program but are still allocated for use by it.

Languages like C/C++ could allow programmers to directly interact and play with memory, a responsibility that is often so abused that it does more harm than good... Problems arise when people recklessly allocate large system resources and the due to some mis-management the allocated memory is never freed. This leaves large chunks of unreacheable memory locations that ultimately cause "Memory Leaks". In comes the invention by John McCarthy, which shoulders the responsibility of memory management by de-allocating memory that is not in use by the program anymore. While the concept was initially developed for LISP only, now it has spread to a variety of High Level Languages, including updated versions of C\C++ themselves.
C++/CLI (Common Language Infrastructure), which is Microsoft's language specification has provision for both manual and automated memory management.

Garbage collector is the term used to represent automatic memory management by the system. Garbage collector scans the runtime environment for objects that are accessible directly or indirectly via references. Then it proceeds to discard all remaining objects. Typically, an object's memory is reclaimed when the number of references to it reaches zero. These scans are done in cycles, which are started automatically by the Garbage collector or when explicit calls are made to it.

Garbage collection does not guarantee immunity from memory leaks, and obviously requires a considerable percentage of system resources to run, but definitely helps programmers who have to deal with a lot of memory in their projects. Garbage collection is not commonly used in embedded projects due to their already small resource size but are available on certain platforms like .NET Micro Framework and Java-ME.

Monday, July 12, 2010

JAVA, A Revolution

Java as we know...is the current "Queen Bee" of languages, emerging not only as an OOPs based language but also a dynamically improving technology.
Java is not just one single language, but also an approach that lets you code for Servers(Java-EE), Portable/Embedded Devices(Java-ME), Standard software applications(Java-SE) and much more...

Java also introduces a Garbage collector that consumes dereferenced memory and provides an optimised environment where the computer, not the programmer manages the memory. More helpful is the JVM-Java Vitual Machine that is an abstract, virtual computer that run the compiled programs as "bytecodes", just as our computer accepts "Assembly language".

Major features:
- Portable - Runs on a variety of CPU's. Java can run on any system with a compatible JVM.
- Robust - Can be made so to withstand and survive crashes and exceptions.
- Secure - No pointers prevent malicious activity.
- Object Oriented - The whole program is encapsulated in a class.

Currently Java is seen as two distinct modules:

1) CORE-JAVA
- Fundamentals of OOPs
- Implementation of the language
- Basic Console Applications
- Basic GUI
- Applets

2) ADVANCED-JAVA
- Advanced features of java built over the core
- Solutions to dynamic problems
- Covers web, networking, DCOM or database handling

The reason why Java is gaining popularity is its ability to relate to reality...as with all Object Oriented Languages. Today Java is seen as the answer to many complex problems that exist in the real world.