Home | | Computer Science 11th std | C++: Array of Structures

Example Program - C++: Array of Structures | 11th Computer Science : Chapter 12 : Arrays and Structures

Chapter: 11th Computer Science : Chapter 12 : Arrays and Structures

C++: Array of Structures

A class may contain many students. So, the definition of structure for one student can also be extended to all the students.

Array of Structures

 

A class may contain many students. So, the definition of structure for one student can also be extended to all the students. If the class has 20 students, then 20 individual structures are required. For this purpose, an array of structures can be used. An array of structures is declared in the same way as declaring an array with built-in data types like int or char.

The following program reads the details of 20 students and prints the same.

#include <iostream>

using namespace std;

struct Student

{

      int age;

      float height, weight;

      char name[30];

};

void main( )

{

Student std[20];

int i;

      cout<< “ Enter the details for 20 students”<<endl;

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

{

      cout<< “ Enter the details of student”<<i+1<<endl;

      cout<< “ Enter the age:”<<endl; cin>>std[i].age;

      cout<< “Enter the height:”<<endl;

      cin>>std[i].height;

      cout<< “Enter the weight:”<<endl;

      cin>>std[i].weight;

}

      cout<< “The values entered for Age, height and weight are”<<endl;

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

      cout<<”Student ”<<i+1<< “\t”<<std[i].age<< “\t”<<std[i].height<< “\t”<<std[i].weight;

}

Output:

Enter the details of 20 students

Enter the details for student1

Enter the age:

18

Enter the height:

160.5

Enter the weight:

46.5

Enter the details for student2

Enter the age:

18

Enter the height:

164.5

Enter the weight:

61.5

.......................

.....................

The values entered for Age, height and weight are

Student 1    18      160.5 46.5

Student 2    18      164.5 61.5

............

The above program reads age , height and weight of 20 students and prints the same details. The output is shown for only two students due to space constraints.

Let an organization has three employees.If we want to read and print all their details thenan array of structures is desirable for employees of this organization. This canbe done through declaring an array of employee structures.

include<iostream>

using namespace std;

struct Employee

{

char name[50];

int age;

float salary;

};

int main()

{

Employee e1[3];

int i;

cout<< “Enter the details of 3 employees”<<endl;

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

{

      cout<< “Enter the details of Employee”<< i+1<<endl;

      cout<< "Enter name: ";

      cin>>e1[i].name;

      cout<<endl<<"Enter age: ";

      cin>>e1[i].age;

      cout<<endl<< "Enter salary: ";

      cin>>e[i]1.salary;

}

      cout<< "Displaying Information" <<endl;

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

{

      cout<< “ The details of Employee” <<i+1<<endl;

      cout<< "Name: " <<e1[i].name <<endl;

      cout<<"Age: " <<e1[i].age <<endl;

      cout<< "Salary: " <<e1[i].salary;

return 0;

}

Output:

Enter the details of 3 employees

Enter the details of Employee 1

Enter name:

Lilly

Enter age:

42

Enter salary:

40000.00

Enter the details of Employee 2

Enter name:

Aster

Enter age:

38

Enter salary:

60000.00

Enter the details of Employee 3

Enter name:

Jasmine

Enter age:

45

Enter salary:

80000.00

Displaying Information.

The details of Employee 1

Name:Lilly

Age: 42

Salary: 40000.00

The details of Employee 2

Name:Aster

Age:38

Salary:60000.00

The details of Employee 3

Name:Jasmine

Age:45

Salary:80000.00

 

Tags : Example Program , 11th Computer Science : Chapter 12 : Arrays and Structures
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 12 : Arrays and Structures : C++: Array of Structures | Example Program


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.