Thursday 8 December 2016

Structures


A structure is a collection of multiple data types that can be referenced with a single name. It may contain similar or different data  types. The data elements in a structures are called structure elements, members or fields. The structures are used to join simple variables together to form complex variable.

The difference between array and structure is the array consist of a set of variables of same data type , and structure may consist of a set of variables of different data types.

Uses of Structure:

The structures is used to define new data types. The user can define a new data type by combining multiple data types in a structure. A simple variable contain single value at a time, but a structure can stores multiple values at the same time.

Declaring a Structure:

A structure is declared by using struct keyword followed by the structure name. The structure elements are defined  with their type inside the braces. A semi-colon is added after the closing braces in structure.Structure declaration is terminated by the semi-colon(;).The declaration tells the compiler about the details of structure. The compiler doesn't allocate any memory at that time.
Syntax:
 struct Structure_Name
{
     Data_Type identifier1;
     Data_Type identifier2;
     Data_Type identifier3;
                   :
                   :
     Data_Type identifierN;
};

Example:
  struct Student
 {
       int rollNo, marks;
       float Percentage;
       char Grade;
 };

The above structure is declared a new data type named as Student, which contain four member variables. This Student structure can store four values at a time.

Defining Structure Variable:

The process of defining structure variable is same as defining primitive data types such as int, char, float. The definition tells the compiler to allocate memory space for the variable.The compiler automatically allocates sufficient memory according to the elements of the structure.
Syntax:
Structure_Name identifier;
Structure_Name: is the name of the structure used in structure declaration.
Identifier: is the name of the variable to be defined.
Example:
Student Ahmed;

In the above example Ahmed define the structure variable(Student). The variable contain four member variables defined in Student structure. The statement allocates the memory space for all four members of the structure(Student).
The structure variable Ahmed  will occupy 9 bytes in the memory. The member variable rollNo occupies 2 bytes, marks occupies 2 bytes, percentage occupies 4 bytes and variable Grade occupies 1 byte in memory. Total memory occupied by the variable of type Student will be 9 bytes.

Accessing Member of Structure Variable:

Any member of  a  structure variable can be accessed by using dot operator. The name of the structure variable is written on the left side of the dot, and name of the structure member is written on the right side of the dot operator. The dot operator is also known as member access operator.
Syntax:
Structure_Variable.Memeber_Variable;
Example:
 Student Ahmed;
 Ahmed.rollNo = 01;
 Ahmed.marks =  885;
 Ahmed.Percentage = 80.45;
 Ahmed.Grade = 'A';

The above example creates a structure variable Ahmed of type Student . It use four assignment statement to store values in member variable of Ahmed. The same method is used  for input values in member variable and to display them.
Example:
cin>>Ahmed.rollNo;
cin>>Ahmed.marks;
cin>>Ahmed.Percentage;
cin>>Ahmed.Grade;

cout<<Ahmed.rollNo;
cout<<Ahmed.marks;
cout<<Ahmed.Percentage;
cout<<Ahmed.Grade;

Array of Structure:

An array of structure is a type of array in which each element contains a complete structure. It can be used to store many records.
Example:
struct stud{
   char id[6];
   char name[50];
   char gender;
   int age;
};
stud s[3];

The above example declares a structure  stud. It define an array of structures s[3]. The array can store the record of three students.


The array is accessed  by using its index. The structure is  accessed by using dot operator.

Nested Structure:

A structure within a structure is known as nested structure. A nested structure is created when the member of a structure is itself a structure. 
Example:
struct A 
{
    int n; 
    float b;
};
struct B 
{
    char c; 
    A x;
};

The above example declares two structures A and B. The structure A contains simple member variables n and b. The structure B also contains two member variables. The first member is character variable c and the second member is a structure variable x. It is an example of a nested structure. 

Write a program that uses three structures Dimension, Result and Rectangle. The  Dimension structure stores length and width, Result structure stores area and perimeter and Rectangle stores two variables of Dimension and Results. The program declares a variable type Rectangle input length, width, calculate area and perimeter and then display the result.


        #include <iostream.h>
        #include<iomanip.h>
        #include <conio.h>
               
         struct Dimension 
         {
                  double length; 
                  double width: 
         };
         
         struct Result
         {
                  double area; 
                  double perimeter;
         };

         struct Rectangle 
         {
                  Dimension size; 
                  Result stat; 
         };

         int  main ()
         {
                  Rectangle box;
                  cout<< "Enter the length of  a rectangle: " ;
                  cin>>box.size.length;

                  cout<< "Enter the width of  a rectangle: " ;
                  cin>>box.size.width;
                 
                  box.stat.area = box.size.length * box.size.width;
                  box.stat.perimeter =  2 *  box.size.length + 2 *  box.size.width;

                  cout.setf(ios::fixed, ios::floatfield);
                  cout.setf(ios::showpoint);
                  cout<<setprecision(2);

                 cout<< "The area of rectangle is : "  << box.stat.area <<endl;
                 cout<< "The perimeter of rectangle is : "<< box.stat.perimeter << endl;

                 return 0;
         }

Programs:

1.   Write a C++ program that declares a structure to store the distance covered by an player along with the minutes and seconds taken to cover the distance. The program should input the records of two players and then display the record of the winner.

2.  Write a C++ program that declares a structure to store the code number, salary and grade of the employee. The program defines two structure variables, inputs records of two employees and then displays the record of the employee with more salary.

3.  Write a C++ program that declares a structure to store income, tax rate and tax of the person. The program defines an array of structure to store the record of the five persons. It input income and tax rate of five persons and then displays the tax payable.

4.  Write a C++ program that declares a structure Book  to store bookId , Book name and price, it declares another structure Order that contains OrderId and an array of Book of length 5. The program should define a variable of type Order and input the values from the user. The program finally display the values.

2 comments:

If you have any doubt please let me know

Methods in JAVA Language

 In general, a way may be thanks to perform some task. Similarly, the tactic in Java may be a collection of instructions that performs a sel...