Saturday, July 30, 2011

Various Sorting Techniques used to sort the Elements of an Array:Bubble Sort-Selection Sort -Insertion Sort-Quick Sort-Merge Sort-Heap Sort

Various Sorting techniques can be implemented to sort an Integer array .Some of them are
  • Bubble Sort
  • Selection Sort
  • Insertion Sort 
  • Quick Sort
  • Merge Sort
  • Heap Sort


Bubble Sort:

Bubble Sorting is the Most Basic Sorting technique.The various steps in technique

1. Compare the two  adjacent elements of the array
2. Check if one on the one on the left is large than one on the right, swap them.
3. Move to the right by one position.

The Code used for Implementing Bubble Sort is given Below
--------------------------------------------------------------

void sort() 
{
int x=l;
int y=0;
int a,b=0;
for (int rep=0;rep<l*4;rep++)
{
for(int i=0;i<x-1;i++)
{
a=ar[i];
b=ar[i+1];
if(a>b)
{

y=a;
a=b;
b=y;
ar[i]=a;
ar[i+1]=b;
}
}
}
}


----------------------------------------------------------------
Other Sorting Techniques will discussed in the future posts.

Read more »

Tuesday, July 26, 2011

Checking whether a String is Palindrome,Complete Source Code

Strings which are read the same, both forward and backward  are called Palindrome Strings eg:malayalam. Palindrome checking is a Common Program for Beginners.The program below can be used to check whether a String is a Palindrome.

 #include<stdio.h>  
 #include<conio.h>  
 #include<string.h>  
 void main()  
 {  
 int i,l,f=0;  
 char a[20];  
 printf("\nEnter the string:");  
 scanf("%s",a);  
 l=strlen(a);  
 for(i=0,l=l-1;a[i]!='\0';i++,l--)  
 {  
 if(a[i]!=a[l])  
 {  
 f=1;  
 break;  
 }  
 }  
 if(f==0)  
 printf("String is palindrome");  
 else  
 printf("String is not palindrome");  
 }  

Read more »

Saturday, July 23, 2011

Pelles C: A Good IDE for C Supporting Windows 7 32 bit and 64 bit versions


Pelles C is a good free Integrated development Environment for developing C programs.
It is available in both 32 and 64 bit versions with support for Windows 7 64 bit editions.

Pelles C is a lighweight bundle of Integrated features.It can be used it to edit source files, icons, cursors, bitmaps, resource scripts.

Pelles C has a Clean user Interface with Quick Compilation,Build and Run Options.It also features a Wizard Mode for Creating Windowed applications.Pelles C can also be used to create applications for Windows Mobile.

Overall Pelles C Serves as a Good Platform for students and professionals who need a good IDE for daily programming Needs.

You can Download Pelles C from http://www.smorgasbordet.com/pellesc/

Read more »

Thursday, July 21, 2011

File Operations in C:Source Code to Write Text to a File ,Copy it to a new Location

File Operations in C are accomplished by the use of pointers.Pointers are use to point to a particular memory location.File are read and written by using file pointers.The Keyword FILE is used to declare a file pointer.

eg:FILE *fp
      Here fp is a file pointer.* before a variable name indicates that the variable is a pointer.

The Program given below demonstrates how to write strings read from keyboard to a file and how to copy the file into a new location.It mainly uses two file pointers fi and fo.

fi- Input file pointer
fo- Output file pointer

fopen is the function used to open a file and assign the address of the first character to a pointer

eg: fo=fopen("c:\myfile.txt","wb");

Here 'myfile' is opened and pointer fo is used to point to that file.This file is opened in the 'write binary' mode.There are different modes of opening files in C. 'write binary' means binary data can be written to the file.The important modes of opening files in C are 

r-read Only Mode
w-Write Only Mode
a-Appending Mode for appending data to an existing file

