Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лабораторні, на англійській.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
604.67 Кб
Скачать

1.5 Arrays of Structures

It’s also possible to create arrays whose elements are structures. The technique is exactly the same as for creating arrays of the fundamental types. For example, to create an array of 100 inflatable structures, you could do the following:

inflatable gifts[100]; // array of 100 inflatable structures

This makes gifts an array of inflatables. Hence each element of the array, such as gifts[0] or gifts[99], is an inflatable object and can be used with the membership operator:

cin >> gifts[0].volume; // use volume member of first struct

cout << gifts[99].price << endl; // display price member of last struct

Keep in mind that gifts itself is an array, not a structure, so constructions such as gifts.price are not valid.

To initialize an array of structures, you combine the rule for initializing arrays (a brace-enclosed, comma-separated list of values for each element) with the rule for structures (a brace-enclosed, comma-separated list of values for each member). Because each element of the array is a structure, its value is represented by a structure initialization. Thus, you wind up with a brace-enclosed, comma-separated list of values, each of which itself is a brace-enclosed, comma-separated list of values:

inflatable guests[2] = { // initializing an array of structs

{“Bambi”, 0.5, 21.99}, // first structure in array

{“Godzilla”, 2000, 565.99} // next structure in array

};

Listing 3 shows a short example that uses an array of structures. Note that because guests is an array of inflatable, guest[0] is type inflatable, so you can use it with the dot opera-tor to access a member of the inflatable structure.

Listing 3

struct inflatable {

char name[20];

float volume;

double price;

};

void main() {

inflatable guests[2] = { // initializing an array of structs

{“Bambi”, 0.5, 21.99}, // first structure in array

{“Godzilla”, 2000, 565.99} // next structure in array

};

cout << “The guests “ << guests[0].name << “ and “ << guests[1].name << “\nhave a combined volume of “ << guests[0].volume + guests[1].volume << “ cubic feet.\n”;

}

Here is the output of the program in Listing 3:

The guests Bambi and Godzilla

have a combined volume of 2000.5 cubic feet.

2. Lab Overview

2.1. Read the theory and try Control Exercises.

2.2. Develop the algorithm flowchart to create input data and handle structures array according to individual case from the Table below.

2.3. Write the program code according to the developed algorithm. Dynamic variables, pointers, user-defined functions should be used.

2.4. Debug the program, run it and make screenshots.

2.5. Prepare the Lab Report according to the required structure.

#

Structures array

Operation on array

Books

Find a book with a minimum number of pages

Vegetables

Find vegetables with red color

Computers

Output a configuration of the most expensive computer

Films

Find a film with a highest budget

Shoes

Output shoes with a price more than average one

Cell phones

Output Nokia cell phones

Students

Calculate the group average rating

Laptops

Output laptops with a screen diagonal more than 15 inches

Fruits

Output fruits with weight less than 200g

Shirts

Find a shirt with a lowest price

Bags

Find a bag with a highest number of pockets

Soundtracks

Output soundtracks of the film “Transformers”

Drinks

Output non-alcoholic drinks

Pens

Output black pens with a price more than $100

Workers

Find a worker with a highest salary

Cars

Output cars of 2012 year

Songs

Output songs with a rating bellow the indicated one

Soft

Output freeware soft

Animals

Calculate the total weight of all animals in the database

Countries

Output countries with a population more than the population in Ukraine

Soccer Teams

Output teams which won Champion League more than 3 times

Tanks

Output middle weight tanks

Motorcycles

Output motorcycles with engine volume more than 50 sm3

Rock groups

Output rock groups from Ukraine

Cosmetics

Output goods from Avon