Loading....
Coupon Accepted Successfully!

 

Arrays of Structures

  • An array contains similar elements, the combination having structures with in an array is called array of structure.
  • To declare an array, define the structure and then declare the array variable next.
  • For example
    • Stddet name[10];
Where stddet - structure name
Name[10] - array variable
  • Program to store information of 10 employees and to display information of an employee depending upon the employee no given.

#include<iostream.h>

#include<conio.h>

struct addr{

int houseno;

char area[10];

char city[10];

char state[15];

};

struct emp{

int empno;

char name[20];

char design[15];

addr address;

float basic;

}worker;

emp sales_emp[10];

void display(int a);

void enter(void);

main()

{

int eno,i;

char ch;

enter();

do

{

cout<<"enter the employee no";

cin>>eno;

int flag=0;

for(i=0;i<10;++i)

{

if(sales_emp[i].empno==eno)

{

display(i);

flag=1;

break;

}

}

if(flag)

{

cout <<"no mach of the employee number";

}

cout<<"display more(y/n");

cin >>ch;

}while(ch=='y');

return 0;

}

void enter(void)

{

for(int i=0;i<10;i++)

{

cout<<"employee no";

cin>>sales_emp[i].eno;

cout<<"employee name";

cin>>sales_emp[i].name;

cout<<"employee designation";

cin>>sales_emp[i].desig;

cout<<"employee address";

cin>>sales_emp[i].address.houseno;

cout<<"employee area";

cin>>sales_emp[i].address.area;

cout<<"employee city";

cin>>sales_emp[i].address.city;

cout<<"employee state";

cin>>sales_emp[i].address.state;

cout<<"employee basic";

cin>>sales_emp[i].basic;

}

return;

}

void display(int a)

{

cout<<"employee data";

cout<<"employee no"<<sales_emp[a].empno;

cout<<"name";

cout.write(sales_emp[a].name,30);

cout<<"designation";

cout.write(sales_emp[a].desig,30);

cout<<"employee address"<<sales_emp[a].address.houseno;

cout.write(sales_emp[a].address.area,26);

cout.write(sales_emp[a].address.city,26);

cout.write(sales_emp[a].address.state,30);

cout<<"employee basic salary"<<sales_emp[a].basic;

return;

}

Note: Arrays can also be used with in the structure and accessed by using dot operator. For example

struct std{

int no;

char name[10];

};

std student;

To access data

student. no=10;

student. name="ravi";





Test Your Skills Now!
Take a Quiz now
Reviewer Name