Clilstore Facebook WA Linkedin Email
Login

This is a Clilstore unit. You can link all words to dictionaries.

Structure and its description

It is known that arrays are used to combine elements of the same-type. To combine elements of different types in C ++ language the concept of structure is used that is similar to the concept of record in Pascal.

Structure is a combination of thedifferent types of attributes, pertaining to a single object.

Type structure is commonly used during the development of information systems and databases.

The description format of the structural type is as follows:

struct name_of the type

{Definitions of elements};

For example: Given information on 2 students, consisting of 5 fields: Full name, date of birth, faculty, course, student ID number.

During description of this structure, names of elements (fields) of the system shall be understandable and easy to read. For the considered example, the definition of the appropriate structural type can be as follows:

struct Student

{char Fam[20];

int God_r;

char Fak[10];

int Kurs;

char Nom_b;};

There after, student becomes the name of the structural type, which can be assigned to the certain variables, for example S1 and S2.

Student S1, S2;

Reference to elements (fields) of the structural value is performed using a qualified name of the following format:

name_of the structure. name_of the element;

For example: S1. Fam – surname of the first student.

S2. Fak – faculty, where the second student is studying.

S2. God_r – the year of birth of the second student

The values of elements of the structure can be defined by input or assignment.

For example: Student S1={“Gizatova”, 1997, ”phys-math”, 1, “PH00234”};

Usually, the arrays of structures are used in programs. For example, information about 10 students can be stored in the array described as follows: Student S [10];

Then, information about individual students will be specified:

S[1].Fam, S[5].Kurs, S[8].Fak etc.

Data input and output regarding 10 students of Student structure described above, is performed as follows:

// input

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

cin>>S[i].Fam>>S[i].God_r>>S[i].Fak>>S[i].Kurs>>S[i].Nom_b;

// output

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

cout<<S[i].Fam<<S[i].God_r<<S[i].Fak<<S[i].Kurs<<S[i].Nom_b;

Example 57. There are data on 5 students: surname and marks for exams for three subjects. Display the names of students and their average grade.

main()

{ int i; float k;

struct stud

{char fam[20];

int x,y,z;};

stud s[5];

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

cin>>s[i].fam>>s[i].x>>s[i].y>>s[i].z;

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

{k=(s[i].x+s[i].y+s[i].z)/3;

cout<<"\n"<<s[i].fam<<"\t"<<k;}

}

Example 58. Given the age of five students. Sort the data in descending order.

main()

{ int i, j, k;

struct stud

{int jas;};

stud s[5];

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

cin>>s[i].jas;

// bubble sorting

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

for (j=i+1; j<5; j++)

if (s[i].jas<s[j].jas) {k=s[i].jas; s[i].jas=s[j].jas; s[j].jas=k;}

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

cout<<"\n"<<s[i].jas;

}

Example 59. Given information about products and prices. Find the most expensive product.

main()

{ int i, k, max;

struct Tovar

{char name[15];

int baga;};

Tovar t[5];

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

cin>>t[i].name>>t[i].baga;

max=t[0].baga; k=0;

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

if (max<t[i].baga) {max=t[i].baga;k=i;}

cout<<"\n Name of the product="<< t[k].name<<" Price ="<< max;

}

Example 60. Given information about student surname and grade, where he is studying. The program displays the data of students, whose surname begins with the given letter and who are studying in a given grade.

main()

{ int i,n; char s[10];

struct Okushi

{char fam[15];

int kl;};

Okushi ok[5];

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

cin>>ok[i].fam>>ok[i].kl;

cout<<"\n input the first letter "; cin>>s;

cout<<"\n input the grade"; cin>>n;

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

if (ok[i].fam[0]==s[0] && ok[i].kl==n) cout<<"\n fam="<<ok[i].fam;

}

Clilstore

Short url:   https://clilstore.eu/cs/6254