Pages

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