Thursday, August 26, 2010

LinkLists in C++: Part 4

Fourth in the series of Link-List funtions.

/***************************************************/
#include< iostream.h >
#include< conio.h >
#include< stdlib.h >

struct node
{
int info;
node* next;
}*start, *end, *t;

void reverse()
{
int n;
t = start;
while(t != NULL)
{
n++;
t = t->next;
}
for(int i = 0;i <= n-3;i++)
{
t = start;
while(t->next->next->next != NULL)
t = t->next;
t->next->next->next = t->next;
t->next->next = NULL;
}
t = start;
t->next->next = start;
t->next = NULL;
t1 = start;
start = end;
end = t1;
}

void sort()
{
t = start;
while(t != NULL)
{
t1 = start;
while(t1 != NULL)
{
if(t1->next->info > t1->next->next->info)
{
t1->next = t1->next->next;
t1->next->next = t1->next->next->next;
t1->next->next->next = t1->next;
}
t1 = t1->next;
}
t = t->next
}
}

LinkLists in C++: Part 3

Third in the series of Link-List funtions.

/***************************************************/
#include< iostream.h >
#include< conio.h >
#include< stdlib.h >

struct node
{
int info;
node* next;
}*start, *end, *t;


void listnode()
{
t = start;
cout <<"\nTHE CONTENTS ARE\n";
while(t != NULL)
{
cout <info <
t = t->next;
}
}

void count()
{
t = start;
int ctr = 0;
while(t != NULL)
{
ctr++;
t = t->next;
}
cout <<"\nNUMBER OF NODES ARE" <
}

void search()
{
t = start;
int n, flag = 0;
cout <<"\nENTER INFO TO BE SEARCHED ::";
cin >>n;
while(t!= NULL)
{
if(t->info == n)
{
flag = 1;
break;
}
}
if(flag == 0)
cout <<"\nNOT FOUND";
else
cout <<"\nFOUND";
}

LinkLists in C++: Part 2

Second in the series of Link-List funtions.

/***************************************************/
#include< iostream.h >
#include< conio.h >
#include< stdlib.h >

struct node
{
int info;
node* next;
}*start, *end, *t;



void delnode()
{
int ch;
cout <<"\n1.DELETE IN THE BEGINNING.";
cout <<"\n2.DELETE AT THE END.";
cout <<"\n3.DELETE IN THE MIDDLE.";
cout <<"\nENTER CHOICE";
cin >> ch;
switch(ch)
{
case 1:delbeg();
break;
case 2:delend();
break;
case 3:delmid();
break;
default:cout <<"\nWRONG CHOICE";
}
}

void delbeg()
{
t = start->next;
start->next = NULL;
delete start;
start = t;
}

void delend()
{
t = start;
while(t->next->next != NULL)
t=t->next;
t->next = NULL;
delete end;
end = t;
}

void delmid()
{
int n;
cout <<"\nENTER THE INFO TO BE DELETED";
cin >>n;
t = start;
while(t != NULL)
{
if(t->next->info == n)
{
t1 = t->next;
t->next = t1->next;
t1->next = NULL;
delete t1;
break;
}
t = t->next;
}
}

LinkLists in C++: Part 1



First in the series of Link-List funtions.

/***************************************************/
#include< iostream.h >
#include< conio.h >
#include< stdlib.h >

struct node
{
int info;
node* next;
}*start, *end, *t;

void addnode()
{
int ch;
if(start == NULL)
{
start = new node;
if(start == NULL)
{
cout <<"\nNOT ENOUGH MEMORY";
getch();
exit(0);
}
cout <<"\nENTER INFO";
cin >>start->info;
start->next = NULL;
end = start;
}
else
{
cout <<"\n1.ADD IN THE BEGINNING.";
cout <<"\n2.ADD AT THE END.";
cout <<"\n3.ADD IN THE MIDDLE.";
cout <<"\n4.EXIT.";
cout <<"\n\nENTER CHOICE ::";
cin >> ch;
switch(ch)
{
case 1:addbeg();
break;
case 2:addend();
break;
case 3:addmid();
break;
case 4:exit(0);
break;
default:cout <<"WRONG CHOICE.";
}
}
}

void addbeg()
{
t = new node;
if(t == NULL)
{
cout <<"\nNOT ENOUGH MEMORY";
getch();
exit(0);
}
cout <<"\nENTER INFO :: ";
cin >>t->info;
t->next = start;
start = t;
}

