Wednesday, February 5, 2014

Choice Program [using if-else]

//Choice Program[simple condition testing]
#include<stdio .h>
//#include<conio .h>
 void main()
{
  int a,b,ch,sum,dif,prod,quot;
  //clrscr();
  printf("\n Enter the two numbers and your choice");
  printf("\n 1 --> SUM\n 2--> DIFFRENCE");
  printf("\n 3 --> PRODUCT\n 2--> DIVIDE \n");
  scanf("%d%d%d",&a,&b,&ch);
 //The same program maybe written using switch case statements
   if(ch==1)
  {
  sum=a+b;
  printf("Sum [%d + %d] : %d\n",a,b,sum);
  }
   else if(ch==2)
  {
  dif=a-b;
  printf("Difference [%d - %d] : %d\n",a,b,dif);
  }
   else if(ch==3)
  {
  prod=a*b;
  printf("Product [%d * %d] : %d",a,b,prod);
  }
   else if(ch==4)
  {
   if(b==0)
   {
   printf("\n Divide by zero error");
   exit(1);
   }
   quot=a/b;
   printf("Quotient [%d / %d] : %d",a,b,quot);
  }
   else
        printf("\n Invalid Input \n");

  //getch();
}


Bubble Sort

//Bubble Sort
//One of the simplest sorting algorithms, but time consuming.

#include<stdio.h>  
//Standard input/output header
#include<conio.h>
void main()
{
   int i,j,temp,a[10],n;
   clrscr();
   printf("\n Enter the array size");
   scanf("%d",&n);  //Size of array to be sorted.
   printf("\n Enter the array elements to be sorted");
   for(i=0;i<n;i++)
   {
      scanf("%d",&a[i]); 
      // Input array element by element.
   }
   printf("\n The given array is...\n");
   for(i=0;i<n;i++)
   { 
     printf("%d ",a[i]); //print input array
   }
   for(i=0;i<n;i++)
   {
     for(j=0;j<n-i-1;j++)
    {
      if(a[j]>a[j+1]) //if current element is greater than
                            //the next element in the array
     {
       temp=a[j];     //current element in temp
       a[j]=a[j+1];   //next element in current loc.
       a[j+1]=temp; //temp to next location
     }
    }
   }
   printf("\n Sorted array is...\n");
   for(i=0;i<n;i++)
   {
     printf("%d\n",a[i]);  //Display sorted array
   }
   getch();
}



Dissecting The Program


The program in the last post is very simple , consisting  just few lines of C code . But it is very important to understand this as it serves as the foundation for larger programs. As they say " Journey Of A Thousand Miles Begins With A Single Step ".
Let's look at the program line by line .
#include<stdio.h>
The Header of the program is very necessary if you want to use the printf statement . Without this line the printf statement won't work.  We will  look at it in detail later when we are writing our own libraries. As of now all we need to know is the printf statement is declared in the stdio.h library and stdio.h stands for Standard Input Output Header. Some other libraries are conio.h , math.h (Just remember the names for now)
 void main ()     
    {
    }

This is the Main part of the program . If you are given a task to do something you need to know how to start the task right?? Similarly main is used so that the computer can know from where it has to start executing the program. The computer executes whatever is inside the Main function`s body.The body of the main function is defined by the curly braces {  } .
printf ("Hello World");

This is the line that prints Hello World onto the screen . We will be using this function more than any other function (Trust Me) 

So thats it lets get started with a few practical aspects from the next post .

Starting Things Practically

You may use various compilers like Dev C++ , Turbo C++ , Visual C , etc to compile your programs.


Installing a C compiler

First we need a C compiler installed on our machine if its an linux system you can open the terminal and check by typing the command below

cc

if you are a windows guy then follow the steps to download the Dev C++ from Bloodshed


Go to the url below and click on the link show the pic(Highlighted with red box around it)


http://www.bloodshed.net/dev/devcpp.html



After clicking it should start the download

........................................ After the download is completed

Run the executable  in the choose components  click on remove previous versions if you have already .

After the setup is finished start the Dev C ++ and keep clicking next

Then it should look like this





Then go to File --- New --- Source File   And click on it or directly pres ctrl+n

then type the following code

#include<stdio.h>
#include<conio.h>
int main()
{
printf("Hello World");
getch();
}

Then click  on Execute Click On Compile
after the program has been compiled
Go to Exexute and click on Run







Congratulations, You Have Successfully Written Your First C Program!


Binary Search

#include<stdio.h>         //Standard input/output header
// #include<conio.h>
  void main()
{
   int i,n,a[10],mid,low,high,key;
   // i is the looping variable
  // clrscr();
   printf("\n Enter the array size");
   scanf("%d",&n);        // Size of array is to be
                          // given as input (n)

   printf("Enter the array element in the ascending order");
   for(i=0;i<n;i++)
    {                  
     //The input array must be in ascending order
              // for a binary search.

     scanf("%d",&a[i]); //Input the array elements one-by-one
    }
   printf("\n Enter the key element to be searched");
   scanf("%d",&key);
   low=0;          // low is initialized to 0
   
high=n-1;       // high is initialized to the
   while(low<=high)
    {
      mid=(low+high)/2;     //Find the middle index in the array
      if(key==a[mid])
      {
        //if the required number(key)
        //is the mid element,
        //Element would be at location mid+1
        // as array starts with index 0.
       
       printf("The Element was found at position %d",mid+1);
       //getch();
       exit(0);             //Successful exit indicated by 0.
      }
     else if(key>a[mid])   // if key is found to be
      {             //bigger than mid,low must be made mid+1
        low=mid+1;    
       //so that search goes only between (mid+1) and (high)
     
}                            // elements in the array.
     else
      {
       //if key is found to be smaller than mid
       // then (high) must be made (mid-1)

        high=mid-1;    
       //so that search goes only between (low) and (mid-1)
     
}                           // elements in the array.
    }
  printf("\n Unsuccessful search\n");
  // getch();
 }



Wednesday, October 9, 2013

Bigger Of Two Numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter two numbers");
scanf("%d%d",&a,&b);
if(a>b)
printf("%d is greater than %d",a,b);
else printf("%d is greater than %d",b,a);
getch();
}

Tuesday, September 24, 2013

C Programming

  C is a general-purpose programming language initially developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs.
  C is one of the most widely used programming languages of all time, and C compilers are available for the majority of available computer architectures and operating systems.
  Many later languages have borrowed directly or indirectly from C, including C#, D, Go, Rust, Java, JavaScript, Limbo, LPC, Objective-C, Perl, PHP, Python, Verilog (hardware description language) and Unix's C shell.


Basic Structure of a C Program is as follows [in order]  :
1.Pre-processor Directives / Header-Files
2.Global-variables
3.Main Function[with-in the main is the local variable declaration and other instructions]
4.Other User defined functions

Note : Every program in C starts executing from the first instruction in the main() and ends with the last instruction in the main().
C Compiles which may be used to compile C programs are : Turbo C, GCC, Dev C, Visual C, Code Blocks ,etc.
Various compilers have minor changes in the compiler-specific commands' syntax[i.e. commands which can be interpreted only by specific compilers like clrscr() to clear screen in Turbo C].

Example C Program :

/* Hello World program */  #include<stdio.h>
  int main()
  {
      printf("Hello World");
      return 0;
  }
next..