There are many more modes of opening files, these will be discussed in detail in another post.
 #include<stdio.h>  
 void main()  
 {  
 FILE *fi,*fo;  
 char c;  
 char fin[125],fout[125],indata[500];  
 clrscr();  
 puts("             File Input&Copy             " );  
 puts("Input Data \n");  
 gets(indata);  
 puts("\nSave File as: ");  
 gets(fout);  
 fo=fopen(fout,"w");  
 fi=fopen(fout,"r");  
 fprintf(fo,indata);  
 puts("\nSave copy as: ");  
 gets(fout);  
 fo=fopen(fout,"w");  
 while(1)  
 {  
 c=getc(fi);  
 if (c==EOF)  
 break;  
 putc(c,fo);  
 }  
 puts("File copied successfully");  
 puts("\n");  
 printf("Reading from file:%s \n \n ",fout);  
 fprintf(fo,indata);  
 puts(indata);  
 getch();  
 }  

Read more »

Sunday, July 17, 2011

Dynamic Memory Allocation in C

Dynamic Memory allocation is the process of allocating memory to program in runtime.

The various dynmaic memory allocation functions available in C are
  • Malloc:Allocates specified number of bytes un memory and returns the pointer to the first byte allocated.

           pointer=(cast-type*)malloc(size in bytes)

  • Calloc:Creates more than one block of memory and returns the pointer to the first byte in the first block.
          pointer=(cast-type*) calloc(n,size in bytes)

          Here n refers to the number of blocks

  • realloc:Used to reallocate a previous allocated memory area.

           pointer=realloc(new pointer,newsize);

  • free:Used to clear previously allocated memory so that memory is used efficiently.

          free(pointer name);

Read more »

Protecting your C Program/exe Files from Decompilation and Reversing

Every Executable file has threat of decompilation and reverse engineering .This threat can be eliminated to extend by packing/compressing  the executable(.exe).
This post teaches you the basics of packing a program thus reducing its size and also increasing the level of security.
The Tool used is called UPX(Ultimate Packer for Executables).

You can download  the GUI of UPX from source forge follow http://sourceforge.net/projects/upxgui/

UPX can be used to protect Executables created using C.
  • Open UPX and select the File you want to Protect/Pack.
  •  Select the compression ratio from a range of 0-9.
  • If the Program is a Windowed application you can select the various resource compression options.
  • After Configuring the Options.Click on Start Compression,to compress the Executable.The new size and the old size of the executable after and before compression will be shown to you after the compression process is completed.

Read more »

Saturday, July 16, 2011

How to Run Dave in Windows 7 64 Bit Version Using DOSBOX


If you are a Windows 7 64 bit version user you wont be able to play or run Good Old Games Like Dave that you enjoyed playing when you where a Kid.This Tutorial will teach you how to run Dave ,one of best classic Dos game in Windows 7 64 Bit Version


    • A console window will open up. Type in the following commands by referring the details given below .Type a Line of Command ,Hit Enter and type next press Enter the Next and So On.      
             _______________________________ 

                  mount c d:\
               c:
               cd dave
               dave
            
             _________________________________________________