void addend()
{
t = new node;
if(t == NULL)
{
cout <<"\nNOT ENOUGH MEMORY";
getch();
exit(0);
}
cout <<"\nENTER INFO :: ";
cin >>t->info;
t->next = NULL;
end->next = t;
end = t;

}

void addmid()
{
int ch;
cout <<"\n1.ADD BEFORE A GIVEN NODE.";
cout <<"\n2.ADD AFTER A GIVEN NODE.";
cout <<"\nENTER CHOICE :: ";
cin >> ch;
switch(ch)
{
case 1:addbefore();
break;
case 2:addafter();
break;
default:cout <<"\nWRONG CHOICE";
}
}

void addbefore()
{
int n;
cout <<"\nENTER THE NODE INFO.";
cin >>n;
t = start;
while(t != NULL)
{
if(t->next->info == n)
{
t1 = new node;
if(t1 == NULL)
{
cout<<"\nNOT ENOUGH MEMORY";
getch();
exit(0);
}
cout <<"\nENTER INFO";
cin >>t1->info;
t1->next = t->next;
t->next = t1;
}
t = t->next;
}
}

void addafter()
{
int n;
cout <<"\nENTER THE NODE INFO.";
cin >>n;
t = start;
while(t != NULL)
{
if(t->info == n)
{
t1 = new node;
if(t1 == NULL)
{
cout<<"\nNOT ENOUGH MEMORY";
getch();
exit(0);
}
cout <<"\nENTER INFO";
cin >>t1->info;
t1->next = t->next;
t->next = t1;
}
t = t->next;
}
}

Sunday, August 22, 2010

A Nifty Trick...

A trick is to make any PC shut down in a specified time with a horrifying message. No harm will be done of course. We will just make a small executable of ".exe" format for our mischief.
The executable will work its magic only when clicked, so expect no "virus-action", but it can be easily disguised as some other program. 
Here are the steps:
  1. On your Desktop, Right click->New->Shortcut
  2. Dialog Box appears, type in: shutdown -s -t 20 -c "YOUR MESSAGE"
  3. Now click on Next.
  4. Shortcut Name is asked, call it "shutdown.exe"
  5. Click on finish.
    shutdown.exe will be created on the desktop.
Note that the file I created has a string argument "YOUR MESSAGE", which can be replaced by anything. example: "FATAL ERROR: SYSTEM CRASH REPORTED". Also, the final output will look somewhat like this on your desktop, Ive shown the portion the the Taskbar that displays the message. The value 20 is the time delay in seconds.

The coolest part is that Alt+F4 or Task manager cannot terminate this file!!!
To terminate: Start->Run->"shutdown -a" and press enter. Moreover this file is easily deletable.

Now for a finishing touch:

  1. Rename the executable to a commonly used program, example: My computer
  2. Right Click->Properties->Shortcut Tab->Change Icon
  3. Now the Icon image must be chosen according to the name given, here it is My computer

Apply any standard icon image and name to your file, send it to friends or have them click it on your PC and watch their faces.

Friday, August 13, 2010

Microsoft Robotics Developer Studio

Robotics Developer Studio, or RDS, is an environment made by Microsoft to develop and test projects on robotics. This is a major step by Microsoft indeed, to promote hobbyists as well as professional developers. Using this Windows-based suite, not only can we write code for a robot, but simulate a virtual prototype in a real world scenario using the state-of-the-art simulation engine. Whats looks so unique about this software is the fact that the RDS has not been built with the expert in mind, rather a beginner. Technically speaking, the learning curve is smooth and provides ample opportunities for experimentation.

Programmers can choose between the the familiar Visual studio and Visual Programming Language or VPL. VPL can be called a beginner's programming platform with a simple drag 'n' drop interface through which we can generate C# code. So you can understand how easy it is to code for an application using RDS.

The most important feature in this studio is the simulation. Now all of us must have seen the port-based emulation feature of IDE's like KEIL, etc. RDS goes way beyond testing with its 3-D simulation software. Not that there is a parallel here, but it definitely shows how far Microsoft has taken the concept of simulation. RDS allows us to directly test the behavior of our robot in a physics-based virtual world. Not only our software, but also the feasibility of the concept can be checked by testing it against a variety of environments.

