Wednesday, 9 November 2011

HAPPY GURU NANAK'S BIRTHDAY

Guru Nanak Dev about Truth
Today is full moon again and it is the birthday of Guru Nanak Dev. I wrote about him in the last year, too, and mentioned how his teachings are showing love and simplicity. Of course, he is nowadays seen as the founder of a new religion but in that time, he himself probably did not see it in this way. I believe he simply was a very spiritual person with no intention to actually start a new tradition.
He saw god in truth. This is why he called God ‘Satnam’ which means eternal truth. In one of his scriptures, the Japji Sahib, there is a phrase that I liked:
Aad Sach, Jugaad Sach Hai Bhee Sach, Nanak Hosee Bhee Sach |
A common translation is ‘The True One was in the beginning, the True One was in the primal age, the True One is now also and Nanak says the True One also shall be.’
Truth was in the beginning, has always been, is right now in the present and will be always in the future. God is truth.
If you have a look in the Vedanta philosophy you will actually find the same idea. The Vedanta says ‘Om Tat Sat’. Both philosophies say that truth is stable and truth will prevail.
For Nanak God created everything but was not Himself created. The same thing is believed in Vedanta, too. In Hinduism there are more scriptures that say ‘The biggest religion is truth’. People have told me that in Christianity, too, one of the ten amendments is that you should not lie.
I don’t believe that any religion would ask you to lie. Every religion, every belief will tell you to be honest, to be true and to that truth is right. I don’t believe in any religion but one of my own main beliefs is that you have to be honest. It doesn’t matter whom you follow or if you don’t follow anyone, there are some basic principles that everybody can take in his heart and follow. One of them is truth, another one is love. For me this is God. Whether you call it God, satnam, truth or love, it is all the same in the end.

Thursday, 3 November 2011

C programs

C code to print or display lower triangular matrix


#include<stdio.h>
int main(){
  int a[3][3],i,j;
  float determinant=0;
  printf("Enter the 9 elements of matrix: ");
  for(i=0;i<3;i++)
      for(j=0;j<3;j++)
           scanf("%d",&a[i][j]);
  printf("\nThe matrix is\n");
  for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
           printf("%d\t",a[i][j]);
  }
   printf("\nSetting zero in upper triangular matrix\n");
   for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
           if(i<=j)
             printf("%d\t",a[i][j]);
           else
             printf("%d\t",0);
  }
   return 0;
}
Enter the 9 elements of matrix: 1
2
3
4
5
6
7
8
9
The matrix is
1       2       3
4       5       6
7       8       9
Setting zero in upper triangular matrix
1       2       3
0       5       6
0       0       9



                    C code to print or display upper triangular matrix


#include<stdio.h>
int main(){
  int a[3][3],i,j;
  float determinant=0;
  printf("Enter the 9 elements of matrix: ");
  for(i=0;i<3;i++)
      for(j=0;j<3;j++)
           scanf("%d",&a[i][j]);
  printf("\nThe matrix is\n");
  for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
           printf("%d\t",a[i][j]);
  }
   printf("\nSetting zero in upper triangular matrix\n");
   for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
           if(i>=j)
             printf("%d\t",a[i][j]);
           else
             printf("%d\t",0);
  }
   return 0;
}
Sample output:
Enter the 9 elements of matrix: 1
2
3
4
5
6
7
8
9
The matrix is
1       2       3
4       5       6
7       8       9
Setting zero in upper triangular matrix
1       0       0
4       5       0
7       8       9


INSERT AN ELMENT IN AN ARRAY AT DESIRED POSITION USING C PROGRAM

