Monday, 27 October 2014

How to Declare a Structure Variable?

Declaring a Structure Variable:

--> By defining a structure we have only created a format, the actual use of structures will be when we declare variables based on this format.

--> We declare structure variables in 2-ways:
 
       1)With Structure definition
       2)Using the Structure tag

1.With Structure definition:-

                            struct student
                            {
                                      char name[20];
                                      int rollno;
                                      float marks;
                             }stu1, stu2, stu3;

--> Here stu1, stu2, stu3 are variables of type struct "student". when we declare a variable while defining the structure template, the tagname is optional.

--> So, we can also declare them as:

                            struct
                            {
                                       char name[20];
                                       int rollno;
                                       float marks;
                             }stu1, stu2, stu3;

--> If we declare variables in this way, then we will  not be able to declare other variables of this structure type anywhere else in the program

--> So, although the tagname is optional  it is always better to specify a "tagname" for the structure.

2. Using Structure Tag:

            We can also declare structure variables using structure tag. This can be written as:

                            struct student
                            {
                                      char name[20];
                                      int rollno;
                                      float marks;
                             };
                             struct student stu1,  stu2;
                             struct student stu3;


--> Here stu1, stu2 and stu3 are structure variables that are declared using the structure tag "student".

--> Declaring a structure variable reserves "space in memory". Each structure variable declared to be of type struct student has 3 members. name, rollno, marks.

--> The "compiler" will "reserve space" for each variable sufficient to hold all the members.

Example:
 
                 each variable of type struct student will occupy 26(20+2+4) bytes.




Next




What is Structure and Explain Structure Syntax?

Structures:

Definition:

--> A "Structure" is a "Collection of related elements", possibly of "different types", having a "single name".

-->A Structure is a collection of related data elements of different data types having a single name.

-->Each element in a structure is called a "field".

-->A "field" is a smallest element of named data that has meaning. It has many of the characteristics of the variables, we have been using in our programs.

Example:
           
                Consider the book database consisting of book name, author, no. of pages and price
we define a structure to hold this information as follows:


-->The General "syntax" of structure definition as follows

                                      struct structure-name
                                      {
                                                    datatype         member 1;
                                                    datatype         member 2;
                                                    datatype         member 3;
                                                    ---------------
                                                    ---------------
                                                    datatype         member n;
                                      };

--> Here "struct" is a "keyword", which tells the compiler that a structure is being defined.

--> member 1, member 2, member 3 ------------- member n are known as the members of the structure and are declared inside curly braces.

--> There should be a "semicolon" at the end of the curly braces. These members can be of any datatype like int, char, float, array, pointer, or another structure type.

--> "structure-name" is the name of the structure and it is used further in the program to declare variables of this structure type.

--> Definition of a structure provides "one more datatype" in addition to the built in data types . We can declare variables of this new datatype that will have the format of the defining structure.

NOTE:-
                         
--> The definition of a structure does not reserve any space in memory for the member.

--> Space is reserved only when actual variables of this structure type are declared.







Friday, 24 October 2014

Explain about Binary Search with Algorithm

Binary Search:

                    Binary Search technique is implemented on sorted list of elements. It is faster than linear search, hence this method is efficient when the number of elements are large

Algorithm:

                    A non-recursive binary search has three arguments i.e L[ ](address of the list of elements), n(the number of elements in the list), key(the element to be searched).
                   
                    In this search method, the list of element is divided into two groups and the key element is searched in these groups. This process is carriedout until the element is found in the list. If the key element is not found in the list then a message "Unsuccessful search, element not found" is displayed.

Step1:

                    Initialize lowalue = 0, highvalue = 0, L[ ]
                    while(lowvalue <= highvalue) do
                    midvalue = (lowvalue + highvalue)/2
                    goto Step2.
                    Otherwise the condition fails
                    print "Unsuccessful search, element not found"
                    goto Step 5

Step2:
                    check, if(L[midvalue] = key) then
                    print "Element found at midvalue
                    goto Step5.
                    else
                   
Step3:

                    check, if(L[midvalue] > key) then
                    highvalue = midvalue - 1
                    goto step1
                    else

Step4:
         
                    check, if(L[midvalue] < key) then
                    lowvalue = midvalue + 1
                    goto step1

Step5:
                   
                     STOP


Time Complexity:

--> The Time Complexity of binary Search is O(logn / log2).
--> In Worest case O(logn) comparisions are required.
--> The number of comparisions depends on the number of elements in the list.

Program:

#include<stdio.h>
#include<conio.h>
int bs(int[],int,int,int);
void main( )
{
          int a[20],i,n,val,bsf,low,high;
          printf("\nEnter the number of elements: ");
          scanf("%d", &n);
          printf("\nEnter the %d elements: ",n);
          for(i=0;i<n;i++)
          scanf("%d", &a[i]);
          printf("\nEnter the Search element:");
          scanf("%d", &val);
          low=0;
          high=n-1;
          bsf=bs(a,val,low,high);
          if(bsf==-1)
              printf("\nThe element not found");
          else
              printf("The element found at %d location", bsf+1);
}
int bs(int a[], int ele, int low, int high)
{
           int mid;
           while(low<high)
           {
                    mid=(low+high)/2;
                    if(ele==a[mid])
                          return mid;
                    else if(a[mid]>ele)
                          high=mid-1;
                    else if(a[mid]<ele)
                          low=mid+1;
                    else
                          return -1;
             }
}




