Tuesday, January 29, 2019

Structure and Union



Write differences between structure and union.
Structure
Union
Structure is design by using ‘struct’ keyword
Union is design by using ‘union’ keyword.
All the member of the structure variable can be processed at a given time
Only one member of the union variable can be processed at a time because only one member of the union variable can be active at a time.
We use structure variable if memory is large and have to store values of the entire variable.
We use union variable if memory is less and have to store one variable in one of the declared variable or members.
Structure are broadly used in programming
Union are not broadly as much as structure
Structure declaration takes large amount of space to store data and values
Union declaration shares the same area of memory to save storage space
The amount of memory required to store a structure variable is the sum of the size of all members.

Example:
struct student
{
char name[30]; //char=1 byte *30
char phone[10]; //char=1 byte * 10
};
struct student s;
The size of the above structure s is the sum of the two member i.e. 40 bytes.
The amount of memory required to store a union variable is equal to the size required by the largest member of the union.

Example:
union student
{
char name[30]; //char=1 byte *30
char phone[10]; //char=1 byte * 10
};
union student u;
The size of the above union variable u is equal to the size of the largest member i.e.30 bytes
Syntax:

struct   structure_name
{
data_type member1;
data_type member2;
………………………………….
};


Syntax:

union  union_name
{
data_type member1;
data_type member2;
………………………………….
};


Differentiate between structure and pointer with example
Structure
Pointer
Structure is the collection of heterogeneous data  under a common name
Pointer is a variable which holds the memory address of another variable instead of directlyholding the value assigned by the user
Structure is design by using ‘struct’ keyboard
Data types such as int, float,char etc are used to declare pointer variables.
Structure can be a member of pointer
A structure can’t include in a pointer declaration.
Syntax:
struct  structure_name
{
data_type  member1;
data_type  member2;
………………………………….
};

Example:
struct student
{
char name[30];
char phone[10];
};

Syntax:
data_ type *pointer_name ;

Example:
int *sum
float *p
Structure is useful to bind dissimilar values which are logically related with each other such as student name, phone etc.


Pointer is useful to handle the memory address of other variables and process them easily.

Write a program to store name and mark of 20 students. Sort the data according to mark in descending order and display them. [HSEB 2066]
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main( )
{
struct student
{
char name [25];
int mark;
};
struct student std[10];
char temp[25];
int i,j,tm;

printf("Enter names and marks for 20 students\n");
for(i=0;i<20;i++)
 {
scanf("%s",std[i].name);
scanf("%d",&std[i].mark);
 }
for(i=0;i<20;i++)
 {
for(j=i+1;j<20;j++)
 {
if(std[i].mark<std[j].mark)
 {
tm=std[i].mark;
std[i].mark=std[j].mark;
std[j].mark=tm;
strcpy(temp,std[i].name);
strcpy(std[i].name,std[j].name);
strcpy(std[j].name,temp);
 }
 }
 }
printf("\nThe sorted names and marks in descending order according to marks are\n");
for(i=0;i<20;i++)
printf("\n%s\t%d",std[i].name,std[i].mark);
getch();
 }

Output:
Enter names and marks for 20 students
santosh  45
muna 34
kiran 67
kamal 68
bina 99

The sorted names and marks in descending order according to marks are
bina 99
kamal 68
kiran 67
santosh  45
muna 34


Write a program that reads different names and addresses into the computer and rearrange them into alphabetical order using the structure variables. [HSEB 2061, 2064]

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main ()
{
struct student
{
char name[30];
char add [30];
}std[100];
char tname[30],tadd[30];
int i,j,n;
printf("\n Enter how many students: ");
scanf("%d",&n);
printf("Enter names and addresses for %d students:",n);
for(i=0;i<n;i++)
scanf("%s%s",std[i].name, std[i].add);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if (strcmp(std[i].name,std[j].name)>0)
{
strcpy(tname,std[i].name);
strcpy(std[i].name,std[j].name);
strcpy(std[j].name,tname);
strcpy(tadd,std[i].add);
strcpy(std[i].add,std[j].add);
strcpy(std[j].add,tadd);
}
}
}
printf("\n Sorted names in alphabetical order according to names are:\n");
for(i=0;i<n;i++)
printf("\n %s\t %s",std[i].name,std[i].add);
getch();
}

Write a program to enter the 5 subject marks and calculate sum and print using structure. [2074]
#include<stdio.h>
#include<conio.h>
      main()
     {
        struct student
{
         int sub1;
          int sub2;
         int sub3;
          int sub4;
         int sub5;
        };
             struct student s[5];
             int i,sum=0;
            
             for(i=0;i<=4;i++)
                 {
                   printf("\nEnter Marks in Five Subjects = ");
                   scanf("%d%d%d%d%d",&s[i].sub1,&s[i].sub2,&s[i].sub3,&s[i].sub4,&s[i].sub5);
                   sum=s[i].sub1+s[i].sub2+s[i].sub3+s[i].sub4+s[i].sub5;
                   printf("\nTotal marks = %d",sum);
                 }
                        getch();

         }

Write a program to enter the 20 employee's name, age and salary using structure and print them

#include <stdio.h>
#include<conio.h>
int main()
{
struct student
{
    char name[50];
    char dept[10];
    int salary;
} s[20];
    int i;
    printf("Enter information of employee:\n");

    // storing information
    for(i=0; i<20; ++i)
    {
            printf("Enter name, dept and salary ");
        scanf("%s%s%d",s[i].name,s[i].dept,&s[i].salary);

              printf("\n");
    }

    printf("Displaying Information:\n\n");
    // displaying information
    for(i=0; i<20; ++i)
    {
       // printf("\nRoll number: %d\n",i+1);
        printf("Name: ");
        puts(s[i].name);
        printf("dept: ");
        puts(s[i].dept);
        printf("salary %d",s[i].salary);
        printf("\n");
    }
    return 0;
}

No comments:

Post a Comment