** Here "c" refers to a virtual drive you can specify any alphabet you like.
** "d:\ " Refers to the drive which contains the dave game folder e:d:\dave
** "cd dave " command is used to change the active directory to the folder named dave(folder that contains the game) which contains dave.exe and other files if you have changed the original name of this folder use that name instead of 'dave' in this command.
**'dave' refers to launching the Executable file(.exe) 'dave.exe'  
Step1: Type in mount c d:\


 
Step 2:Type in c: and rest of the commands specified


 
On typing dave you will be greeted with a window with dave running in it.
  •  All Done. There is a quick way to launch the game by avoiding the need to type in these commands every time you Launch the Game,I will share it if you respond to this Post by just Commenting on it.
     
  • For the Video Tutorial  please see http://www.youtube.com/watch?v=XfxJFwsdXE4

    Read more »

    Friday, July 15, 2011

    Source Code for Performing Matrix Multipliaction in C

    The program below demostrates how to multiply any two matrices in c.Before multiplication the order of the matrix is checked to ensure multiplication is possible.

     #include<stdio.h>  
     #include<conio.h>  
     void main()  
     {  
      int A[50][50],B[50][50],C[50][50];  
      int i,j,p,r1,r2,c1,c2;  
      clrscr();  
      printf("\nMATRIX MULTIPLICATION");  
      printf("\nEnter The order Of First Matrix:");  
      scanf("%d%d",&r1,&c1);  
      printf("\nEnter The Elements Of First Matrix:");  
      for(i=0;i<r1;i++)  
       for(j=0;j<c1;j++)  
         scanf("%d",&A[i][j]);  
      printf("\n\nEnter The order Of Second Matrix:");  
      scanf("%d%d",&r2,&c2);  
      printf("\nEnter The Elements Of Second Matrix:");  
      for(i=0;i<r2;i++)  
       for(j=0;j<c2;j++)  
         scanf("%d",&B[i][j]);  
      if(c1!=r2)  
      {  
       printf("\n\nThe Matrix Cant Be Multipied!!!");  
       getch();  
       exit(0);  
      }  
      for(i=0;i<r1;i++)  
       for(j=0;j<c2;j++)  
       {  
        C[i][j]=0;  
        for(p=0;p<c1;p++)  
         C[i][j]+=A[i][p]*B[p][j];  
       }  
       printf("\n\nThe Product Of Matrices Is:");  
      for(i=0;i<r1;i++)  
      { printf("\n");  
        for(j=0;j<c2;j++)  
         printf("%d ",C[i][j]);  
      }  
      getch();  
     }   
    
    }

    Read more »

    Thursday, July 14, 2011

    C Program to Add Two Matrices using 2D Array

    As a C Programmer ,you need to deal with data structures likes an array.Programmers often needs to use a Multidimensional array in the form of matrix to do mathematical Calculations.The Program below Demonstrates the addition of any  two user specified matrices together and obtain the result in a new Matrix.
    The Complete Source Code to implement matrix addition  by using 2D Integer Array is given Below.

     #include<stdio.h>  
     #include<conio.h>  
     void main()  
     {  
      int A[50][50],B[50][50],C[50][50];  
      int i,j,r1,r2,c1,c2;  
      clrscr();  
      printf("   * Matrix Addition*     ");  
      printf("\nEnter The Size Of First Matrix:");  
      scanf("%d%d",&r1,&c1);  
      printf("\nEnter The Elements Of First Matrix:");  
      for(i=0;i<r1;i++)  
      for(j=0;j<c1;j++)  
      scanf("%d",&A[i][j]);  
      printf("\n\nEnter The Size Of Second Matrix:");  
      scanf("%d%d",&r2,&c2);  
      printf("\nEnter The Elements Of Second Matrix:");  
      for(i=0;i<r2;i++)  
       for(j=0;j<c2;j++)  
         scanf("%d",&B[i][j]);  
      if(r1!=r2||c1!=c2)  
      {  
       printf("\n\nThe Matrix Cant Be Added!!!");  
       getch();  
       exit(0);  
      }  
      for(i=0;i<r1;i++)  
       for(j=0;j<c1;j++)  
         C[i][j]=A[i][j]+B[i][j];  
      printf("\n\nThe Sum Of Matrices Is:");  
      for(i=0;i<r1;i++)  
      { printf("\n");  
        for(j=0;j<c1;j++)  
         printf("%d ",C[i][j]);  
      }  
      getch();  
     }  
    

    Read more »

    Wednesday, July 13, 2011

    The Art of Encryption Using C

    Encryption is the process of transforming information  using an algorithm  to make it unreadable to anyone except those who has a key.

    A common way to Encrypt files in C is by using XOR Encryption

    XOR is Logical Operation

    1 xor 0 = 1
    0 xor 1 = 1
    1 xor 1 = 0
    0 xor 0 = 0

    XOR encryption encodes each bit with a a key provided and the generated output is unreadable without a Key.

    Read more »

    Subscribe

    The Source Codes Published in this Blog can be used freely for Educational purposes but should not be reproduced on any other Blog or Website without the consent of the author.