Thanks for visiting...........

Explain about Towers of Hanoi with Program, Towers of Hanoi Problem

Towers of Hanoi:

                          Towers of Hanoi is a classical example of recursion problem. It is easier, efficient and do not make use of complex data structures.
                          In this problem, there are three towers named as Source, Auxiliary and Destination. The Source tower consists of three disks of different sizes, where each disk resting on the one just larger than it which is shown below.

                     
                         The task is to move all the three disks from Source to Destination without violating the following rules.
--> At a time, only one disk may be moved.
--> Larger disk should not be placed on top of a smaller disk.
--> For the purpose of intermediate storage of disks, only one auxiliary tower should be used.

Example:

     Let us consider three disks

The Source tower consisting of three disks.
Step1:
    
          Shift one disk from Source to Destination

Step2:
   
         Shift the second disk from Source to Auxiliary
Step3:

         Shift the first disk from Destination to Auxiliary.
Step4:

         Shift the third disk from Source to Destination.

Step5:

          Shift the first disk from Auxiliary to Source.
Step6:

         Shift the second disk from Auxiliary to Destination.
Step7:

         Shift the first disk from Source to Destination.

                    Finally, the three disks are moved from Source to Destination with satisfying the given rules.

--> The Number of moves from Source to Destination are 2n-1 = 23-1=8-1=7 moves.

Program Logic:





Program:

#include<stdio.h>
#include<math.h>
void towers(int, char, char, char);
void main( )
{
                int d,m;
                printf("\nEnter the Number of Discs: ");
                scanf("%d", &d);
                m = pow(2,d)-1;
                printf("The Number of moves required are: %d", m);
                towers(d,'A','C','B');
}
void towers(int d, char from, char to, char aux)
{
                if(d==1)
                {
                         printf("\nMove the disc from %c to %c", from, to);
                }
                else
                {
                         towers(d-1,from,aux,to);
                         printf("\nMove the disc from %c to %c", from, to);
                         towers(d-1,aux,from,to);
                 }
}





Thanks for visiting.........

Monday, 20 October 2014

C Program that calculates the Volume of the Sphere

Program:

#include<stdio.h>
#include<conio.h>
#define pi 3.14
void main( )
{
         int r,
         float v;
         printf("\nEnter the radius of the Sphere:");
         scanf("%d",&r);
         v=(4/3)*pi*r*r*r;
         printf("\nVolume of the Sphere is %f",v);
}


Input/Output:

Enter the radius of the Sphere:1

Volume of the Sphere is  4.082000





C Program that Calculates the area of a square by giving length of the side from the user

Program:

#include<stdio.h>
#include<conio.h>
void main( )
{
         int a,l;
         printf("\nEnter the length of the Square:");
         scanf("%d",&l);
         a=l*l;
         printf("\nArea of the Square is %d",a);
}


Input/Output:

Enter the length of the Square:10

Area of the Square is 100

Write a program to read three numbers from the user and find the highest number

Program:

#include<stdio.h>
#include<conio.h>
void main( )
{
        int x,y,z,h;
        clrscr( );
        printf("\nEnter Three Numbers:");
        scanf("%d%d%d",&x,&y,&z);
        h=((x>y)&&(x>z))?x:(y>z)?y:z;
        printf("\nHighest Number is %d",h);
        getch( );
}


Input/Output:

Enter Three Numbers:10
20
30

Highest Number is 30




Write a program to read two numbers from the user and find highest number

Program:

#include<stdio.h>
#include<conio.h>
void main( )
{
           int x,y,h;
           clrscr( );
           printf("\nEnter two Numbers:");
           scanf("%d%d",&x,&y);
           h=(x>y)?x:y;
           printf("\nHighest Number is %d",h);
           getch( );
}


Input/Output:

Enter two Numbers: 10
20

Highest Number is 20




C program to Print the following Output

1 2 3 4 5 6 7 7 7 8 9 10

Program:

#include<stdio.h>
#include<conio.h>
void main( )
{
          int i,j;
          for(i=1;i<=10;i++)
          {
                     if(i==7)
                     {
                              for(j=1;j<=3;j++)
                              {
                                          printf("%d  ",i);
                               }
                       }
                       printf("%d  ",i);
            }
}


Input/Output:

1 2 3 4 5 6 7 7 7 8 9 10



Sunday, 19 October 2014

C Program for finding Second Largest Number in the given array of elements

Program

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,j,temp,n;
        clrscr( );//clear the screen contents
printf("\nEnter size of the Array:");
scanf("%d",&n);
printf("\nEnter elements to Array:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)// Sorting logic begins
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}//ends
printf("Second largest: %d",a[n-2]);
getch();
}


Input/Output:

Enter size of the Array: 5

Enter elements to Array:5
4
8
2
1
Second largest: 5



C Program for Given Number is Even or Odd using Bit-Wise Operators

Program:

#include<stdio.h>  //header file
#include<conio.h>  //header file
int odd(int); // funtion proto type
void main( )
{ //main function begins
          int n;
          printf("\nEnter a Number:");
          scanf("%d",&n);
          if(odd(n))
          {
                printf("\n Given Number is ODD");
           }
           else
           {
                printf("\n Given Number is EVEN");
            }
}//main function ends
int odd(int n)
{
           if(n&1)
           {
                      return 1;
            }
            else
            {
                       return 0;
             }
}

Input/Output:

Enter a Number: 5

Given Number is ODD