Microsoft has provided several tutorials for getting a good start on their product. Besides this, external help has also been provided by software developers all over the world and their programs are just a few clicks away.

A let-down for the average Indian student is, however the requirements. The RDS does require a decent configuration of a PC in order to work its magic. Well this is mainly due to the Graphics requirements and overall PC specs that people normally don't think about when buying PC's. This problem can be solved easily with some minor and cheap upgrades.
The software in itself is completely free, and easily available from the Microsoft website. However you may be required to download and install additional software or drivers if you do not already have them, which is highly unlikely if you are a developer.

Overall, this amazing software by Microsoft is bound to raise eyebrows and most importantly, raise standards. It will get a thumb's up from all types of users, whether commercial or not, simply because of its quality and easy-to-use features.

Wednesday, August 11, 2010

What your C\C++ teachers didnt teach you...or forgot!!!

Well their is no end to C\C++ and their never will be, simply because C++ can evolve to suit whats being demanded... Even this infinite syllabus has certain features that all of you should know. You might know most of this stuff but mind you many do not have that privilege.

Level - C++ is NOT a "High Level Language", or atleast not a conventional HLL because of its heavy use of low level features...

Header Files - YES!! you can make your own, it should only have class and function declarations and definitions in a related "library" file...but you can avoid that if you want and have entire classes as headers. They can be included just as normal header files are included in programs.

Graphics - C++ has a very primitive DOS based working environment but does support very simple graphics. More advanced graphics can also be supported by designing the right API for your own graphics driver. You can use graphics to make anything from games to animations...although that might get really frustrating and tiring!!!

Friend Class - A class can declare other classes/functions as friends to allow access to non-public class members unaccessible to outside code. Of course this was taught, but what was not told is that 'friend' is a USELESS concept. It can be used to split data between multiple classes, something that can be avoided.

IDEs - NO, the world doesnt end after Turbo C, in fact TC is very much dead nowadays. GUI based Integrated Development Environments are openly available for download that do not need (crappy) DOS to run and are much useful when it comes to more modern Operating Systems like Windows 7. But yes there is a catch, you need to download and install compilers all by yourself.

Templates - An advanced concept, templates are used on both  functions and classes. All they do is to extend the functionality of a class that has not been defined yet. Now this template function/class can use the undefined class as a regular datatype!! Templates look somewhat like this...
template ...other datatype> 
function/class declaration;
There can be multiple templates for the same class.

Keyword "volatile" - A type qualifier that indicates that the value of the variable will change value depending on external conditions, like hardware, operating system, network, etc. Then the compiler will always work on the current value every time a volatile variable is used.

Keyword "mutable" - A 'mutable' data member can be assigned values by a 'const' method.

New style casts - Forget c-style casts that you were taught and move on to the following for lesser bugs...
  • static_cast, 
  • dynamic_cast, 
  • reinterpret_cast and 
  • const_cast)

Threading - Several libraries that allow threading are now open source....


Exception Handling - Same as Java....

Garbage Collection - As I mentioned in my previous post, Garbage collection is simply automatic memory management incorporated into runtime. C++ was much criticized for its lack of a Garbage Collector. But automatic memory management is possible through external libraries and Smart Pointers. Also, a Garbage colletor by Boehm-Demers-Weiser can be used. It replaces malloc in C and new in C++

Interrupts - Interrupts, like the name suggests, disrupt the linear flow of control to execute certain subroutines. After that the program execution resumes from where it was suspended. Interrupts are messages to the CPU to suspend the program, execute a small executable and resume its activity. These executables are sets of instructions that are already stored in the CPU. Interrupts can be used to anything a library function can do and much beyond. Consult your IVT(Interrupt Vector Table) for details.

Register Variables - Variables can be stored directly onto CPU's registers, (which is storage available on the CPU). This feature reduced cost of accessing RAM. In embedded systems, storage is done directly onto the processor's registers as primary storage.

Hardware Application - Embedded C and C++ are dialects for embedded systems. They work directly on a processor's registers and interrupts using the familiar syntax. The focus here is on using low-level features to work closely with memory. A common example is the language used with Keil.

Updates - C++ has constantly evolved over the years thanks to a C++ Standard that is maintained as well as updated. The first was ANSI-C++ and the current is C90, also the next modern dialect of C++ will be called C++0x