Thank you all for being with us.
Learn, Create and Implement.

  • Build Concept

    You can build your concept in programming on programsway.com.

  • Examples

    There are many C Programming examples on programsway.com.

  • Ask Questions

    Ask questions and get quick answers. Request any program and we will publish that program.

  • Showing posts with label Examples. Show all posts
    Showing posts with label Examples. Show all posts

    Thursday, 9 January 2014

    Written by Kousik :
    C provides some fundamental DATA types. They are int, float, char, double. They are very useful and important in C language. But they have a limiting fact. The fact is that a variable of these types can store only one value at a time. With variables of those types we cannot handle a large volume of data of same type at a time. So we need a powerful data type with which we can solve this problem. C supports such a data type called ARRAYS.
    Definition: An array is a well defined collection of elements of same data type. In others words One can say that an array is grouping of some elements that are of same data type. Some applications of arrays in C are given below:
    §  To store age of some boys.
    §  To store marks of subjects.
    §  To store daily rainfall data.
    And so on.
    Declaration of ARRAYS:  ARRAY in C can be declared by following format
    int array[50]; , float temperature[7];
    See carefully that an array is declared by defining the data type first then the name and then the  size of array.
    Now a question should be come to reader’s mind and that is how can we take input of many elements at a time by using array? Answer of this question is very easy. This problem can be solved by using for loop for taking input by using following syntax.
    For (i=0; i<n; i++)
    Scanf (“%d”, &arr[i]);
    Where n is the size of the array and arr is the name of array.
    Now here a example is given to understand ARRAYS and the way of using an ARRAY in C.


    #include<stdio.h>
    int main()
    {
    int a[25],i,n,sum=0,mean;
    printf("Enter the number of array elements\n");
    scanf("%d",&n);
    printf("Enter the value of array elements\n");
    for(i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
    Printf(“%d ”, a[i]);
    }
    return 0;
    }
    There are two kinds of array those can be used in C language. One is ONE DIMENSIONAL and the other one is TWO DIMENSIONAL.
    In this section we will discuss about only one dimensional array.
    INITIALIZATION OF ONE DIMENSIONAL ARRAY: After declaring an array its elements can be initialized. Otherwise elements of array are of ‘garbage’. The following syntax can be used to initialize an array.
    Data type  variablename[size]={values};
    Example : int student[3]={1,2,3};
    Above example is called compile time initialization. Another type of initialization can be used for initialize an array. That is Run time initialization. For example take the following segment.

      for(j=0;j<50;j++)
      {
        If(j<20)
       A[i]=0.0;
       Else
       A[i]=1.0;
      }
     We can also use a scanf function to initialize an array as we discuss earlier with the answer of the question how to take input value of elements of an array.
    The following example illustrates a sorting program in ascending order as an important application of arrays in C science.


    /* a program to sort an array */
    # include<stdio.h>
    int main()
    {
    int a[25],i,j,n,temp,item;
    char c;
    printf("Enter the number of array elements\n");
    scanf("%d",&n);
    printf("Enter the value of array elements\n");
    for(i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
    }
    /* sorting of array */
    for(i=0;i<n;i++)
    {
    for(j=i+1;j<n;j++)
    {
    if(a[i]>a[j])
    {
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
    }
    }
    }
    printf("After sorting \n");
    for(i=0;i<n;i++)
    {
    printf("%d ",a[i]);
    }
    }









     


    Sunday, 28 July 2013

    Written by Saikat :

    DIVIDE AND CONQUER ALGORITHM:

    The most commonly used & convenient algorithm is the 'Divide & conquer' algorithm. If a problem is tough to solve, then we can assume that this problem is made up by some small problems. Then we can break the main problem in some small sub-problems. Now we can easily solve the sub-problems. By this we can solve the whole problem very easily. This method is called Divide and Conquer method. By this method we can also develop efficient programs to solve a particular problem. 

    STEPS INVOLVED IN THIS ALGORITHM: 

    1) Divide the total problem into several sub problems of equal length.
    2) Obtain the solution of the individual sub problems.
    3) Combine the solutions to get the main solution.

    ASPECT:

    The basic aspect of this algorithm is to minimize the time complexity of a given problem as much as possible. There may be various types of algorithms to solve a particular problem. Among   those we shall choose that one, which will take a polynomial order of time complexity, i.e. we will have to choose in such a way that it requires a least number of operations. It will be better if it takes time complexity of linear order.


    EXAMPLE:

               Take the example of the finding out the maximum and minimum from a set of integers. If we look the overall problem then we will face some challenges. So, just forget the whole problem. Now if there are two numbers then we can easily find out which is smaller and which is bigger by following algorithm.

    If(a>b)  then  MAX <- a and MIN <- b
    Else MAX <- b and MIN <- a

    This is an easy problem to solve. So, we assume this problem as a unit problem and break the whole problem into sub problems.

    Now look at the program and find out what we are doing in it.
    #include<stdio.h>
    #include<stdlib.h>

    int *a;

    typedef struct list
    {
      int min,max;
    }ln;

    ln minmax(int,int);
    int min(int,int);
    int max(int,int);

    main()
    {
      int i,n;
      ln ext;
      printf("Enter the number of element ");
      scanf("%d",&n);
      a=(int *)calloc(n,sizeof(int));
      printf("Enter the numbers ");
      for(i=0;i<n;i++)
      scanf("%d",(a+i));
      ext=minmax(0,n-1);
      printf("The minmum value is %d and the maximum value is %d",ext.min,ext.max);
      return;
    }

    ln minmax(int n1,int n2)
    {
      ln ext,ext1,ext2;
      if(n2-n1==1)
      {
        ext.min=min(a[n1],a[n2]);
    ext.max=max(a[n1],a[n2]);
    return ext;
      }
      else
      {
      ext1=minmax(n1,(n1+n2)/2);
      ext2=minmax((n1+n2)/2+1,n2);
      ext.min=min(ext1.min,ext2.min);
      ext.max=max(ext1.max,ext2.max);
      return ext;
      }
    }

    int max(int a, int b)
    {
      if(a>b)
      return a;
      else
      return b;
    }

    int min(int a, int b)
    {
      if(a<b)
      return a;
      else
      return b;
    }

    Thursday, 25 July 2013


    Posted by Kousik
    This is a program to calculate your age.
    /*AGE CALCULATE*/
    #include<stdio.h>
    int main()
    {
    int dd,dm,dy,cd,cm,cy,d,m,y;
    printf(“Enter your DATE OF BIRTH in dd mm yyyy format\n”);
    scanf(“%d %d %d”,&dd,&dm,&dy);
    printf(“Enter Current Date in dd mm yyyy format\n”);
    scanf(“%d %d %d”,&cd,&cm,&cy);
    y=cy-dy;
    if(dm>cm)
    {
    y--;
    m=12-(dm-cm);
    if(dd>cd)
    {
    m--;
    d=30-(dd-cd);
    }
    else
    {
    d=cd-dd;
    }
    }
    else
    {
    m=cm-dm;
    if(dd>cd)
    {
    m--;
    d=30-(dd-cd);
    }
    else
    {
    d=cd-dd;
    }
    printf(“You AGE is \n”);
    printf(“%d days %d months %d years\n”,d,m,y);
    }