Pages

Thursday, July 14, 2011

Sorting and Searching Program with C and C++

Sorting and Searching is an important task in computer programming. So, this article will show how to do a sorting or searching program with C and C++ language. The following program will first store some strings into an array and then this array will be sorted with sorting algorithm. This program also stores user searching input and then searches the string in array.  If the search keyword is matched, the program will show how many times the search keyword is matched. Otherwise, search keyword not found message will be shown. 

Sorting and Searching program with C and C++  

#include<stdio.h>  
#include<conio.h>
#include<string.h>

  struct name            // Custom data type and variable
  {
      char a[10];   
       
   }b [10];        
       
void main()

  int N,i,j;
  int c=0;
  char name[10];
  clrscr();
  printf("How many names :");
  scanf("%d",&N); 
  fflush(stdin);
  for(i=0;i<N;i++)
  {
   printf("Enter %d Name :",i+1);
   gets(b[i].a);               
  }

  printf("\n\nEntered name list :");
  for(i=0;i<N;i++)
  {
            printf("\n%s",b[i].a);
  }


  for(i=0;i<N;i++)
  {
            for(j=i+1;j<N;j++)
            {
             if((strcmp(b[i].a,b[j].a))>0) 
               char temp[10];
               strcpy( temp,b[i].a);
               strcpy(b[i].a,b[j].a);
               strcpy( b[j].a,temp);
             }
   }
  }
  printf("\n\nSorted name list :");
   for(i=0;i<N;i++)
   {
             printf("\n%s",b[i].a);
   }

 printf("\n\nEnter name to search :");
 gets(name);
 for(i=0;i<N;i++)
 {
   if((strcmp(b[i].a,name))==0)
   c++;
 }
 if(c==0)
 {
   printf("%s not found .",name);
 }
 else
 {
    printf("%s remains %d times .",name,c);
 }
 getch();
 }

Program Output



Making Pyramid with C and C++ Program

The following program will show how to make a pyramid using C and C++. The  number of rows of the pyramid will be taken from the user and then it will be stored into a variable. Also two while loops are being used to build the pyramid.  

C and C++ pyramid program

#include<stdio.h>
#include<conio.h>

void main()
{
   int i = 0, j , n ;
   clrscr();
   printf("How many rows in pyramid:");
   scanf("%d",&n);

 while(i<n)
   {
             j=0;
             while(j<=i)
             {
               gotoxy(30+j,10+i);
               printf("%d",j);
               j++;
             }
             j=0;
             while(j<=i)
             {
                gotoxy(30-j,10+i); 
                printf("%d",j);
               j++;
             }
             i++;
             printf("\n");
   }

   getch();
}

Output



Remove Directory/Folder with C and C++ Program



The following program will show how to remove a directory/folder with C and C++. This program will store directory path and name from the user and then directory will be deleted with the rmdir() function. The rmdir() function will return 0 if directory is removed successfully otherwise it will return 1.



Directory removal program with C and C++

#include<stdio.h>

#include<conio.h>
#include<process.h>
#include<dir.h>

void main()


   int cheack;
   char *dirname;
   clrscr(); 
   printf("Enter a directory path and name to be deleted (C:/name):");
   gets(dirname);
   system("dir/p");
 
   cheack = rmdir(dirname);
   if (!cheack)
      printf("Directory deleted\n");
   else
   {   
            printf("Unable to remove directory\n");
            getch();
            exit(1);
   }
  getch();
}

Program Output

Enter a directory path and name to be deleted (C:/name):C:/test

Directory deleted.


Create Directory/Folder with C/C++ Program

The following program will show how to create a directory/folder with C and C++. This program will create a folder in a specific directory path with the mkdir() function. The mkdir() function accepts an argument which is the name and path of the directory.




C and C++  program to create a directory/folder

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<dir.h>


void main() {
   int check;
  char *dirname;
  clrscr();
  printf("Enter a directory path and name to be created (C:/name):");
  gets(dirname);
  check = mkdir(dirname);


  if (!check)
  printf("Directory created\n");


 else
 {
   printf("Unable to create directory\n");
   exit(1);  }

  getch();
  system("dir/p");
  getch();
}


Program Output

Enter a directory path and name to be created (C:/name): C:/test.
Directory created.


Loop Statement in C/C++ Programming

Loop statement is used to iterate a block of statement. Generally a loop statement has three parts. 

         1. Loop initialization. 
         2. Loop condition. 
         3. Loop increment or decrement.  

Loop statement first stores initial value in loop control variable and then check condition with loop control variable for taking decision and then increases or decreases the initial value. Mainly three types of loop statements are used in C and C++ programming. 

         1. for loop statement
         2. while loop statement
         3. do while loop statement


for Loop Statement

for loop statement is the mostly used loop statement in C and C++ programming.  The basic structure of a for loop statement is shown below.

for (initialization ;  condition ; increment/decrement )
{
    loop body;
}

Say, you want to print numbers from 1 to 100. For this, you can use a for loop statement. An example is given below. 

#include<stdio.h>
#include<conio.h>

void  main ()
{
   clrscr ();
   for(int counter =1; counter<=100; counter++)
    {
      printf (“%d\t”, counter);
    }
getch();
}

Program Output



while Loop Statement

while loop statement works same as for loop statement. The structure of a while loop statement is given below.

initialization;
while (condition)
{
   loop body;
   increment/decrement;
}

Say, you want to print those numbers which are divisible by 5 from 1 to 100. For this, you can use a while loop statement. An example is given below.

#include<stdio.h>
#include<conio.h>

void  main ()
{
   clrscr ();
   int counter = 5;
   while (counter<=100)
    {
      printf (“%d\t”,counter);
    counter = counter+5;
   }
getch();
}

Program Output




do while Loop Statement 


do while loop statement works same as while loop statement but only difference is that do while loop statement checks condition after executing first block and while loop statement first checks condition and then executes any block. The above example can be done with do while loop statement. The above example with do while statement is given below.

#include<stdio.h>
#include<conio.h>

void  main ()
{
   clrscr ();
   int counter = 5;
   do
    {
      printf (“%d\t”,counter);
      counter = counter+5;
   }while (counter<=100);
   getch();
}

This example will generate same output like above example but the structure and working procedure is different.