#include<stdio.h>
int main(){
int a[50],size,num,i,pos,temp;
printf("\nEnter size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ",size);
for(i=0;iscanf("%d",&a[i]);
printf("\nEnter position and number to insert: ");
scanf("%d %d",&pos,&num);
i=0;
while(i!=pos-1)
i++;
temp=size++;
while(i{
a[temp]=a[temp-1];
temp--;
}
a[i]=num;
for(i=0;iprintf(" %d",a[i]);
return 0;
}




C code to find largest and smallest number in an array

#include<stdio.h>
int main(){
  int a[50],size,i,big,small;
  printf("\nEnter the size of the array: ");
  scanf("%d",&size);
  printf("\nEnter %d elements in to the array: ", size);
  for(i=0;i<size;i++)
      scanf("%d",&a[i]);
  big=a[0];
  for(i=1;i<size;i++){
      if(big<a[i])
           big=a[i];
  }
  printf("Largest element: %d",big);
 
  small=a[0];
  for(i=1;i<size;i++){
      if(small>a[i])
           small=a[i];
  }
  printf("Smallest element: %d",small);
  return 0;
}
Sample Output:
Enter the size of the array: 4
Enter 4 elements in to the array: 2 7 8 1
Largest element: 8
Smallest element: 1



DELETE ELEMENT FROM AN ARRAY AT DESIRED POSITION USING C

void main()
{
  int a[50],i,pos,size;
  clrscr();
  printf("\nEnter size of the array: ");
  scanf("%d",&size);
  printf("\nEnter %d elements in to the array: ",size);
  for(i=0;i<size;i++)
            scanf("%d",&a[i]);
  printf("\nEnter position where to delete: ");
  scanf("%d",&pos);
  i=0;
  while(i!=pos-1)
            i++;
  while(i<10)
  {
            a[i]=a[i+1];
            i++;
  }
  size--;
  for(i=0;i<size;i++)
            printf(" %d",a[i]);
  getch();
}


simple bubble sort implementation using array ascending order in c programming language

#include<stdio.h>
int main(){
  int s,temp,i,j,a[20];
  printf("Enter total numbers of elements: ");
  scanf("%d",&s);
  printf("Enter %d elements: ",s);
  for(i=0;i<s;i++)
      scanf("%d",&a[i]);
  //Bubble sorting algorithm
  for(i=s-2;i>=0;i--){
      for(j=0;j<=i;j++){
           if(a[j]>a[j+1]){
               temp=a[j];
              a[j]=a[j+1];
              a[j+1]=temp;
           }
      }
  }
  printf("After sorting: ");
  for(i=0;i<s;i++)
      printf(" %d",a[i]);
  return 0;
}

Output:
Enter total numbers of elements: 5
Enter 5 elements: 6 2 0 11 9
After sorting:  0 2 6 9 11


simple quick sort implementation using array ascending order in c programming language

#include<stdio.h>
void quicksort(int [10],int,int);
int main(){
  int x[20],size,i;
  printf("Enter size of the array: ");
  scanf("%d",&size);
  printf("Enter %d elements: ",size);
  for(i=0;i<size;i++)
    scanf("%d",&x[i]);
  quicksort(x,0,size-1);
  printf("Sorted elements: ");
  for(i=0;i<size;i++)
    printf(" %d",x[i]);
  return 0;
}
void quicksort(int x[10],int first,int last){
    int pivot,j,temp,i;
     if(first<last){
         pivot=first;
         i=first;
         j=last;
         while(i<j){
             while(x[i]<=x[pivot]&&i<last)
                 i++;
             while(x[j]>x[pivot])
                 j--;
             if(i<j){
                 temp=x[i];
                  x[i]=x[j];
                  x[j]=temp;
             }
         }
         temp=x[pivot];
         x[pivot]=x[j];
         x[j]=temp;
         quicksort(x,first,j-1);
         quicksort(x,j+1,last);
    }
}
Output:
Enter size of the array: 5
Enter 5 elements: 3 8 0 1 2
Sorted elements: 0 1 2 3 8

simple merge sort implementation using array in ascending order in c programming language
#include<stdio.h>
#define MAX 50
void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);
int main(){
   
    int merge[MAX],i,n;
    printf("Enter the total number of elements: ");
    scanf("%d",&n);
    printf("Enter the elements which to be sort: ");
    for(i=0;i<n;i++){
         scanf("%d",&merge[i]);
    }
    partition(merge,0,n-1);
    printf("After merge sorting elements are: ");
    for(i=0;i<n;i++){
         printf("%d ",merge[i]);
    }
   return 0;
}
void partition(int arr[],int low,int high){
    int mid;
    if(low<high){
         mid=(low+high)/2;
         partition(arr,low,mid);
         partition(arr,mid+1,high);
         mergeSort(arr,low,mid,high);
    }
}
void mergeSort(int arr[],int low,int mid,int high){
    int i,m,k,l,temp[MAX];
    l=low;
    i=low;
    m=mid+1;
    while((l<=mid)&&(m<=high)){
         if(arr[l]<=arr[m]){
             temp[i]=arr[l];
             l++;
         }
         else{
             temp[i]=arr[m];
             m++;
         }
         i++;
    }
    if(l>mid){
         for(k=m;k<=high;k++){
             temp[i]=arr[k];
             i++;
         }
    }
    else{
         for(k=l;k<=mid;k++){
             temp[i]=arr[k];
             i++;
         }
    }
   
    for(k=low;k<=high;k++){
         arr[k]=temp[k];
    }
}
Sample output:
Enter the total number of elements: 5
Enter the elements which to be sort: 2 5 0 9 1
After merge sorting elements are: 0 1 2 5 9