Pages

Sunday, September 4, 2011

Finding Maximum Logical Drives with C and C++

This article will show how to know the current drive’s name and the maximum logical drives that your system supports with C and C++. The getdisk () function is used to show the current drive name and the setdisk () function is used to know the maximum logical drives of any system. The programming code to find Maximum Logical Drives with C and C++ is given below.


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

void main()
{
  clrscr();
  int disk, maxdrive;
  disk=getdisk()+'A';
  maxdrive=setdisk(2);
  printf("Current drive is : %c\n",disk);
  printf("Maximum logical drive is:%d\n",maxdrive);
  getch();
}

Output

Wednesday, August 31, 2011

Two Dimensional Array Program with C and C++

This article will show how to find boundary elements and higher and lower level elements of a matrix or a two dimensional array with C and C++ program. The following program will first take user inputs to build a two dimensional array or a matrix and then show the matrix, the boundary elements and the sum of the boundary elements, diagonal elements and its sum. Here, for loop is used to set and retrieve array elements. 


Programming code to find boundary elements and higher and lower level elements of a matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
  int a[5][5],m,n,sum=0;
  printf("Enter row :");
  scanf("%d",&m);
  printf("Enter coloum :");
  scanf("%d",&n);
  printf("Enter matrix element :\n");
  for(int i=0;i<m;i++)
  {
            for(int j=0;j<n;j++)
            {
              printf("a[%d][%d]=",i,j);
              scanf("%d",&a[i][j]);
            }
  }
  printf("\nMatrix is : \n");
  for(i=0;i<m;i++)
  {
            for(int j=0;j<n;j++)
            {
              printf("%d\t",a[i][j]);
            }
            printf("\n");
  }
  printf("\nBoundary element :");
  for(i=0;i<m;i++)
  {
            for(int j=0;j<n;j++)
            {
              if((i*j==0)||(i==m-1)||(j==n-1))
              {
                        printf("%d  ",a[i][j]);
                        sum=sum+a[i][j];
              }
            }
  }
  printf("\nSum of boundary element is : %d",sum);

 printf("\nHigher level : ");
 sum=0;
 for(i=0;i<m;i++)
 {
   for(int j=0;j<n;j++)
   {
             if(i==j)
             {

               printf("\t%d",a[i][j]);
               sum=sum+a[i][j];
             }
   }

 }
  printf("\nSum of higher level = %d",sum);

  printf("\nLower lavel : ");
  sum=0;
  i=m;
  for(int j=0;j<n;j++)
  {
            i=i-1;
            printf("\t%d",a[i][j]);
            sum=sum+a[i][j];
  }
  printf("\nSum of lower level = %d",sum);
  getch();
}

Output

Tuesday, August 30, 2011

Array Sorting Program with C and C++

This article will show how to sort array elements with C and C++ program. The most popular Bubble Sorting algorithm is used in this program. The following program will first store inputs into array with the scanf() function and then Bubble sort algorithm is applied to sort the array elements. The complete programming code is given below. 



Array Sorting Program

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

  void main()
  {
            int a[20],N;
            clrscr();

            printf("How many array elements:");
            scanf("%d",&N);
            printf("Enter elements:\n");

            // Storing elements into an array


            for(int i=0;i<N;i++)  
            {
                 scanf("%d",&a[i]);
            }

             // Showing  unsorted array elements

            printf("Array is :"); 
            for(i=0;i<N;i++)
            {
                  printf("%d  ",a[i]);
            }

             //sorting array elements with Bubble sort algorithm


            for(i=0;i<N;i++)
            {
              int ptr=0;
              while(ptr<N-1-i)
              {
                if(a[ptr]>a[ptr+1])
                 {
                         int temp=a[ptr];
                         a[ptr]=a[ptr+1];
                         a[ptr+1]=temp;
                 }
                        ptr=ptr+1;
              }
            }

           // Showing sorted array elements

           printf("\nSorted array :"); 
           for(i=0;i<N;i++)
            {
                   printf("%d  ",a[i]);
            }
    getch ();
  } 

Output
  

Sunday, August 28, 2011

How to show a Digital Clock on the Screen using C and C++ Program

Digital Clock
The following C and C++ program will show a digital clock on the output screen. The gettime() function is being used to catch current system time and then the time is being shown with the cprintf() function. The gettime() function accepts a structure type variable reference. So, the time structure variable is being used in this program. The delay() function is also being used to hold the program execution till 1 second or 1000ms.

C and C++ program to show an digital clock

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

void main()
{

 struct time t;
 do
 {
  clrscr();
  gettime(&t);
  gotoxy(30,10);
  textcolor(2);
  cprintf("Time: %2d:%02d:%02d",t.ti_hour, t.ti_min, t.ti_sec);
  delay(1000);
 }while(!kbhit());

}

Output


How to Show Date and Time with C and C++

The following program will show how to print system date and time using C and C++. To show date and time, this program is using __DATE__ and __TIME__ macros. These macros will show date and time on the screen.

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

void main()
{
    clrscr();
    textcolor(6);
    cprintf("Showing Executing Date and Time \n");
    printf("\n") ;
    textcolor(2);
    cprintf("    Date: %s ",__DATE__);
    printf("\n") ;
    textcolor(4);
    cprintf("    Time: %s ",__TIME__);
    getch();
}

Output



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